Class And Structure.
Dear Developers
I would like to share something which i found while performing one of the development task. Working difference between Class Objects and Structure Objects. If you see them they both are same in low level of the working. Classes has much more feature compared to Structures.The choose come when you have limit property and basis feature needed.
Lets see the working
Public Class Person
Dim strName As String
Public Property Name() As String
Get
Return strName
End Get
Set(ByVal value As String)
strName = value
End Set
End Property
End Class
Get
Return strName
End Get
Set(ByVal value As String)
strName = value
End Set
End Property
End Class
Private Structure StrPerson
Dim Name As String
End Structure
Dim Name As String
End Structure
Private Sub btnClass_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClass.Click
Dim clsObjPerson As New Person
clsObjPerson.Name = "Switin Class"
clsObjPerson.Name = "Switin Class"
Dim clsCopyObjPerson As New Person ' Create New Object of Same Class
clsCopyObjPerson = clsObjPerson 'Copy Class object to new variable
'Check Value in Class. Values are Copied form Source Class Object {clsObjPerson} To
{clsCopyObjPerson}
MessageBox.Show(clsCopyObjPerson.Name) '
clsObjPerson.Name = "Switin Class Changed" ' Change Original {Source} Class Value.
MessageBox.Show("Value from Copied Class :- " & clsCopyObjPerson.Name)
'Change has reflected to Copied Class Object. Reason they are pointing to same memory
' location
End Sub
clsCopyObjPerson = clsObjPerson 'Copy Class object to new variable
'Check Value in Class. Values are Copied form Source Class Object {clsObjPerson} To
{clsCopyObjPerson}
MessageBox.Show(clsCopyObjPerson.Name) '
clsObjPerson.Name = "Switin Class Changed" ' Change Original {Source} Class Value.
MessageBox.Show("Value from Copied Class :- " & clsCopyObjPerson.Name)
'Change has reflected to Copied Class Object. Reason they are pointing to same memory
' location
End Sub
Private Sub btnStructure _Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStructure .Click
'Now Check Same with Structures
Dim strObjPerson As New StrPerson
strObjPerson.Name = "Switin Structure"
Dim strObjPerson As New StrPerson
strObjPerson.Name = "Switin Structure"
Dim strCopyObjPerson As New StrPerson
strCopyObjPerson = strObjPerson
MessageBox.Show(strCopyObjPerson.Name)
strObjPerson.Name = "Switin Structure Changed"
'Original Value is changed. This will not effect in copied Structure
MessageBox.Show("Value from Copied Structure :- " & strCopyObjPerson.Name)
'Value is not Changed. Different working from Class. Structure only Copies the data and Not
' Memory location.
End Sub
strCopyObjPerson = strObjPerson
MessageBox.Show(strCopyObjPerson.Name)
strObjPerson.Name = "Switin Structure Changed"
'Original Value is changed. This will not effect in copied Structure
MessageBox.Show("Value from Copied Structure :- " & strCopyObjPerson.Name)
'Value is not Changed. Different working from Class. Structure only Copies the data and Not
' Memory location.
End Sub
I have created one Class which is copied to other object of same class. Value changed in first Object of Class has effect to copied Object. This is because they share same memory location as they are reference types.
In case of Structures change done in object will not effect other object as Structures are Value types. It will copy data and not the reference. Change in one object will not effect other.
This means Structures has to be faster compare to class but limit features.
Regards
Switin Kotian
Learning skills is good, But Understanding it's working is more important. There it makes difference
Comments
Post a Comment