 Originally Posted by SuperSmashcz
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.
|
|
Bookmarks