Pointers in C#
-
Hi, i need something similar to a pointer in C#. C# itself has pointers implemented but they do not fit my needs. I want to "map" a Variable to another one, in two different objects of a different class. Pointing to a variable of a "user-defined" type is not a problem but pointing to a "managed" type is. It doesn't let me point to strins, integers or other built-in datatypes. Thats why pointers do not work in my case - do you know any workaround for making the pointers work as i want them to or do you know another possibility to realize this? Here is a little example of what i want to be able to do:
class MyClass { public int AValue = null; } class AnotherClass { public int * APointerToAValue = null; public AnotherClass(ref int MappingVar) { unsafe { fixed (int * tmpptr = &MappingVar) { APointerToAValue = tmpptr; } } } }
Now when executing the following codeMyClass MyObj = new MyClass(); AnotherClass AnotherObj = new AnotherClass(ref MyObj.AValue); AnotherObj.APointerToAValue = 3 Console.WriteLine("Pointer: " + AnotherObj.APointerToAValue.ToString()); Console.WriteLine("Value: " + MyObj.AValue.ToString()); MyObj.AValue = 4 Console.WriteLine("Pointer: " + AnotherObj.APointerToAValue.ToString()); Console.WriteLine("Value: " + MyObj.AValue.ToString());
this result should be produced:Pointer: 3 Value: 3 Pointer: 4 Value: 4
Is there any way to manage it like this in C#? It doesn't has to be realized with pointers at all i just need a solution for the given problem! -
Hi, i need something similar to a pointer in C#. C# itself has pointers implemented but they do not fit my needs. I want to "map" a Variable to another one, in two different objects of a different class. Pointing to a variable of a "user-defined" type is not a problem but pointing to a "managed" type is. It doesn't let me point to strins, integers or other built-in datatypes. Thats why pointers do not work in my case - do you know any workaround for making the pointers work as i want them to or do you know another possibility to realize this? Here is a little example of what i want to be able to do:
class MyClass { public int AValue = null; } class AnotherClass { public int * APointerToAValue = null; public AnotherClass(ref int MappingVar) { unsafe { fixed (int * tmpptr = &MappingVar) { APointerToAValue = tmpptr; } } } }
Now when executing the following codeMyClass MyObj = new MyClass(); AnotherClass AnotherObj = new AnotherClass(ref MyObj.AValue); AnotherObj.APointerToAValue = 3 Console.WriteLine("Pointer: " + AnotherObj.APointerToAValue.ToString()); Console.WriteLine("Value: " + MyObj.AValue.ToString()); MyObj.AValue = 4 Console.WriteLine("Pointer: " + AnotherObj.APointerToAValue.ToString()); Console.WriteLine("Value: " + MyObj.AValue.ToString());
this result should be produced:Pointer: 3 Value: 3 Pointer: 4 Value: 4
Is there any way to manage it like this in C#? It doesn't has to be realized with pointers at all i just need a solution for the given problem!You can not access the member of the class directly (unless resorting to reflection), but you can use a propery to transparently access the member via the object:
class AnotherClass {
private MyClass theOther;
public AnotherClass(MyClass theOther) {
this.theOther = theOther;
}public int TheValue {
get { return this.theOther.AValue; }
set { this.theOther.AValue = value; }
}
}--- b { font-weight: normal; }
-
You can not access the member of the class directly (unless resorting to reflection), but you can use a propery to transparently access the member via the object:
class AnotherClass {
private MyClass theOther;
public AnotherClass(MyClass theOther) {
this.theOther = theOther;
}public int TheValue {
get { return this.theOther.AValue; }
set { this.theOther.AValue = value; }
}
}--- b { font-weight: normal; }
Thanks for replying. The situation is a bit more complicated than i told you that's why it won't be solvable this way. I don't know the variable which will be "mapped". So in the first example i used "ref AValue" as parameter for AnotherClasses constructor. In fact, AValue could be MyValue, HisValue or AnyonesValue - i don't know it before calling the constructor at runtime. The "TheValue"-property would have to return the variable, which was permitted to the constructor. At this point i will need pointers again. As already said, thank you for you reply but i couldn't solve the problem by now :(
-
Thanks for replying. The situation is a bit more complicated than i told you that's why it won't be solvable this way. I don't know the variable which will be "mapped". So in the first example i used "ref AValue" as parameter for AnotherClasses constructor. In fact, AValue could be MyValue, HisValue or AnyonesValue - i don't know it before calling the constructor at runtime. The "TheValue"-property would have to return the variable, which was permitted to the constructor. At this point i will need pointers again. As already said, thank you for you reply but i couldn't solve the problem by now :(
Then you have to rethink the entire problem. You can't use pointers in that way in a platform that uses a garbage collected heap. You would have to pin the object that you are pointing into at the current memory location, which will badly cripple the whole garbage collecting process as long as it is pinned.
--- b { font-weight: normal; }