|
-
Mar 5th, 2010, 10:34 AM
#1
Thread Starter
Fanatic Member
Structure vs Class
Hi all,
Pursuing the Microsoft Certified Professional Developer Certification and working through the training kit. I've programmed for a while and mainly used classes as my objects.
The training certification book I'm working through has a section on structures. they look very similiar to classes so my question is: why use them? Do they offer something that creating a class doesn't? What's the difference between structures and classes?
Thanks,
Strick
-
Mar 5th, 2010, 10:43 AM
#2
Re: Structure vs Class
The main difference between structures and classes is that structures are value type while classes are reference type.
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Mar 5th, 2010, 10:45 AM
#3
Re: Structure vs Class
A structure is a value type, whereas a class is a reference type. This leads to all kinds of fun problems. For instance:
Code:
Public Structure Foo
Public myVal as Integer
End Structure
Dim ar(1) As Foo
ar(0) = new Foo
ar(0).myVal = 2 'Doesn't compile.
The problem in that last line is that ar(0) returns a copy of the Foo object at that array slot. If Foo was a class, ar(0) would return the address of the object rather than a copy. Since you get a copy, and are changing the value of the copy, then discarding it, the compiler flags it as an error, which it is (though technically it would work, and do nothing). Therefore, you have to do something like this:
Code:
Dim tF as Foo = ar(0)
tF.myValue = 2
ar(0) = tF
So what's the advantage of structures? Well, they are value types, and there are some advantages to that, but there aren't all that many, and I know of none that are particularly significant, so I rarely use them.
My usual boring signature: Nothing
 
-
Mar 5th, 2010, 11:17 AM
#4
Thread Starter
Fanatic Member
Re: Structure vs Class
Ok thanks guys.
Ima stick to classes in my programs..lol. But i'll just have to make sure I remember those things for the exam.
Thanks,
Strick
-
Mar 5th, 2010, 11:42 AM
#5
Re: Structure vs Class
I should also mention these differences because they are important too:
Code:
Structures Classes
Sub New() with no parameters Not allowed Allowed
Sized array declaration Not allowed Allowed
Default access modifier Public Private
Let us have faith that right makes might, and in that faith, let us, to the end, dare to do our duty as we understand it.
- Abraham Lincoln -
-
Mar 5th, 2010, 12:10 PM
#6
Re: Structure vs Class
stack versus heap memory allocation, which directly related to value type (memory is allocated on stack) versus reference type (memory is allocated on the managed heap)
-
Mar 5th, 2010, 02:26 PM
#7
Re: Structure vs Class
I can give an example where I have recently found a Structure to be more useful than a class. I was adding 3D points to a list and wanted to avoid duplicates. I declared the Structure and the List as follows:
vb.net Code:
Private Structure Point3D
Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer)
Me.X = x
Me.Y = y
Me.Z = z
End Sub
Public Z As Integer
Public X As Integer
Public Y As Integer
End Structure
Private CheckList As New List(Of Point3D)
Then I could easily check if a 3D point was already on the list thus:
vb.net Code:
Dim np3 As New Point3D(10, 10, 100)
'.......
If Not CheckList.Contains(np3) Then CheckList.Add(np3)
This only worked with Point3D defined as a Structure, not as a Class. It seems that Lists (and perhaps other generic collections) of a value type can compare their items for equality. There must be alternative ways of doing this but it was convenient in this case. I suppose Points and Rectangles are defined as Structures for the same reason.
BB
-
Mar 5th, 2010, 04:05 PM
#8
Re: Structure vs Class
It is that when using a class you are using a reference type, so comparing equality on a reference type is going to try to compare if they are in fact the same reference.
I would guess points and rectangles are defined as structures simply because value types can be allocated and worked with quicker since they reside on the stack, and generally they involve a lot of math and mutating of values.
-
Mar 5th, 2010, 07:26 PM
#9
Re: Structure vs Class
 Originally Posted by stricknyn
Ima stick to classes in my programs..lol.
That's not really an appropriate choice. Structures exist for a reason and you should use them whenever it's appropriate to do so. All VB native types other than String are structures, e.g. Date, Integer, Boolean. All enumerations are based on integral types, so they are all structures too. Size, Point and Rectangle are all structures. There are lots of examples of structures throughout the .NET Framework and you should use them too, wherever it's appropriate to do so.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|