In Visual Basic, when you create a class reference using the
Dim
statement with the
New
keyword, it is equivalent to a handle to a managed object, similar to C++ handles. The reference can be relocated in memory due to garbage collection and other memory management processes.
Your
myclassallocation
is a variable that holds an instance of your
MyClass
class. The
New
keyword indicates that you want to create a new instance of the class. In this case, memory is allocated for the object, and the reference is stored in the
myclassallocation
variable.
Your
myclassreference
is declared as a reference to an instance of your
MyClass
class. It does not hold an actual instance, it's just a reference that can be assigned to point to an instance later on.
Both
myclassallocation
and
myclassreference
would behave the same. They are references to managed objects, and the garbage collector will take care of relocating them as needed.
C++ pointers are closer to raw memory addresses, and they can be used to point to both managed and unmanaged memory, but also require manual memory management and are not automatically relocated like managed references in VB.
As a small example, you can use:
Public Class MyClass
Public Sub New()
Console.WriteLine("MyClass instance was created.")
End Sub
End Class
Module MainModule
Sub Main()
Dim myclassallocation As New MyClass
Dim myclassreference As MyClass
myclassreference = myclassallocation
myclassreference = Nothing
GC.Collect()
Console.ReadLine()
End Sub
End Module