Scott Hanselman

.NET 4.1 Preview - New Base Class Library (BCL) Extension Methods - RFC

April 1, '09 Comments [16] Posted in ASP.NET | DLR | Javascript | Learning .NET | Microsoft | Musings | Open Source | PHP | Programming | Python | Silverlight | Source Code | Tools | VB | Web Services | Windows Client | XML
Sponsored By

As web programmers, we use a lot of strings to move data around the web. Often we’ll use a string to represent a date or an integer or a boolean. Basically "1" in instead of 1, or "April 1, 2009" rather than a proper ISO-8601 formatted culture-invariant date.

While these strings are flying around via HTTP it's not a huge deal, but sometimes this loose, even sloppy, use of strings can leak into our own code. We might find ourselves leaving the data times as strings longer and longer, or not even bothering to convert them to their proper type at all. This problem is made worse by the proliferation of JSON, and schema-less/namespace-less XML (that I've often called "angle-bracket delimited files" as they're no more useful than CSVs in that point.

.NET 4.0 is pretty much locked down, but version 4.1 still has some really cool "Futures" features that are being argued about. If we don't know the type of a string, or we want to leave the string, as a string, longer than usual, what if we had an class that could be both a string and another type, essentially deferring the decision until the variable is observed. For example:

StringOr<int> userInput= GetUserInput("Quantity"); 
string szUserInput=userInput.StringValue; 
int intUserInput=userInput.OtherValue;

Sometimes you just don't know, or can't know.

This reminds me of a similar, but orthogonal physics concept, that of the Heisenberg Uncertainty Principle. Sometimes you know that an object is a string, and sometimes you know how long it is, but you can’t know both at the same time.

One of my favorite jokes goes:

Heisenberg gets pulled over by the police. The officer asks, “Do you know how fast you were going?” Heisenberg answers, “No, but I know exactly where I am!”

This library doesn't solve THAT issue, with respect to strings, but we’ve got folks in DevDiv working on this and many other metaphysical - and physical - problems as they apply to computer science.

Big thanks to Eilon, who's working hard to get this pushed into the .NET 4.1 Base Class Library. Visit Eilon's blog for more details on this new library, more code, graphics and details on how Intellisense will handle this new case.

Hopefully, someone is working to make this important new library Open Source.

Your thoughts, Dear Reader?

Related Posts

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Paving my machine for a fresh 2009 - First-Pass Must-Haves

March 31, '09 Comments [62] Posted in Tools
Sponsored By

Back in 2006 I paved (or "formatted", or "torched", or "gave "a technical enema") to my main machine and blogged about the what I call the first-pass software I installed on this new fresh machine.

It was time to torch my Atwood Quadpower machine and name it Quadpower7. Time for a clean install, and this time it includes Windows 7 x64 Ultimate.

I know I have a Tools List (that I know needs to be updated, thank you) but what I needed on this new machine was to be as productive as possible as fast as possible. I needed the 80% stuff installed. 

First-Pass Must-Have Apps for a Fresh Machine and a Happy Life

Here's what I installed. Total time from naked drive to productive was about 4 hours. Could have been faster had I downloaded things ahead of time or used CDs.

Windows Utils

  • 7-Zip - Best Zip Tool with the best shell integration.
  • AutoHotkey and the AutoCorrect for English Script - This will change "teh" to "the" everywhere I type. Plus 4700 other common English misspellings.
  • SmartFTP - Love them and gave them money. Got the 64-bit version.
  • Paint.NET - The best free Image Editor in town. Thanks Rick Brewster!
  • Prish Image Resizer - If there's one secret super amazing gem in this list, it's Prish. After I pave my wife's machine this is the ONLY util she insists on. She refuses to even touch the machine - "Is Prish on? No? Don't talk to me." Go get it and bask in the wonder of this glorious little app that's hard to find on the web. Gotta get this man a domain name.
  • Notepad2 3.0 - It's like notepad, but 2. Er, 3.0.

Developer Stuff

Easiest and fastest way to get productive is to use the Web Platform Installer. It also configures IIS, installs VS and SQL, and can install Silverlight tools, etc. if you like. This tool alone saves me HOURS each time I pave a machine.

Organizational Stuff

Blogging Stuff

  • Windows Live Writer - I love this team and I love their product. Also installed the Pre-Code Plugin for my code samples.
  • SnagIt and WindowClippings - SnagIt because their image editor is butter, and WindowClippings because it's amazing with transparency.
  • FeedDemon - I know you guys are all Google Reader and all, but I really prefer to read my blogs in a REAL client. FeedDemon's awesome, it's free, and it's written in Delphi. What's not to like? 

Social Stuff

  • TweetDeck - My current Twitter power-client of choice.
  • Zune - Because the Zune Pass kicks ass, as does Jazmine Sullivan.
  • Messenger - Cuz you gotta chat.
  • Skype and ooVoo - The former so I can talk cheap around the world, and the latter so I can video chat with up to 6 other people in 640x480 high res.

Pretty Stuff

  • Mike Swanson's Windows 7 Theme Pack - Some of Mike's best images for my wallpaper, auto-updating every 30 minutes.
  • VLC Player - For playing any video that I don't feel like getting a Codec for. (Which is fewer and fewer as there's lots of codecs included in Win7.)

