By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,189 Members | 1,028 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,189 IT Pros & Developers. It's quick & easy.

Why does VB.Net not support pointers?

P: 1
VB.NET does not support pointers: Why?
Aug 24 '14 #1
Share this Question
Share on Google+
2 Replies


Expert 100+
P: 1,035
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?
Aug 24 '14 #2

Frinavale
Expert Mod 5K+
P: 9,731
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:
Expand|Select|Wrap|Line Numbers
  1. Public Class Person
  2.   Private _name As String
  3.  
  4.   Public ReadOnly Property Name As String
  5.    Get 
  6.      return _name
  7.    End Get
  8.    Set(ByVal value As String)
  9.      _name = value
  10.    End Set
  11.   End Property
  12.  
  13.   Public Sub New(ByVal n As String)
  14.     _name = n
  15.   End Sub
  16.  
  17. End Class
  18.  

And I create an instance to that person in my code as such:
Expand|Select|Wrap|Line Numbers
  1. Public Function Main(ByVal ParamArray Args() As String) As Integer
  2.   Dim p As New Person("Frinavale")
  3.  
  4.   Return 0
  5. End Function
  6.  
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
Expand|Select|Wrap|Line Numbers
  1. Public Function Main(ByVal ParamArray Args() As String) As Integer
  2.   Dim p As New Person("Frinavale")
  3.  
  4.   changePersonName(p, "Jenil")
  5.  
  6.   Return 0
  7. End Function
  8.  
  9. Private Sub changePersonName(ByRef personInChaneNameMethod As Person, ByVal newName as String)
  10.   personInChaneNameMethod.Name = newName
  11. End Sub
  12.  
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
Sep 16 '14 #3

Post your reply

Sign in to post your reply or Sign up for a free account.