value types + reference types
reference types:
variable store a reference to an object
object is created using New
variable/reference is stored on Stack, object is stored on Heap
Dim e1 As New Employee("Scott",True)
Dim e2 As Employee=e1
e2.Name="Rob"
Dim e3 As Employee = Nothing
Console.WriteLine(e1.GetDescription())'Rob
Console.WriteLine(e2.GetDescription())'Rob
Console.WriteLine(e3.GetDescription()) '?
p.s.
string type is a reference type, but the behaves like a value type
pass value type by value: value type pass a copy of value
pass reference type by value: reference type pass a copy of reference
pass value type by reference
pass reference type by reference 'fairly uncommon
Sub Main()
Dim y As Integer = 5
Foo(y)
Console.WriteLine(y) '5
Dim emp As New Employee("Rob", False)
Bar(emp)
Console.WriteLine(emp.Name) 'Scott 'Rob
Dim y As Integer = 5
Foo2(y)
Console.WriteLine(y) '10
Dim emp As New Employee("Rob", False)
Bar2(emp)
Console.WriteLine(emp.Name) 'Scott 'Kate
End Sub
Sub Foo(ByVal x As Integer)
x += 5
End Sub
Sub Bar(emp As Employee)
'emp.Name = "Scott" 'Scott
emp = New Employee("Scott", True) 'Rob
End Sub
Sub Foo2(ByRef x As Integer)
x += 5
End Sub
Sub Bar2(ByRef emp As Employee)
emp.Name = "Scott" 'Scott
emp = New Employee("Kate", True) 'Kate
End Sub