Class Vs Structure in ByValue and ByRef : Structure « Class Module « VB.Net Tutorial






Imports System

Public Enum Country
  US = 1
  CA = 2
End Enum


Public Class AClass
  Public Name As String
  Public Status As Country
End Class

Public Structure AStruct
  Public Name As String
  Public Status As Country
End Structure

Public Module Test
  Public Sub ChangeName_Obj_ByVal(ByVal details As AClass,ByVal NewName As String)
    details.Name = NewName
  End Sub

  Public Sub ChangeName_Obj_ByRef(ByRef details As AClass,ByVal NewName As String)
    details.Name = NewName
  End Sub

  Public Sub ChangeName_Struct_ByVal(ByVal details As AStruct,ByVal NewName As String)
    details.Name = NewName
  End Sub

  Public Sub ChangeName_Struct_ByRef(ByRef details As AStruct,ByVal NewName As String)
    details.Name = NewName
  End Sub

  Sub Main()

    Dim classInstance As AClass = New AClass()
    Dim structInstance As AStruct = New AStruct()
    classInstance.Name = "A"
    classInstance.Status = Country.CA
    structInstance.Name = "B"
    structInstance.Status = Country.CA

    Console.WriteLine("{0}, {1}", classInstance.Name, classInstance.Status)
    Console.WriteLine("{0}, {1}", structInstance.Name, structInstance.Status)
    Console.WriteLine()

    ChangeName_Obj_ByVal(classInstance, "AAAA")
    ChangeName_Struct_ByVal(structInstance, "BBBB")
   
    Console.WriteLine("{0},{1}", classInstance.Name, classInstance.Status)
    Console.WriteLine("{0},{1}", structInstance.Name, structInstance.Status)
    Console.WriteLine()

    ChangeName_Obj_ByRef(classInstance, "AAAA")
    ChangeName_Struct_ByRef(structInstance, "BBBB")
    Console.WriteLine("{0}, {1}", classInstance.Name, classInstance.Status)
    Console.WriteLine("{0}, {1}", structInstance.Name, structInstance.Status)
  End Sub
End Module
A, CA
B, CA

AAAA,CA
B,CA

AAAA, CA
BBBB, CA








6.46.Structure
6.46.1.Structure
6.46.2.Define and use Structure
6.46.3.Define and use structure with modifier
6.46.4.Define Structure
6.46.5.By value or by reference
6.46.6.Use Structure as Function parameter
6.46.7.Use With and Structure
6.46.8.Class Vs Structure in ByValue and ByRef
6.46.9.structure implements interface