And there you go. That was it. There's other stuff, but it's part of the 20% and I'll install them as I need them.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Mix09: Hanselminutes on Channel 9

March 28, '09 Comments [8] Posted in ASP.NET | ASP.NET MVC | Channel9 | Microsoft | Mix | Podcast
Sponsored By

Channel 9 Guy While I was running around Mix09 and the MVP Summit this last few weeks I was taking video with my Flip Ultra and my new Creative Vado HD (I took the Flip Mino HD back, as I thought the quality was poor). I put a bunch of these videos up on Channel 9 and declared it "Hanselminutes On 9." It's not official, but maybe if you leave comments they'll make it a regular thing. Some of the videos are pretty cool. Others, totally useless. ;)

While Channel 9 recompresses my already compressed streams, I want to point out that the letterboxed 16:9 videos were done with the Creative Vado HD, then the aspect ratio was changed with Expression Encoder. The 4:3 videos were done with my regular Flip Ultra. Let me know which ones you prefer more.

If you like this idea, I'll start taking my cameras everywhere and when I talk to interesting people, I'll throw the videos up on Channel 9 as I make them. I started doing this almost two years ago with Rory and a bunch of videos were recorded, but those recordings are lost in the sands of time. Here's the first "pilot" episode of Hanselminutes on 9 with Polita Paulus, though. You can see from the old comments that it was a pretty decent show, that.

Johnny Lee
Johnny Lee on Computer Vision
Wow. I just bumped into Johnny Lee in the halls here at Mix09. I'm a huge fan boi with a man-crush on this dude. You've seen Johnny before on Channel 9 talking to Robert Hess.  Johnny's a legend (in my mind) in the computer vision space, and he put up with me gushing at him here at Mix09. We chatted in the hall about computer vision, what he's working on, how he got the gig at Microsoft and where he sees the future of human-computer-interaction.
Eilon Lipton
Eilon Lipton and the ASP.NET MVC Rumors
In this episode of Hanselminutes on 9, I break into Eilon Lipton's office and demand the real scoop on who or what wrote ASP.NET MVC. The word on the street is that ScottGu wrote the original prototype, codenamed "Scalene" on an airplane. Is this true? I pretend to turn off my camera and fool Eilon, the lead developer on ASP.NET MVC, to give up the goods.
See how he gazes lovingly at me? That's just the kind of bromance that Eilon and I have.
image
Phil Haack on the ASP.NET MVC 1.0 Release
This Hanselminutes on 9 video was filmed at the MVP Summit on the Redmond Campus. I was on campus for the week with my trusty Flip Video camera and I pulled Phil Haack outside and demanded the real scoop on ASP.NET MVC. I beat him about the head and ears and asked questions like "what qualifies you to be a PM at Microsoft!" If you like this kind of hard-hitting journalism, tell the folks at Channel 9 to give "HanselminutesOn9" its own Tag or Category for Pete's Sake! Wink
image 
Joel Spolsky and Jeff Atwood preparing for StackOverflow Keynote
Security is tight here at Mix09 but I've snuck past the guards and followed Joel Spolsky (JoelOnSoftware) and Jeff Atwood (CodingHorror) up the escalator to their Keynote Rehearsal. The fans went wild! Well, one fan. A lady on the escalator recognized them. But still! The air is electric. Let's listen in...
image 
Rob Conery and the MVC Storefront
I'm wandering the halls in Building 42 and bump into the man himself, Rob Conery. This is amazing for a number of reasons, not the least of which being that Rob lives in Hawaii. I put the hot lights up to Rob and get him to confess to a number of transgressions. Turns out he IS still working on the MVC Storefront and will be giving us the source in some form or another. Most likely in the form of source. So, we got that going for us.
image 
Brad Wilson has Lunch
In this completely useless video (you have been warned),  Scott bumps into Brad Wilson in the halls of Building 42 and finds out what Brad is having for lunch. He also says a little about the Data Annotation-based Model Binder that they showed at Mix 09. A glimpse at what is to come in the unholy marriage of ASP.NET MVC and ASP.NET Dynamic Data. I shudder to think of the possibilities. A small shudder, to be clear, but it's there. Almost palpable.

