CodeBetter.Com
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @CodeBetter

Grant Killian's Blog

No, this has nothing to do with beer -- but maybe it should?

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!



Comments

JosephCooney said:

Regarding your 3rd point - why did they choose the word "shared" for VB.NET instead of "static" - static is already a reserved word in VB.NET and means something completely different.

See:
http://weblogs.asp.net/psteele/articles/7717.aspx
# March 2, 2004 10:01 AM

Grant said:

Both good points -- I know "static" is used in VB for mainting values between method calls (C# has no equivalent); the post by Patrick Steele is a good one!
# March 2, 2004 10:13 AM

Enjoy Every Sandwich said:

You've been Taken Out. Thanks for the good post.
# March 2, 2004 11:44 AM

Richard Tallent said:

Things I like about this:

- The calling code doesn't have to know it is a static member. If it only calls it through instances, I can change it in the future to a non-shared member in the future.

- The "assumed Me" semantics work correctly for an instance of an object calling its own static member--no need to prefix the call with its own type.

The things I *don't* like:
- the inability to override a static member in a subclass.
- the inability to flag a static member as the Default accessor (useful for shortcutting access to a singleton collection, for instance).
# March 2, 2004 6:48 PM

Frans Bouma said:

"- The calling code doesn't have to know it is a static member. If it only calls it through instances, I can change it in the future to a non-shared member in the future."
This is not a 'good thing'. Static methods imply that they should be used with care because of threading issues. Only if the method itself is thread-safe should you use static methods in a multi-threading environment (like a webapp). Not knowing if a method is threadsave is opening up your application for misery which is hard to track down.

I've seen this behaviour a couple of times when VB.NET developers stored their DataAccess code in VB.NET modules and wondered why things went wrong in a multi-threaded environment (asp.net app).
# March 2, 2004 8:38 PM

grant said:

Frans, regarding the multi-threading issue and Static methods, is there something inherently thread UNsafe? If you aren't using instance variables, my understanding was you should be OK.

This article speaks to the topic (sort of): <a href="http://authors.aspalliance.com/stevesmith/articles/staticthreads.asp">http://authors.aspalliance.com/stevesmith/articles/staticthreads.asp</a> ?

# March 3, 2004 1:39 AM

Greg Robinson said:

The word shared was chosen because the shared members of a class are actually shared across every object created from the class. I would argue Shared is a more menaingful word then Static in this context.

The VB.NET recommends you use a shared member just like you do in C#, Classname.MemberName. To me the convenience of a Shared memeber is not having to create an instance of an object. I use them for Utility classes all the time.
# March 30, 2004 1:04 AM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Check out Devlicio.us!