Question Shared vs instance members

GendoIkari

Member
Joined
Mar 20, 2009
Messages
20
Programming Experience
1-3
It seems that in VB.net, all my instance members are accessible as though they were shared members, as long as my class inherits Windows.Forms.Form.

For example:

Public Class MyNonFormClass
Public Sub ShowTest1()
MessageBox.Show("test")
End Sub
End Class

Public Class MyFormClass
Inherits System.Windows.Forms.Form
Public Sub ShowTest2()
MessageBox.Show("test")
End Sub
End Class

Public Class MyCallingClass
Public Sub RunTests()
MyNonFormClass.ShowTest1()
MyFormClass.ShowTest2()
End Sub
End Class

As expected, MyNonFormClass.ShowTest1() throws a compiler error, because ShowTest1 is not a shared member. But The second line, MyFormClass.ShowTest2(), does not throw an error, even in runtime.

Is this correct behavior? Why would inheriting from Windows.Forms.Form cause instance members to be accessible as shared members? Also, this does not happen in C#... the exact same code in C# works as expected.

Thanks!
 
Last edited:
Forms have default instances, which can make things easier when you don't need multiple instances.
 
Said another way, VB has long had (imho a stupidity) whereby some objects can are instantiated by default, with the exact same name, case, etc as their type. It blurs the distinction between static and instance (imho not a good thing) and though C# can also create variables where the name is the same as the type (except structs) by default C# case sensitive policy couple with naming conventions dictating TypesLikeThis and variablesLikeThis keep them separate

It drove me nuts with VB at the start, when every MyDataSet i dropped on a form was instanced with the name MyDataSet. I adopt the net convention of _memberVariable for all my own instanced stuff on forms now.. But it's not possible to change the default behaviour of making a Form1 called Form1 (unless you turn off the application framework?)
 
Back
Top