Results 1 to 4 of 4

Thread: [RESOLVED] [2008] Classes vs Structures

  1. #1

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Resolved [RESOLVED] [2008] Classes vs Structures

    Hi Guys,

    This question spawned from an example jmc gave to someone on how to use a structure to implement IComparable... now I always thought that structures could only be used to store basic things like this:
    vb.net Code:
    1. Private Structure PersonStruct
    2.   Dim FirstName As String
    3.   Dim Surname As String
    4.   Dim HouseNumber As Integer
    5. End Structure

    but it turns out you can define functions, subs, delegates and even events, all from within a structure. So that made me think... whats the real differences between a Structure and a Class? JMC highlighted one difference and that was not being able to create a new instance of a structure and pass in arguments to be initialized but apart from the lack of constructors is there really much difference? I assume there must be as Classes seem to get used a hell of a lot more than structures but I dont really understand why there is a need for both of them if they are so similar... I mean if classes have a few more features then why ever use a structure? Are they more 'lightweight' or something?

    Thanks
    Chris
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


  2. #2

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Classes vs Structures

    Actually, after typing that post I just tested creating a constructor (at least I think thats the right term for the public "New" sub) in a structure and it seems to work fine... so is there any difference between structures and classes??

    Example:
    vb Code:
    1. Public Structure myStruct
    2.         Private _something As String
    3.  
    4.         Public ReadOnly Property Something()
    5.             Get
    6.                 Return _something
    7.             End Get
    8.         End Property
    9.  
    10.         Public Sub New(ByVal test As String)
    11.             _something = test
    12.         End Sub
    13. End Structure
    Usage:
    vb Code:
    1. Dim newthing As New myStruct("this is a test")
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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

    Re: [2008] Classes vs Structures

    Quote Originally Posted by chris128
    JMC highlighted one difference and that was not being able to create a new instance of a structure and pass in arguments to be initialized
    That's not actually what I said. I said that you cannot initialise fields where they are declared and I also said that you cannot add a parameterless constructor. You certainly can add constructors with parameters though. For instance, this is legal:
    vb.net Code:
    1. Public Class SomeType
    2.  
    3.     Public int As Integer = 100
    4.     Public str As String = "Hello World"
    5.  
    6. End Class
    as is this:
    vb.net Code:
    1. Public Class SomeType
    2.  
    3.     Public int As Integer
    4.     Public str As String
    5.  
    6.     Public Sub New()
    7.         Me.int = 100
    8.         Me.str = "Hello World"
    9.     End Sub
    10.  
    11. End Class
    but if you make SomeType a structure then neither would be valid.

    All this and more of the difference between structures and classes is explained in the MSDN Library. You did look there first, right?

    http://social.msdn.microsoft.com/Sea...ses+structures
    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

  4. #4

    Thread Starter
    Pro Grammar chris128's Avatar
    Join Date
    Jun 2007
    Location
    England
    Posts
    7,604

    Re: [2008] Classes vs Structures

    I didnt know where to look on the MSDN site for something like that, I mean I know where to find function definitions and examples etc but I didnt think they would have just a plain description of the difference between the two (embarrassed face icon would go here if I wasn't too lazy to always use the Quick Reply window).

    I also found this article which explains it nice and clearly... its 1:30 in the morning here so thats my excuse for not being sensible and having a good search on the web first! http://www.startvbdotnet.com/oop/structure.aspx

    Thanks
    My free .NET Windows API library (Version 2.2 Released 12/06/2011)

    Blog: cjwdev.wordpress.com
    Web: www.cjwdev.co.uk


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