VB.net Shared vs C # Static differences in accessibility, why?

This might be a dumb question, but why are generic methods available for types and instances in VB.net. Am I mistaken in assuming they are equivalent to C # static methods?

i.e

MyClass.MySharedMethod() dim mc as new MyClass() mc.MySharedMethod() 

Why can I do this? What is the possible advantage of this - all I see is that it confuses people when using intellisense. I'm sure this is due to some kind of agreement with the classic VB6 on why bothering to bring this to .NET - it just seems so broken to me.

+6
c #
source share
2 answers

Yes, it's basically a hangover from VB6. Java also allows you to do this, but most IDEs warn you these days.

And yes, this is a really bad idea. The most obvious example of its bad is Thread.Sleep:

 Dim t as new Thread(...) t.Sleep(1000) 

Which stream slept? Current. Doh!

+17
source share

This might be a dumb question, but why are generic methods available for types and instances in VB.net. Am I mistaken in assuming they are equivalent to C # static methods?

They are the same. VB will warn you if you try to use them on instances, but this is not prohibited. This is likely due to dynamic typing and late binding. Imagine you have an object with late binding ( Object ), not knowing its exact type. How could you call the static method of your class?

As far as I know, this would be impossible in C # (without resorting to reflection). In VB with Option Strict Off you can simply name it as an object instance method.

+2
source share

All Articles