VB.NET is a bit weird when it comes to pointers because you wouldn't think that it makes use of pointers; however, pretty much everything in VB.NET is a pointer!
To quickly answer your question:
VB.NET does not support pointers: Why?
VB.NET
does support pointers! In fact it almost
exclusively uses pointers. It appears as if VB.NET doesn't support pointers because it is so transparent but it is very important to understand what VB.NET is doing for you! Otherwise you will be confused when you pass something
ByVal
and it is changed in the calling code unexpectedly.
Here's more of an explanation of what I'm talking about...
Let's say I have a class
Person
defined as such:
-
Public Class Person
-
Private _name As String
-
-
Public ReadOnly Property Name As String
-
Get
-
return _name
-
End Get
-
Set(ByVal value As String)
-
_name = value
-
End Set
-
End Property
-
-
Public Sub New(ByVal n As String)
-
_name = n
-
End Sub
-
-
End Class
-
And I create an instance to that person in my code as such:
-
Public Function Main(ByVal ParamArray Args() As String) As Integer
-
Dim p As New Person("Frinavale")
-
-
Return 0
-
End Function
-
The variable
p
is a pointer to a memory location that is allocated to hold the information about the person.
If I pass this instance to a this
ChangeName
method
-
Public Function Main(ByVal ParamArray Args() As String) As Integer
-
Dim p As New Person("Frinavale")
-
-
changePersonName(p, "Jenil")
-
-
Return 0
-
End Function
-
-
Private Sub changePersonName(ByRef personInChaneNameMethod As Person, ByVal newName as String)
-
personInChaneNameMethod.Name = newName
-
End Sub
-
The name for the variable
p
in the
Main
method will be changed to the new name.
But what is VERY important to know is that if we change the
ByRef
in the
changePersonName
method to be
ByVal
the same thing happens!!!
Why?
Because VB.NET only passes around pointers for anything that inherits from
Object
(which is pretty much everything).
If you pass a non-Object-inheriting item "ByVal" to the method, then the item in the calling code will not be changed (whereas if you pass this type "ByRef" it will be changed).
But if you pass an Object-inheriting item "ByVal" into a method it is still a pointer...so the item in the calling code Will be changed (in this case "ByRef" acts the same way).
To use pointers in VB.NET all you have to do is declare instances of
Objects
and use these variables
-Frinny