I also want to point out, that while it's not totally obvious, there are some tiny icons next to the images when you get to the Channel 9 site. They look like this:

Picture of Tiny C9 Icons

These icons are clickable and mean each video is available in a number of formats, including portable device ones, and also that you can easily share these links/videos on various social networking sites. So please do.

Enjoy!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

The Weekly Source Code 39 - Commodore 64 Emulator in Silverlight 3

March 27, '09 Comments [8] Posted in Mix | Open Source | Silverlight | Source Code | WPF
Sponsored By

C64 Application Icon I had the pleasure of interviewing Pete Brown this last week and talking about the Silverlight 3 Commodore 64 Emulator he's been working on. He just launched the CodePlex site a few minutes ago (my time), but I've had the code for a while to play with. You can read Tim Heuer's blog post for details on how to get started with Silverlight 3 Beta and the tools you'd need or see some video of the emulator in action.

Silverlight C64 Emulator

Keep in mind that this is a labor of love that Pete's doing, and the code has been written in "gett'er done" mode, so it won't win any awards for aesthetic. A lot of the code as been ported directly over from Open Source C++ in the Frodo Emulator or from Sharp C64.

It does have some pretty clever ideas, though, and I thought I'd take a look at those in this Weekly Source Code (which I promise to make more Weekly, starting now).

Dynamically Creating a Video Stream

Pete wanted the screen to draw as fast as possible, which is 50Hz (50 times a second). He was originally creating PNGs or Bitmaps and throwing it up on the screen as fast as possible, but then a member of the Silverlight team suggesting "making a video." What did he mean by "making a video?" He suggested actually using a Silverlight MediaElement (the "video player" control) and acting as a DataSource for a video. He's dynamically creating a movie that never ends.

This means the UI XAML is basically:

<MediaElement x:Name="VideoDisplay"
Grid.Row="0"
Grid.Column="0"
VerticalAlignment="Top"
Stretch="Uniform"
IsHitTestVisible="False"
Margin="4" />

And in the code behind he creates a VideoMediaStreamSource the had blogged about here, deriving from MediaStreamSource:

_video = new VideoMediaStreamSource(TheSID.Renderer.AudioStream, C64Display.DISPLAY_X, C64Display.DISPLAY_Y);

and it looks like:

private byte[][] _frames = new byte[2][];
public VideoMediaStreamSource(int frameWidth, int frameHeight)
{
_frameWidth = frameWidth;
_frameHeight = frameHeight;

_framePixelSize = frameWidth * frameHeight;
_frameBufferSize = _framePixelSize * BytesPerPixel;

// PAL is 50 frames per second
_frameTime = (int)TimeSpan.FromSeconds((double)1 / 50).Ticks;

_frames[0] = new byte[_frameBufferSize];
_frames[1] = new byte[_frameBufferSize];

_currentBufferFrame = 0;
_currentReadyFrame = 1;
}

public void Flip()
{
int f = _currentBufferFrame;
_currentBufferFrame = _currentReadyFrame;
_currentReadyFrame = f;
}

When he wants to write a pixel to his buffer, as he often does at the low level:

public void WritePixel(int position, Color color)
{
int offset = position * BytesPerPixel;

_frames[_currentBufferFrame][offset++] = color.B;
_frames[_currentBufferFrame][offset++] = color.G;
_frames[_currentBufferFrame][offset++] = color.R;
_frames[_currentBufferFrame][offset++] = color.A;

}

