Default Instances

Fresh off of writer’s block, I thought I’d dive straight back into the sea of controversy and talk about a feature called “default instances.” Default instances are a “new” feature in VB 2005 that is really the return of a very old feature, one that’s been around for a long time. Now, the return of default instances has stirred some very passionate debate, but what I’m going to do is address this question in three separate entries. In this entry, I’m going to describe what default instances are at a technical level. In my next entry, I’m going to talk about what I see as the positive case for default instances. Then, in the final entry, I will talk about some limitations of default instances and address the controversy about their reintroduction more directly. You can choose whether or not you want to wait for the last entry before you start throwing brickbats…

So, what are default instances? Well, they’re exactly what their name suggests: a way to provide a default instance of a class that you can use without having to New the class. If you’ve used versions of VB prior to VB 2002 (i.e. pre-.NET), you’ll probably have come across the most common default instance, the form default instance. For example, in VB 6.0, if you created a form named Test, you could say in code:

Test.Show()

And voila! an instance of the form would just magically appear. What was going on behind the scenes was a bit of magic. When you referred to “Test,” the VB runtime would go check and see if it already had a default instance of Test lying around. If it did, then it would hand back that default instance when you called Show(). If there wasn’t a default instance lying around, the runtime would create a new instance of Test and use that as the default instance going forward.

Default instances disappeared in VB 2002 and VB 2003, but are making a comeback in VB 2005. The high-level “why,” I’ll leave to my second entry, so let’s talk about the “how” for the moment. Hopefully, all of you are familiar with the introduction of the My namespace in VB 2005. (If not, go read this MSDN article and then come back.) One part of the My namespace is the Forms object. If you bring up a VB 2005 project with a Form named Test in it, you’ll notice that the My.Forms object has a property called Test. This property returns a default instance of Test. So you can say, sort of like you did in VB 6.0, “My.Forms.Test.Show()” and voila! up pops a new instance of Test. What happens behind the scenes is that the Test property of My.Forms checks to see if it’s already returned an instance of Test and, if it has, it just returns that instance. If it hasn’t returned an instance yet, it just creates a new instance. Pretty simple.

We also extended default instances to work in exactly the same way that VB 6.0 default instances did. Besides saying My.Forms.Test.Show(), you can also just say Test.Show() and it will also access the default instance. Essentially, Test.Show() is a shortcut to saying My.Forms.Test.Show().

There are, however, a couple of differences between the way default instances are implemented in VB6 and the way they are implemented in VB 2005:

  • In VB6, if you tried to use the Is operator to test the default instance to see if it was Nothing, the expression would always evaluate to False. This is because referring to the default instance caused it to be created if it didn’t exist! In VB 2005, we recognize when you are testing the default instance using the Is (or IsNot) operator and won’t auto-create the instance in that case. So you can see if the default instance exists or not.
  • A common source of default instance errors was using the default instance instead of Me within the default instance’s type. For example, if Form1’s load method has the statement “Form1.BackColor = Color.Red”, this will only change the background color of the deafult instance’s form. If you create a new instance of Form1 that is not the default instance, it’s background color will not be red! To assist users in this case, we give an error when you refer to the default instance inside of the type, telling you to use Me instead.

Well, that’s about it for the technical side of things. Now onto the good, the bad and the ugly…

23 thoughts on “Default Instances

  1. karl

    I’m not familiar with default instances, but the idea is that you can simply use the classname to have an instance created, ala: <CLASSName>.<Property/Function> ? (I want to say similar syntax as accessing shared members, but I realize it’s a totally different thing) ]]>

    Reply
  2. Ninputer

    It introduced a little confusion on the concept of class, so there will be somebody doesn’t love it. You can made it optional in the Compiler Options. I don’t mean to turn of My.Forms generating. Just prevent user to access them from the class name. Only open it when upgrades from VB6 programs or when a programmer really knows what it means and want to open it.

    Reply
  3. Merak

    I’ll leave my comments for those, however I do have a couple of requests:
    Can you please address "Why" it was reintroduced?
    Was it truly a popularly requested feature, or was it just simply easy to implement with the My namespace (ie an intern’s project)?
    Can you also please address how you have learnt from the problems with the VB6 implementation, and how the new version of this feature addresses them?
    (Many of these are discussed in the comments posted to the ladybug site, but mainly boil down to the "accidental" semi-Singleton nature of them..memory of the private vars of previous incarnations, showing a 2nd instance, just to see an already visible one just come to the foreground, etc..behaviour which is sometimes desired, but usually is’nt)
    Thanks]]>

    Reply
  4. Merak

    I doubt it will help if they do.
    It would be like saying it’s optional to light a match in a gas filled room…you only have to use the option once to introduce the problem to the project. Afterwards turning the option off will either be impossible, or will produce so many compile-time errors that it would be unfeasible to do so.
    However, if it was introduced with an option, I’d like to see the ability to disable the option completely via enterprise policy/templates (so it can’t be "accidentally" turned on)]]>

    Reply
  5. Len Weaver

    I’m curious… What will happen if one tries to use this feature with a form that lacks a parameterless constructor?

    Reply
  6. Andy Maggs

    IMO this feature is only being introduced to enable people to upgrade VB6 apps more easily, and has no other place in .NET. Rather than trying to influence the marketiing folks at MS with arguments about programming purity (a subject for which I imagine they could care less), use C# instead which so far at least has no need for such nonsense.

    Reply
  7. Pingback: notgartner.com: Mitch Denny's Bl

  8. Pingback: Daniel Moth

  9. Pingback: Anonymous

  10. Pingback: Anonymous

  11. Pingback: Brad McCabe's WebLog

  12. Pingback: d.code

  13. Pingback: d.code

  14. Pingback: Dennes

  15. Pingback: Anonymous

Leave a Reply