- Home /
Store reference to array as variable
I want to have a class that can store references to arrays in a different class and be able to modify those arrays by the new arrays.
public class test : MonoBehaviour
{
// Variables
//,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,
test2 testing;
int[] original;
void Start()
{
original = new int[] { 1, 2, 3, 4 };
testing = new test2();
testing.reference = original;
testing.Change(); // changes original values
testing.Modify(); // does not modify original
DebugArray( original );
Debug.Log( ":::" );
DebugArray( testing.reference );
}
void DebugArray( int[] array )
{
for ( int i = 0; i < array.Length; ++i )
{
Debug.Log( array[i] );
}
}
}
public class test2
{
public int[] reference;
public void Change()
{
reference[0] = 135134;
}
public void Modify()
{
reference = new int[] { 4, 3, 2, 1, 0 };
}
}
The problem I'm running into with this code is that I can change the values of the original array via the new class, but I can't modify the array. Is there a way I could do this in C#? It seems like a serious limitation of C# if I can't. Are there any alternatives? Maybe using IntPtr? To be clear I need to be able to change the size of the array in the new class as well as the values.
Answer by Habitablaba · Oct 24, 2014 at 12:00 AM
The right answer would be to think real hard about what it is you're trying to do, and then think real hard about a better way to do it.
Answer by Jeff-Kesselman · Oct 23, 2014 at 03:27 PM
To change the array referenced by a variable you need access to that variable. This is tru in C or C# or any other language.
public class test : MonoBehaviour
{
// Variables
//,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,
test2 testing;
public int[] original;
void Start()
{
original = new int[] { 1, 2, 3, 4 };
testing = new test2();
testing.reference = original;
testing.Change(); // changes original values
testing.Modify(); // does not modify original
DebugArray( original );
Debug.Log( ":::" );
DebugArray( testing.reference );
}
void DebugArray( int[] array )
{
for ( int i = 0; i < array.Length; ++i )
{
Debug.Log( array[i] );
}
}
}
public class test2
{
test1 theOtherInstance;
public void Change()
{
theOtherInstance.original[0] = 135134;
}
public void Modify()
{
theOtherInstance.original = new int[] { 4, 3, 2, 1, 0 };
}
}
well in c++ i would just use a pointer and wouldn't have this problem
Yes you would have this problem.
There is very little real difference between a pointer and a reference under the hood.
If you change one pointer to an array it does NOT change any other pointers to the same array-- they still point to the old data.
You would need a pointer to the pointer you want to change in order to change what it points at, and thats exactly what an object instance reference is.
C# is a true OOP language. If you desperately want to write pointer based procedural C code then mark your code as "unsafe" and do just that.
Your answer
Follow this Question
Related Questions
does Array.Concat() and Array.Push() add objects by Value rather than by Reference ? 1 Answer
How to check if vector in array was not changed? 1 Answer
How to let multiple values contribute two one value constantly 1 Answer
[c#] Save an instance for future re-instantiation 0 Answers
c# How does an == decide if two objects are the same? 1 Answer