VB.NET does not support pointers: Why?
Why does VB.Net not support pointers?
Collapse
X
-
A simple search finds a lot of pages on this topic, this is the first one:
How to do pointers in Visual Basic
and the second one:
Vb.net Pointers
Could you please give more details about your question after doing some research yourself? -
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 somethingByValand 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 classPersondefined as such:
Code: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:
The variableCode:Public Function Main(ByVal ParamArray Args() As String) As Integer Dim p As New Person("Frinavale") Return 0 End Functionpis a pointer to a memory location that is allocated to hold the information about the person.
If I pass this instance to a thisChangeNamemethod
The name for the variableCode: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 Subpin theMainmethod will be changed to the new name.
But what is VERY important to know is that if we change theByRefin thechangePersonNam emethod to beByValthe same thing happens!!!
Why?
Because VB.NET only passes around pointers for anything that inherits fromObject(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 ofObjectsand use these variables
-FrinnyLast edited by Frinavale; Sep 16 '14, 05:55 PM.Comment
Comment