Results 1 to 9 of 9

Thread: Structure vs Class

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    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

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,109

    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

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Mar 2004
    Posts
    541

    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

  5. #5
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    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 -

  6. #6
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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)

  7. #7
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    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:
    1. Private Structure Point3D
    2.         Public Sub New(ByVal x As Integer, ByVal y As Integer, ByVal z As Integer)
    3.             Me.X = x
    4.             Me.Y = y
    5.             Me.Z = z
    6.         End Sub
    7.         Public Z As Integer
    8.         Public X As Integer
    9.         Public Y As Integer
    10.     End Structure
    11.  
    12. 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:
    1. Dim np3 As New Point3D(10, 10, 100)
    2. '.......
    3. 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

  8. #8
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    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.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Structure vs Class

    Quote Originally Posted by stricknyn View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width