This blog has moved!

Check out www.CodeBetter.com/blogs/grant.killian

<December 2008>
SuMoTuWeThFrSa
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910


Navigation

Professional Props...

Extracurricular Props...

Subscriptions

Article Categories



Some VB Static

After a bit of time away, I'm teaching part-time again.  We were covering static members in VB and it ocurred to me (and the class) that static methods in VB exhibit different behaviour than in C#.  I do a lot of my programming with C#, and I was explaining how Shared (the VB equivalent to the C# static keyword) methods are available through instances of the class instead of just the type itself.  So, if I make a DeckOfCards object with a Shared (static) method named Sort(), the following is valid VB.Net:

dim objInstance as DeckOfCards = new DeckOfCards()
objInstance.Sort( args )

In C#, the only way to call the static Sort method is through the type:

DeckOfCards.Sort( args );

Four things strike me:

  1. This is a fairly trivial thing . . .
  2. . . . Except that, to be technically correct, it means you have to caveat your explanation of Static methods based on each language.
  3. This is possibly (although I'm not sure) why VB.Net uses the keyword Shared in place of “static“.  Shared is not completely identical to the C# static.
  4. I like the c# way better; it's cleaner.  For example, the next question a student will ask is “can I reference the current instance though the Shared member?“  The rationale for static/shared methods comes into question . . .

I can't think of a “good” reason for VB.Net exhibiting this behaviour, but I'm not on the VB.Net language team so who knows; it's not the first strange difference I've seen (see my post here for example) and it won't be my last.  Anyway, I thought I should document it here for posterity.

Happy .Netting!

posted on Tuesday, March 02, 2004 1:20 PM by grant.killian





Powered by Dot Net Junkies, by Telligent Systems