• Eric Gunnerson's Compendium

    Hangin' with generics


    I've been using generics in a project that I'm using on Whidbey. They are tremendously convenient when you want to use collection classes. That wasn't really very surprising.

    What was surprising - mostly because I hadn't spent any time reading the generics spec - was how useful generic methods would be. For my app, I sometimes needed to fetch a list of values from a database. I didn't want to write a separate function for each type,  and with generic methods, I could write the following:

    public List<T> GetValues<T>(string selectStatement)
     OleDbCommand select = new OleDbCommand (selectStatement, connection);
     List<T> list = new List<T> ();
     OleDbDataReader reader = select.ExecuteReader ();  while (reader.Read())
      list.Add ((T)reader[0]);
     return list;

    I think that's pretty cool. I wrote several functions like that that perform database operations in a generic (ha ha!) way. So I think generics are going to be cooler than I expected.

    I should mention one more thing. If the type you use in the generic method is one of the parameters, the compiler can figure out what generic type you're using, and you don't have to specify it. If I wrote:

            public T Process<T>(T param) {...}

    I could call it with:

            int i = inst.Process(12);

    and the compiler figures out that I want Procees<int>.

    Oh, and before you ask me for the spec, you can't have it... yet... It's still churning due to the ECMA process, and we don't want to release it until it's a bit more concrete.

  • Eric Gunnerson's Compendium

    WMP, album art, and Amazon Web Services


    So, I'm working on a music playing application (to be revealed sometime around PDC, or perhaps a bit later if I don't get it working) to play around with Whidbey features. One of the things I want to do is be able to display the album art.

    So, I dust off the docs for WMP - which are a bit spotty in places - and go searching for the album art. For a media item in WMP, you can fetch various attributes, and one of them is WM/CoverArt (or something like that). Seems like just the ticket, but none it's not set for any of the items in my collection.

    I go searching for another way to get the art. There's a MoreInfo attribute that you can fetch for all the items, but it points to a non-existant page. I play around a bit more and find that I can fix the URL to get to the "More Info" page, which has a link to the album art. A quick Regex, and I pull the cover art url out. I code it up, and run it on a small subset of my album selection (about 20 albums). There is no album art for 5 of the 20 albums.

    That's pretty pathetic. It's not like I had a lot of obscure albums there - they were all albums that had certified at least platinum. Now, it may not have had all the albums, but it was really slow, so I had that going for me.

    A bit of time on Google led me towards Amazon web services.  The SDK is free, and using it is free, as long as you don't do more than 1 request a second per application (multiple instances can do more). The Details object has a link to the album art, so I just needed to get that object for the album. So, I started up VS, added the WSDL as a web reference, and went to town.

    The first attempt was to use keyword searching. That worked well if I as looking for something like "Green Day Dookie", but something like "rush rush" gives you 282 matches, some in artist, some in album. The basic problem is that you're just looking at keywords, and you can't specify album and artist directly. Nor can you find that in the Details object - it just has a single property named ProductName.

    The solution was to do an Artist search. This gives me all albums with a specific value in the artist field (once again, a keyword search, not an exact search). You then need to look through all the matches you got back and match against the album you want. This works, but is a bit ungainly, but at least you don't need to do it that often.

    Here's some code:

    			ArtistRequest request = new ArtistRequest();
    			request.artist = albumCoverArt.Artist;
    			request.devtag = ericsDevtag;
    			request.mode = "music";
    			request.type = "lite"; = "1";
    			AmazonSearchService search = new AmazonSearchService();
    			ProductInfo productInfo = null;
    			productInfo = search.ArtistSearchRequest(request);

    That gets the first chunk of data, and you have to make other calls 1 second apart to get the rest. The devtag is given to you when you register with Amazon, and it identifies who's using the service.

  • Eric Gunnerson's Compendium

    Question of the day

    If you put wheat bread into a toaster and take out wheat toast, what happens when you put french bread in a toaster?
  • Eric Gunnerson's Compendium

    Lunch with Bill Venners and Bruce Eckel


    Yesterday I had the pleasure of having lunch with Bill Venners and Bruce Eckel. Bill runs, a cool site with lots of interesting content. Bruce, of course, is the noted author of Thinking in C++ , and Bruce taught me C++, or at least his book "Using C++" helped a bunch, way back in the early 1990s before I came to Microsoft.

    They were here doing interviews, and we (Anders, Dan Fernandez, and I) talked a fair bit about language design and philosophy with them. Anders couldn't be there the whole time, so I fielded questions the rest of the time. One of the best ones that Bruce asked was, "How did the design team know that they were doing the right thing, absent real feedback. How did you avoid creating problems the way the C++ committee did?" (Bruce can say that since he was on the C++ standards committee for a number of years).

    The answer is a two-part answer. All of us on the design team were real-world programmers, and Anders has a very practical approach to language design. The second reason is that we had a large group of framework programmers using C# as we developed it, and they were not shy when they found it too hard to do things with our current design.

    A very enjoyable hour. Bruce has a blog.

  • Eric Gunnerson's Compendium

    What I Ride, and learning to Ride


    I mentioned a day or so ago that I have a motorcycle. A little clarification is in order.

    I started riding in 1986 when my 1969 Citroen Safari broke a timing chain at 70 MPH, and embedded a couple of valves in the pistons. It was either a cheap car or a decent motorcycle, and a Honda FT500 made its way to my apartment. After a few months of riding, I got my endorsement, and started riding from time to time. Kim was also interested in riding, but the FT was a bit tall for her, and her attempts were not successful.

    Enter the Motorcycle Safety Foundation. MSF was formed in the 1970s to provide, not surprisingly, motorcycle training. I took their Experienced Rider Course, and Kim took the basic course. The basic course is a wonderful way to find out if you really want to ride a motorcycle - the course provides motorcycles, helmets, and a world-class curriculum, all for a few hundred dollars or less. In many states, you walk away from the course having earned a motorcycle endorsement (assuming you pass).

    Kim's success led to a 1988 Ninja 250 for her, which was soon followed by a 1989 EX500 for me, followed closely after by an MSF instructor course for me.

    MSF takes its instructor training very seriously, and I spent 10 days doing nothing but motorcycle training. After that, I taught a lot of classes, and did a lot of riding, commuting year round by motorcycle for about a decade. The Ninja 250 was replaced by a CBR600F2 for Kim, and a few years later, my EX500, having hit 50K miles, was replaced with a 1997 VFR750, my current bike (though mine is a bit more customized than this one).

    Kim liked the power of her CBR, but it's a little too tall for her (she's somewhat height-challenged). So, where do you find front-line sportbikes for people who are a bit on the short side? The answer is obvious. All the Japanese manufacturers make home market bikes that would be perfect. A friend led me to somebody who imports Japanese-spec bikes and registers, and the CBR was soon replaced with a very rare in the states Honda CBR400RR, which is pretty much the perfect wife for Kim

    Then, a few years ago I moved near the Microsoft campus and got a lot more busy, so I had to give up teaching motorcycle safety, and I still have a hard time finding time to ride.

    I missed a few high points, including an introduction to the pavement, riding a GP spec 125cc bike, and being a passenger around a racetrack at 130MPH, but that's for another entry.

  • Eric Gunnerson's Compendium

    The persistance of nomenclature


    Yesteraday, I was struck by the fact that in a technological world, nomenclature outlasts the items it refers to.

    When I first joined Microsoft ("The year was 1994. The place was Redmond, Washington..."), we used MS Mail for email, and for scheduling, there was part of it named Schedule+. If you wanted somebody to schedule a time with you, you would ask them to "sched+" a meeting with you.

    That usage has persisted over the years, and just yesterday I found myself using it. As I used it, I wondered if the person I was sending that message to had ever actually used Schedule+.

    There's a similar situation with our bug tracking software. For years, we used various releases of an internal product named "Raid", and though we moved to Product Studio (the "studio" term shows up a lot...), you still "Raid" a bug, and probably always will.

    When new people start and you have to explain this to them, you feel a bit strange (don't worry about uncle Herbert. He does think that he's a plant and insist we water him every day, but other than that, he's perfectly all right).

    This morning, I was looking for more examples of this. Record/CD is one, though I always use "album", and I think that's still true if you choose to buy your music. Since IP pays for my motorcycle, my skiing, I buy all my music.

    Are there other examples of this?



  • Eric Gunnerson's Compendium

    What does a Program Manager do? (1)


    Today, Scoble gave me a field promotion.

    After I publicly gave him a hard time for it, I got thinking about what a strange thing it is to be a program manager, and that some people might be interested in what we really do. I'm therefore going to talk about what I do from time to time.

    The last few days, I've been trying to create a summary of an SDR the C# team held recently. An SDR (Strategic Design Review, Software Design Review) is when we bring in a group of customers, NDA them, and then show them what our plans are for the next release. They then tell us what they think, and we go back and revise our plans based on that feedback.

    To create the summary, I go through the official notes we have, the MP3 recordings we made, and the summaries that other people wrote, and try to pull out the salient points for each session that we want to use to get the right things happening. Whenever possible, I use customer quotes, because if a customer says, "It would be better for customers if you killed <x> and forced your customers to buy <y> instead", it's not *my* opinion, it's the customer's opinion.

    An SDR is always a humbling experience, as our attendees a very good at telling us what we're doing wrong (and believe me, we're doing a lot wrong).

    The summary will be created as a powerpoint presentation (being good at powerpoint is an important PM skill), which will be forwarded throughout the developer division.

  • Eric Gunnerson's Compendium

    Unit testing and TDD


    Back in March, I wrote a column entitled "Unit Testing and Test-First Development".

    I've been playing around with unit testing a bit more since then, and have a few tentative conclusions.

    1. Unit testing is, overall, a "Good Thing"
    2. Unit testing works well for class libraries, especially ones that act fairly statically. If you create one of these, you should be writing unit tests
    3. Unit testing is hard with graphical applications and/or dynamic applications. I have an app that I'm writing that uses multiple threads, fires events asynchronously, and is peer to peer. There aren't any unit tests for that section yet.
    4. Unit testing is great for tricky code that you wrote but weren't sure that it really worked. I updated my Regex Workbench a while back, and in the process wrote unit tests for all the code that I have that interprets regular expressions to english. I found one feature I hadn't implemented, and two that were implemented incorrectly. I'm now much more confident that it works.

    I'e also been playing with Test-driven development. I'm not sure about it yet, though it is true that if you write the tests up front, you're much more likely to write them.

  • Eric Gunnerson's Compendium

    Red vs. Blue


    Every week the Visual C# PM team spends a few minutes watching Red vs. Blue, a serial told using the characters in Halo on the XBox. Funny even if you don't play Halo, very funny if you do.

    If you like this, pony up the $10 or $20 to help defray the costs, and get the hi-res versions.


  • Eric Gunnerson's Compendium

    MC Hawking


    Little known fact of the day...

    Noted Physicist Stephen Hawking also has a career as a dope MC

  • Eric Gunnerson's Compendium

    Eric Sink relates to the "Software Legends" marketing campaign by putting up his own site...
  • Eric Gunnerson's Compendium

    C# Column: Web Services, Remoting, and PocketPC


    My most recent C# column has gone live on MSDN. In it, I talk about what I learned in trying to build a remote control application that runs on the PocketPC using the compact framework.

    If you have comments on the column, please enter them, and I'll be sure to respond.

  • Eric Gunnerson's Compendium

    Higher Primates Can Program


    This came to me from one of the members of the VB team.

  • Eric Gunnerson's Compendium

    Who said this?


    I was listening to something a day or so ago, and I came across one of my favorite:

    Supreme executive power derives from a mandate of the masses, not from some farcical aquatic ceremony.

    Who said this, and who did he say it to?

    If you don't know the answer, then I despair at the quality of your education. Look at the comments, where I'm sure somebody will post the answer.

  • Eric Gunnerson's Compendium

    Forms, threading, and frustration


    I have a fair amount of windows forms code that uses multiple threads. Because of the way that Windows handles its user interface, you should only be updating the user interface from the main thread. If you try to do it on other threads, bad things happen, and they can be pretty hard to track down.

    Windows forms includes some code to detect when that is happening, but it can't do it in all cases. Well, it could, but if it did, the perf would be pretty atrocious.

    When you get in this situation, you need to call Invoke() on the form, and pass it a delegate to the function that you want to be called on the main thread. In my case, I need to do an update to my form text when I get an event from the other thread. My code looks something like this:

                    // setup code
                object.RemoteUpdate += new UpdateHandler(RemoteUpdateFunc); 
            public void RemoteUpdateFunc2(object sender, RemoteUpdateEventArgs args) 
                // use the values here. 
            public void RemoteUpdateFunc(object sender, RemoteUpdateEventArgs args) 
                this.Invoke(new UpdateHandler(RemoteUpdateFunc2), new object[]
            {sender, args}); 

    I have to create a separate function just to do the forwarding, and I have to do that for every event that I want to hook to. That's a lot of boilerplate code that I don't want to write.

    So, I set out to try to create a class that could wrap the object. Here's the class that I wrote:

      	public class Invoker
    		Delegate d;
    		Form form;
    		public Invoker(Form form, Delegate d)
    			this.d = d;
    			this.form = form;
    		public Delegate Handler
    				return Delegate.CreateDelegate(d.GetType(), this, "Dispatcher");
    		public void Dispatcher(object sender, EventArgs args)
    			form.Invoke(d, new object[] { sender, args });

    The goal would be to write code like this:

               Invoker invoker = new Invoker(this, new UpdateHandler(RemoteUpdateFunc));
               object.RemoteUpdate += (UpdateHandler) invoker.Handler;

    Unfortunately, delegates can only point to methods that are *identical* to the delegate definition. You can't, for example use a delegate that's defined as:

            public delegate void EventHandler(object sender, EventArgs args);

    to point to:

            public delegate void UpdateHandler(object sender, RemoteEventArgs args); 

    even though RemoteEventArgs is derived from EventArgs. So, that means that you can only use this approach to point to delegates that have EventArgs as their second parameter, which doesn't make it very interesting.

    So, I had to abandon this approach. The alternative is to modify the class that I'm using so that it can make the call. I didn't want that class to have to have a reference to the Form class, so I created a delegate like:

            public delegate void InvokeHandler(Delegate d, object[] args);    

    and passed that to my the class that has the events. It can then use the delegate to make the Invoke happen. Not as clean as I had hoped, but it does help a bit.

  • Eric Gunnerson's Compendium

    It's Time for Language Divergence


    In It's Time for Language Divergence, Patrick Meader discusses divergence between C# and VB programmers.

    I agree that VB and C# users have differences between them, but I think that saying that the languages should diverge is a strange position. It's not about divergence, it's about the making sure that VB and C# serve their customers well. If that means language divergence, then that's fine

  • Eric Gunnerson's Compendium

    Disposing, using, and null


    I had an interesting discussion a day or so ago on when you should implement IDisposable on a class. Like many things, it's more complicated when you dig a little. There are three scenarios that are interesting:

    1) A class that wraps an unmanaged resource
    2) A class that has fields that implement IDisposable
    3) A class where no fields implement IDisposable

    The first one is the easy one. Since you are directly responsible for that unmanaged object, you will need to implement a finalizer (written using destructor syntax in C#) to do the cleanup. You will also *probably* want to implement IDisposable, so the user can call Dispose() to clean things up. There's a standard idiom in the docs for doing this.

    In the second case, there is no direct cleanup you need to do, so you shouldn't write a finalizer (what would it do?). If your users will want to clean up early, you may want to implement IDisposable and call Dispose() on your fields from your Dispose() function.

    In the third case, you should do either. If you do implement a finalizer, you will just slow things down.

    Another related question is whether setting fields to null will result in quicker recovery of memory. This was important to do in the VB6 world, but in the .NET world, it rarely does anything. The only time it would help is if there was a variable that held a live reference, but wouldn't drop off the stack for a long time. I think I could construct a loop like that, but I think it's pretty rare.

  • Eric Gunnerson's Compendium

    The Tour gets interesting...


    Yesterday's stage was the best one I've watched in a long time. Lance Armstrong gets attacked, and doesn't blow away the field. Tyler Hamilton rides with Lance despite having a broken collarbone. Jan Ulrich loses 90 seconds to Lance. Iban Mayo does blow away the field.

    All of this in front of thousands of rabid fans.

    Lance ends up in the yellow jersey, but not by much. This is going to be much more interesting than last year.

    Best moment in the coverage was when they showed 3 cows in a pasture, wearing jerseys yellow, green, and spotted.

  • Eric Gunnerson's Compendium

    Socket programming tip: Localhost != local ip address


    I've been writing some socket-based code for an upcoming column, and it worked really nice when I used localhost for the client and server on another machine, but broke when I tried to use the machine name.

    Just something else I didn't know about networks. localhost is not the same thing as the local ip address, but is rather a totally separate address.

    If you want to create a TcpListener, you should use IPAddress.Any as your parameter for the IP address. That will do what you want.

  • Eric Gunnerson's Compendium

    Le Tour 2003 starts


    Le Tour de France 2003 started last weekend. The tour, if you don't know, is a 2000ish-mile, three-week race up and down the mountains in France. It's widely regarded as one of the toughest physical challenges around.

    The best coverage in the US is on OLN.

  • Eric Gunnerson's Compendium

    The Brunching Shuttlecocks site closes


    The offbeat and often insanely funny Brunching Shuttlecocks website has closed. Well, it hasn't actually closed, but they've stopped updating it. The site is still there, with archived content.

    From their wonderful movie reviews, to their offbeat ratings, they've written the kind of comedy I sometimes aspire to write.

    I highly recommend the "The Book of Ratings", which is an assembly of ratings from the site, in portable dead tree format.

  • Eric Gunnerson's Compendium

    Online Slang Dictionary


    The Online Slang Dictionary is a reference everybody needs sometime, especially those of us who find it increasingly hard to appear cool to our 9-year-old daughters.

  • Eric Gunnerson's Compendium

    Regular Expression Workbench V2.0


    I've finished a new version of my Regular Expression Workdbench, and it's now available on gotdotnet. If you use regular expressions on .NET, or you've heard about them but haven't really tried them, this tool can help you a lot. If I do say so myself.

    As an old Perl guy (in both senses of the word "old"), I've spent a fair amount of time writing regular expressions. It's easy to try out a regex in Perl, but not so easy in a compiled language like C#. I wrote the first version of this utility a couple of years ago, and in the first version, all it did was let you type in a regex, a string to run it against, and execute it.

    Over time, it grew. The next version supported some fairly cool features:

    • A menu with all the regex language options, so you don't have to remember what the syntax is for zero-width positive lookaheads.
    • Automatic creation of C# or VB code based on your regex and the options you choose.
    • Interpretation of regexes. Hover over a part of the regex, and the popup will tell you what that part of the regex means. This is very useful if you're trying to learn regex, or you don't remember what "(?<=" means.
    • Support for calling Split()

    This version adds a few more features:

    • A nicer UI. Not a very high bar, given the previous design ("Who designed this UI? Vandals?") (5 points to anybody who knows who wrote that line...). A real menubar, a somewhat-pleasant grouping of controls, etc.
    • Library functionality. Give the regex you wrote a description, and save it away into a library, so you can open it up later, or show it off to your friends. Chicks dig a well-crafted regular expression.
    • Unit tests for the interpretation features. Found 3 or 4 good bugs when writing the unit tests. These tests will get better over time.
    • Support for calling Regex.Replace(). Specify the replacement string, and you'll see exactly what gets replaced.
    • Support for calling Regex.Replace() with a MatchEvaluator. For the cases where you can't do your replacement with a simple substitution string, the Regex class lets you write a delegate. The workbench now allows you to write the function, which it saves away, compiles, loads and then uses to call Replace.

    Comments & suggestions are always welcome.







  • Eric Gunnerson's Compendium

    Getting pictures onto your computer


    If you take a lot of digital pictures (I have 7000 images on my laptop right now, and lots more archived), you spend lots of time copying files across to your computer. Most cameras come with USB cables, which aren't really that useful.

    My preferred solution is a CompactFlash PCCard adapter. You take the compact flash out, plug it into the adapter, and slide the adapter into your laptop. At that point, it looks just like a disk drive, as compactflash cards look like IDE drives. Easily copy the files across and delete the existing ones, without having to use camera power. It's also a bit faster.

    You can also get these for other storage formats.

  • Eric Gunnerson's Compendium

    Review continued - Canon G3


    (review continued)

    These two contraints - what you want to do with the aperture or shutter speed, and what the camera needs to do to give you a good exposure - sometimes come into conflict. If you take a picture of moving water, you often want a slow shutter speed (1/30th to 1 second, depending on the effect), but if it's a sunny day, you'll overexpose at that shutter speed.

    Enter the neutral-density filter. With the the filter, you get a 3-stop (3 factors of 2, or 1/8th) decrease in the amount of light that comes in. That means that instead of an exposure of F16.0 at 1/125th of a second, you can either open the lens up to F2.0, or lengthen the exposure to 1/15th of a second.

    I used this last night on some fireworks, just so the CCD sensor doesn't get overloaded.

    The cool part is that it's just a setting in the recording menu, and everything else works the way you'd expect.

Page 45 of 46 (1,133 items) «4243444546