When it comes time to get a sample, the MediaSteamSource calls GetSampleAsync:

protected override void GetSampleAsync(MediaStreamType mediaStreamType)
{
if (mediaStreamType == MediaStreamType.Audio)
{
GetAudioSample();
}
else if (mediaStreamType == MediaStreamType.Video)
{
GetVideoSample();
}
}

He grabs a video frame from his buffer, he make a sample and reports he's done:

private void GetVideoSample()
{
_frameStream = new MemoryStream();
_frameStream.Write(_frames[_currentReadyFrame], 0, _frameBufferSize);

// Send out the next sample
MediaStreamSample msSamp = new MediaStreamSample(
_videoDesc,
_frameStream,
0,
_frameBufferSize,
_currentVideoTimeStamp,
_emptySampleDict);

_currentVideoTimeStamp += _frameTime;

ReportGetSampleCompleted(msSamp);
}

His app makes frames as fast as they can, putting them in the buffer at 50Hz, and the MediaElement requests frames from his VideoMediaStreamSource as fast as it can take them.

Emulating a 1541 Disk Drive

There's a file format in the world of C64 emulators that everyone has standardized on called .d64. The D64Drive.cs file contains the meat of the code to read these image files. "The *.D64 file format is a 1:1 copy of all sectors as they appear on a floppy disk."

Most of it looks like C/C++ code, because it once was. Some of it used to be "unsafe" C# code, writing with the unsafe keyword so the runtime could pin down pointers and use them directly.

I love it when there's things like byte[] magic. ;) Seems like every binary file format has them. In this case, we're looking for 0x43, 0x15, 0x41 and 0x64. Notice that 0x43 is "C", while the second and third bites are "1541" with the final "64" in there. ;)

private void open_close_d64_file(string d64name, Stream fileStream)
{
long size;
byte[] magic = new byte[4];

// Close old .d64, if open
if (the_file != null)
{
close_all_channels();
the_file.Dispose();
the_file = null;
}


// Open new .d64 file
if (fileStream != null)
{
//the_file = new FileStream(d64name, FileMode.Open, FileAccess.Read);
the_file = fileStream;

// Check length
size = the_file.Length;

// Check length
if (size < NUM_SECTORS * 256)
{
the_file.Dispose();
the_file = null;
return;
}

// x64 image?
the_file.Read(magic, 0, 4);
if (magic[0] == 0x43 && magic[1] == 0x15 && magic[2] == 0x41 && magic[3] == 0x64)
image_header = 64;
else
image_header = 0;

// Preset error info (all sectors no error)
Array.Clear(error_info, 0, error_info.Length);

// Load sector error info from .d64 file, if present
if (image_header == 0 && size == NUM_SECTORS * 257)
{
the_file.Seek(NUM_SECTORS * 256, SeekOrigin.Begin);
the_file.Read(error_info, 0, NUM_SECTORS);
}
}
}

This is all fun stuff, but as Pete said to me in an email:

"PS. My “real” code *never* looks like this. This is full of c++-isms and just plain “let me see if I can get this to work” junk."

So, take it for what it is. It's awesome.

References

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Hanselminutes Podcast 155 - A C64 Emulator with Silverlight 3 by Pete Brown

March 27, '09 Comments [4] Posted in Podcast | Programming | Silverlight | WPF
Sponsored By

c64screen[1] My one-hundred-and-fifty-fifth podcast is up. Scott digs deep with Pete Brown about the Commodore 64 Emulator he is writing in Silverlight 3. Is Silverlight fast enough? What about offline support? What Silverlight 3 features made the job easier? All this and next steps in this week's show. See video of the emulator in action!

Subscribe: Subscribe to Hanselminutes Subscribe to my Podcast in iTunes

Do also remember the complete archives are always up and they have PDF Transcripts, a little known feature that show up a few weeks after each show.

Telerik is a sponsor for this show!

Building quality software is never easy. It requires skills and imagination. We cannot promise to improve your skills, but when it comes to User Interface, we can provide the building blocks to take your application a step closer to your imagination. Explore the leading UI suites for ASP.NET and Windows Forms. Enjoy the versatility of our new-generation Reporting Tool. Dive into our online community. Visit www.telerik.com.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

Enjoy. Who knows what'll happen in the next show?

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.