Skip to main content
gamedev.net
🔒 Locked

C# ListT pointers

Started by daerom Jun 1, 2006 at 10:07 PM 1 replies 800+ views
Original Post
daerom
daerom
Hello, I have recently (3 months) switched over to writing some stuff in C# just to try it out. I use to do everything in C++, so it's not that big of a deal. But one thing is bothering me... and I just can't get it to work. Basically, here is what I use to do in C++. Lets say I had a class that managed what models were loaded and added them into a List... then I'd have a class that would be called CEntity, it would store a pointer to the same data. Problem is, I can't get C# to do this... it just makes a copy... so if I modified CEntity.MeshData, the entry in the list would not reflect that change. Any help would be appreciated.
-Daerom
Nypyren
Nypyren
Break into the debugger when you assign the CEntity's MeshData reference.

Add the MeshData to the watch window, right click on the variable and say "Make Object ID". This makes an ID that's "attached" to the object that will let you visually compare in watch or quickwatch windows whether the reference is the same as other references.

Now go to your list and examine its contents and see if that ID is on the item you're referencing.

If you're using List and then directly saying "CEntity.MeshData = list[1]" or whatever, then it should work how you WANT. If you're using List then it will *copy* like you are experiencing.

(This assumes you're using Visual Studio)

By default in C#, any time you say "class1 = class2" you're doing the same thing as "pointer1 = pointer2" in C++. But if you use "struct1 = struct2" in C#, it will do the same thing as "*pointer1 = *pointer2" in C++.

So if all of your stuff are classes, it should just work.

Post some code if you still have problems.
daerom
daerom
Yeah, that's exactly how I had read it too... basically structs are value types and classes are reference types... I guess I must have made a mistake somewhere. Thanks for the suggestions on the debugger.
-Daerom

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.