locked
I don't know how to do this without pointers... RRS feed

  • Question

  • User1250726567 posted

    I know vb.net does not include pointers like C language. but I am trying do populate an simple integer array with variable of the same type

    for example :

    dim variable1 as integer = -1
    dim variable2 as integer = -1
    dim intArray() as integer = {variable1,variable2}
    
    for i = 0 to 1
       intArray(i) = 3
    next i

    it is a very simplyfied verson of what I want to do.

    I want to change the value of ma variables with my array referencing them with an index as if my variables

    were pointers of integer in c++ for exemple

    so that normally after the example, both variable should hold the number 3 instead of the number -1

     

    anyone know how to do that?

    Thursday, August 9, 2012 11:55 AM

Answers

  • User-1719515186 posted

    Apologies if I sound totally out of the blue here.

    I would probably replicate that behavior by not dealing with native types (such as integers) and create my own class called CustomInt

    Public Class CustomInt
       Public myInt as Integer
    End Class

    Now create 2 CustomInt objects

    Dim v1 as New CustomInt() With {.myInt = -1}
    Dim v2 as New CustomInt() With {.myInt = -1}

    Now create the array of CustomInt objects

    Dim custIntArray as CustomInt() = {v1, v2}

    Wrte a function that accepts this array by reference, loops over it, and changes the individual values.

    Private Sub MyArrayLooper(ByRef CustIntArray as CustomInt())
         Dim i As Integer  
         For i = 0 To CustIntArray.Length - 1
             (CustIntArray(i)).myInt = 3
         Next i
    End Sub

    Call this private function and pass in your array

    MyArrayLooper(custIntArray)
    

    When this call completes and the control is back into the caller (Main method for example), you will find that the individual CustomInt objects (stored in v1 and v2) have been modified.

    '---- v1.myInt should be equal to 3 ---




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 9, 2012 2:43 PM

All replies

  • User-287491473 posted

    I do not think you can do that directly with .NET.  You could probably figure out a way to do it with GCHandle, but it would be messy.  There is probably an easier way to do this.  What are you trying to do?

    http://www.dotnet-snippets.com/dns/varptr-for-net-SID585.aspx

    http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.gchandle.aspx

    Thursday, August 9, 2012 1:54 PM
  • User-1719515186 posted

    Apologies if I sound totally out of the blue here.

    I would probably replicate that behavior by not dealing with native types (such as integers) and create my own class called CustomInt

    Public Class CustomInt
       Public myInt as Integer
    End Class

    Now create 2 CustomInt objects

    Dim v1 as New CustomInt() With {.myInt = -1}
    Dim v2 as New CustomInt() With {.myInt = -1}

    Now create the array of CustomInt objects

    Dim custIntArray as CustomInt() = {v1, v2}

    Wrte a function that accepts this array by reference, loops over it, and changes the individual values.

    Private Sub MyArrayLooper(ByRef CustIntArray as CustomInt())
         Dim i As Integer  
         For i = 0 To CustIntArray.Length - 1
             (CustIntArray(i)).myInt = 3
         Next i
    End Sub

    Call this private function and pass in your array

    MyArrayLooper(custIntArray)
    

    When this call completes and the control is back into the caller (Main method for example), you will find that the individual CustomInt objects (stored in v1 and v2) have been modified.

    '---- v1.myInt should be equal to 3 ---




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 9, 2012 2:43 PM
  • User1250726567 posted

    youre not totally out of the blue, youre dead on! thanks!

    Thursday, August 9, 2012 2:57 PM