• Lucid Dreaming - Dream Views




    Results 1 to 9 of 9

    Hybrid View

    1. #1
      Explorer of the Mind SuperSmashcz's Avatar
      Join Date
      Jul 2008
      Gender
      Location
      MD
      Posts
      51
      Likes
      0

      Weird C# Pointer Problem

      Okay this is the small code block

      {
      Tiles Current = null;
      Current = new Tiles(Maps[currentMap].map[ClickedX, ClickedY]);
      EditHistory.Add(new TileHistory(Current));
      Maps[currentMap].map[ClickedX, ClickedY].setLayer(layer, tile.TileNumber, myTileSets, tileX, tileY);
      }

      When i run my program the Current Variable is set to null and then takes on the value of Maps[currentMap].map[ClickedX, ClickedY]. Basically i want to store Maps[currentMap].map[ClickedX, ClickedY] into Current and then change Maps[currentMap].map[ClickedX, ClickedY] to something different. After i change Maps[currentMap].map[ClickedX, ClickedY] to something different, the current variable changes as well. Any reason why? I thought u had to use unsafe{*&} stuff to use pointers
      From every time we meet, to every time we part, i will add another memory to my shattered heart, and for every dream we chase, another memory will take its place, so we can remember it one day.

      LD's 15
      DILDs 14
      WILDS 1

    2. #2
      Member Achievements:
      1 year registered Veteran First Class 5000 Hall Points

      Join Date
      Sep 2004
      Gender
      Location
      Seattle, WA
      Posts
      2,503
      Likes
      217
      I have zero C# experience, but if it's all by-reference like Java, then if the Tiles constructor just assigns a non-primitive instead of copying it, you might experience that problem.

    3. #3
      Member Identity X's Avatar
      Join Date
      Mar 2004
      Gender
      Posts
      1,529
      Likes
      7
      Quote Originally Posted by SuperSmashcz View Post
      Okay this is the small code block

      {
      Tiles Current = null;
      Current = new Tiles(Maps[currentMap].map[ClickedX, ClickedY]);
      EditHistory.Add(new TileHistory(Current));
      Maps[currentMap].map[ClickedX, ClickedY].setLayer(layer, tile.TileNumber, myTileSets, tileX, tileY);
      }

      When i run my program the Current Variable is set to null and then takes on the value of Maps[currentMap].map[ClickedX, ClickedY]. Basically i want to store Maps[currentMap].map[ClickedX, ClickedY] into Current and then change Maps[currentMap].map[ClickedX, ClickedY] to something different. After i change Maps[currentMap].map[ClickedX, ClickedY] to something different, the current variable changes as well. Any reason why? I thought u had to use unsafe{*&} stuff to use pointers

      I'm a C# developer by trade (but quite new to it).

      C# uses reference types by default for all types, unless they are primitive types (int, uint, byte etc. but not string) or defined by a Struct. These work the a similar way to what true C++ pointers do: if variables are assigned to it, only the location in memory is stored, so any change to it will affect the "value" of any other variable referencing the same object. That's whats happening here. And it's a good thing.

      C# does not support pointer arithmetic and other "unsafe" code in normal blocks. If a programmer were to use pointers in the true C++ fashion, he'd have to surround it in an "unsafe" block.

      {
      //Sensible stuff
      unsafe
      {
      //Here be madness
      }
      }

      Back to your problem, there are at least two solutions. Either rewrite Tiles as a struct, which is a value type (each variable stores the entire value of all the objects fields and changes are independent between variables as each has its own "seperate" version) - this may simply be a case of changing "class" to "struct" and then a little tweaking. Note that structs cannot be inherited, cannot inherit from other structs/classes, but can implement interfaces.

      The other way is to just write a method to copy the Tiles object:

      public Tiles GetCopy()
      {
      Tiles t = new Tiles(...);
      t.SomeProperty = this.SomeProperty;
      ....
      return t;
      }

      Note however that if any of your properties are reference types, you'll face the same problem again.

      So, in summary: C# uses reference types - which are somewhat equivalent to pointers - implicitly for all objects. Use structs or helper methods if you want to create exact copies of them.

      This is very vital stuff to know. The difference between reference and value types is an important concept. Check out MSDN, Wikipedia, or whatever. You say you are a "Video Game Programmer"? I would have thought to qualify for that job you might've known this, but, meh. It's perhaps a strange concept for those that are not initiated in the fact that "by reference by default" is the future

      Also, congrats on choosing C#, it's easily the best thing out here. I used Java for 3 years previously. It's awful in comparison. Java doesn't have structs, properties, "ref" and "out", explicitly virtual methods, and a thousand other lifesavers.
      Last edited by Identity X; 08-13-2008 at 12:18 AM.

    4. #4
      Member kichu's Avatar
      Join Date
      Oct 2005
      Gender
      Posts
      1,803
      Likes
      25
      DJ Entries
      40
      This should fix the problem:

      Let me know if you need any more help.

    5. #5
      FBI agent Ynot's Avatar
      Join Date
      Oct 2005
      Gender
      Location
      Southend, Essex
      Posts
      4,337
      Likes
      14
      Quote Originally Posted by kichu View Post
      This should fix the problem:

      Let me know if you need any more help.
      but if anyone asks,
      you can't point and say "he went that-a-way"

      that would be unsafe.....

    6. #6
      Explorer of the Mind SuperSmashcz's Avatar
      Join Date
      Jul 2008
      Gender
      Location
      MD
      Posts
      51
      Likes
      0
      ah cool thanks guys ill try it later at the moment im working on a different part of the program
      From every time we meet, to every time we part, i will add another memory to my shattered heart, and for every dream we chase, another memory will take its place, so we can remember it one day.

      LD's 15
      DILDs 14
      WILDS 1

    7. #7
      Banned
      Join Date
      Apr 2007
      Location
      Out Chasing Rabbits
      Posts
      15,193
      Likes
      935
      Cocoa is a C# derivative right?

    8. #8
      Member kichu's Avatar
      Join Date
      Oct 2005
      Gender
      Posts
      1,803
      Likes
      25
      DJ Entries
      40
      Quote Originally Posted by Ynot View Post
      but if anyone asks,
      you can't point and say "he went that-a-way"

      that would be unsafe.....
      Don't be ridiculous. This is a serious thread. GOD.

    Bookmarks

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •