|
-
Jan 15th, 2009, 08:13 PM
#1
[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:
Private Structure PersonStruct
Dim FirstName As String
Dim Surname As String
Dim HouseNumber As Integer
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
-
Jan 15th, 2009, 08:17 PM
#2
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:
Public Structure myStruct
Private _something As String
Public ReadOnly Property Something()
Get
Return _something
End Get
End Property
Public Sub New(ByVal test As String)
_something = test
End Sub
End Structure
Usage:
vb Code:
Dim newthing As New myStruct("this is a test")
-
Jan 15th, 2009, 08:22 PM
#3
Re: [2008] Classes vs Structures
 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:
Public Class SomeType
Public int As Integer = 100
Public str As String = "Hello World"
End Class
as is this:
vb.net Code:
Public Class SomeType
Public int As Integer
Public str As String
Public Sub New()
Me.int = 100
Me.str = "Hello World"
End Sub
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
-
Jan 15th, 2009, 08:36 PM
#4
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
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
|