Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Pointers in C#

Pointers in C#

Scheduled Pinned Locked Moved C#
csharphelptutorialquestion
4 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mikone
    wrote on last edited by
    #1

    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 code MyClass 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!

    G 1 Reply Last reply
    0
    • M mikone

      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 code MyClass 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!

      G Offline
      G Offline
      Guffa
      wrote on last edited by
      #2

      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; }

      M 1 Reply Last reply
      0
      • G Guffa

        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; }

        M Offline
        M Offline
        mikone
        wrote on last edited by
        #3

        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 :(

        G 1 Reply Last reply
        0
        • M mikone

          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 :(

          G Offline
          G Offline
          Guffa
          wrote on last edited by
          #4

          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; }

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups