Connecting Tech Pros Worldwide Forums | Help | Site Map

Class vs Structure Vs Module in Visual Basic .Net

Member
 
Join Date: Apr 2008
Location: Illinois
Posts: 92
#1: Nov 24 '08
I was quickly curious what the difference between a class, a structure, and a module is in Visual Basic .Net in relation to C++.

Is a Structure truly a struct like in C, or is it essentially a class with all members defaultly public? And what is the point of a Module (I used one back in the day with VB6, but have read they have major speed issues--and why are they inefficient?).

How do these three fit in with each other in relation to Polymorphism? Can I inherit a Module from a Class or vs versa?

I've found it's quite hard to find documentation in MSDN on things like this, and googling hasn't gotten me far. And I hate to start up a new thread on such a fundamental/easy question...

I am basically interested in knowing features one may have over another. And if anyone knows of how to do a Union like in C/C++, I would be greatly appreciative to know how.

Hopefully Helpful,

TamusJRoyce
balabaster's Avatar
Moderator
 
Join Date: Mar 2007
Location: Canada
Posts: 757
#2: Nov 24 '08

re: Class vs Structure Vs Module in Visual Basic .Net


A module is the same as a Public Static Class in C# (Public NotInheritable Class in VB). Cosmetically you don't have to specify the class name to reference its methods like you do with a Public Static Class. In a module, members by default are public and as such a variable can be seen across the global scope of your application. In a class you normally have to reference variables and other members by the fully qualified path name, i.e. instance.member, however, if you use imports MyNamespace.MyModule in VB or using MyNamespace.MyClass in C# then you can access the member directly just as if it were in a module... as long as the member is declared as shared in VB or static within C#.

If you do some digging into the underlying IL, you will see that both modules and static classes are implemented in the same way, so really - modules and static classes are the same, any differences are purely cosmetic.

Of course, there's always some caveats: The keyword Module can only be used directly within a namespace, you cannot nest modules... however, you can nest static/notinheritable classes. So while under the covers they're the same, syntactically, one is not allowed.

A module cannot inherit or implement anything... it can contain subclasses


At first glance a structure is similar to a class in many ways, one noticeable way that it's different is that it's passed ByVal instead of ByRef between methods - unless you explicitly state that it must be passed ByRef. Structs perform better due to heap/stack management differences because structs are stored on the stack whereas classes are stored on a heap. When a struct stores small amounts of data, it is stored in memory more efficiently than a class.
Reply

Tags
class, module, struct, visual basic, visual basic .net