Pointer to the first element of an array? RRS feed

  • Question

  • Hi,


    I'm working with automatically generated code for test units that are produced in both C# and VB. The generated code calls a function that has 2 parameters, a ref int representing the first element of an array and an int representing the size of the array.


    MIL.MmetAddFeature( ref MILFeatureArray, arrayLength);


    The parameters are strings that are the same for both C# and VB ("ref MILFeatureArray" and "arrayLength"), so I can't use the [] operator since it wouldn't work in VB! I am free to do whatever I want before and after the call of this function, change the variables's underlaying strings, but I can't modify the call to the function individually in the C# test unit in a way that would only work in C#, since the code is generated frequently and the modifications would be lost...


    The first solution that came to my mind would be to have MILFeatureArray as an int that would point to the first element of an array. However, I found no way to do so, "int MILFeatureArray = tempArray[0];" clones the value from tempArray and allocates new memory in which it is put. Would there be a way to create an int which would point to the first element of an int array?


    Else, there could be a way to workaround the problem if there was a way to get the first element of the array without using the [] operator that would be the same in C# and VB.


    Creating an overload for the function that would have a "ref int[]" as the first parameter is not possible.


    Unsafe code not alllowed!


    Any help would be appreciated!

    Friday, March 9, 2007 3:24 PM

All replies

  • Hi,

    use - Array.GetValue(0) -  Method 
    Gets the value of the specified element in the current Array.
    ch.t.gopi kumar
    Sunday, March 11, 2007 7:50 AM
  • Thanks for your answer, TilakGopi


    Unfortunately, the GetValue method cannot be used to pass a reference directly in a function, since it isn't a declared variable (A ref or out argument must be an lvalue), and that it does a copy of the value to a new instance of an object. Also, there would be a problem with casting from an Object to an int which can't be done with the same code in VB and C#.


    I still haven't found a way to solve my problem, I think I'm going to have to modify the code generator.

    Monday, March 12, 2007 12:06 PM