Why does VB.Net not support pointers?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Jenil
    New Member
    • Aug 2014
    • 1

    #1

    Why does VB.Net not support pointers?

    VB.NET does not support pointers: Why?
    Last edited by Frinavale; Sep 16 '14, 05:47 PM.
  • Luuk
    Recognized Expert Top Contributor
    • Mar 2012
    • 1043

    #2
    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?
    Last edited by Frinavale; Sep 16 '14, 05:02 PM. Reason: Removed unhelpful links

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      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:
      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:
      Code:
      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
      Code:
      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 changePersonNam e 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
      Last edited by Frinavale; Sep 16 '14, 05:55 PM.

      Comment

      Working...