Variable Pointers

jadaoradix

New member
Joined
Jun 19, 2011
Messages
2
Programming Experience
5-10
I would like to do something like this:

VB.NET:
Dim GenericData as String = "Hello, World"
Dim FirstName as String = "Daniel"
Dim LastName as String = "Gero"

Dim ToChange as VariablePointer 'pseudo

If ChangeFirstName
  ToChange = MemoryOf FirstName
  MsgBox("Changed first name!")
Else
  ToChange = MemoryOf LastName
  MsgBox("Changed last name!")
End If

ToChange = GenericData

So that we can use logic to determine the variable to change. This is to avoid using two Select-cases, as per my actual code:

VB.NET:
    Sub GMImport()

        Dim Magic As Int16 = 0
        Dim Version As Int16 = 0

        Dim Data() As Byte = File.ReadAllBytes(ImportData)
        Dim Pos As UInt64 = 0
        While Pos < Data.Length
            Dim BytesToTravel As UInt16 = 4
            'FIRST select case here ::
            If Pos = 0 Then
                BytesToTravel = 4
            ElseIf (Pos + 1) = 4 Then
                BytesToTravel = 4
            End If
            Dim SubBytes(BytesToTravel) As Byte
            For G As Int16 = 0 To BytesToTravel
                SubBytes(G) = Data(Pos + G)
            Next
            Dim Final As Int16 = BitConverter.ToInt16(SubBytes, 0)
            'ANOTHER SELECT CASE HERE, to determine which variable to change.
            'ANNOYING because these select cases will get very long and there will be two of them.
            If Pos = 0 Then
                Magic = Final
            ElseIf (Pos + 1) = 4 Then
                Version = Final
            End If
            MsgError(Pos.ToString + ": " + Final.ToString)
            Pos += BytesToTravel
        End While
    End Sub

You ask - why not put the variable setting code in the first select-case (combine branches)? Not possible. The code to determine the value to set, is generic and happens after the first select case. Then the variable is set. You'd have to copy that code into each branch.

I don't see any way to do it, than to have a pointer variable that can be set to the memory of a variable. I already use Delegates for functions; my .NET code is going to get really ugly, repetitive and inefficient if there's no way to use pointers like my first example.

Thanks.
 
VB doesn't support pointers, plain and simple. If you want to use pointers then use C#.

That said, I'm guessing that there's a better way to do what you want but it's hard to say because you haven't actually shown us these Select Case statements.
 
you haven't actually shown us these Select Case statements.
I used If statements instead; sorry about calling them selects though -

VB.NET:
If Pos = 0 Then
  BytesToTravel = 4
ElseIf (Pos + 1) = 4 Then
  BytesToTravel = 4
End If

I managed to find a way to do it though! I will use my top example in the first post.

VB.NET:
Sub PointerSet(ByRef Variable as Object, ByVal TheValue as Object)
  'Generic code which gave 2 select cases can go here
  Variable = TheValue
End Sub

Dim GenericData as String = "Hello, World"
Dim FirstName as String = "Daniel"
Dim LastName as String = "Gero"
If ChangeFirstName
  PointerSet(FirstName, GenericData)
Else
  PointerSet(LastName, GenericData)
End If

When you run the sub, the ByRef passes the memory or reference to the variable, very pointer-like.
 
I still must be missing something because I don't see how that code is functionally any different to this:
Dim GenericData as String = "Hello, World"
Dim FirstName as String = "Daniel"
Dim LastName as String = "Gero"
If ChangeFirstName
  FirstName = GenericData
Else
  LastName = GenericData
End If
 
Back
Top