Skip to main content
GameDev.net gamedev.net
🔒 Locked

c# - pointers and unsafe code scenario

Started by supagu Dec 28, 2006 at 11:59 PM 4 replies 1.2k views
Original Post
supagu
supagu
okay, i dont use c# much im used to c++, but ive seem to be in a situation where i need pointers but want to see if i can avoid it first. my scenario: i have a bunch of class 'C's which has a reference to a class 'T', 'C's can share 'T's T's go in to a linked list, and they have a list of which C's point to them. eg. i do T t = new T(); C0.t = t t.Add(C0) C1.t = t t.Add(C1) managedList.Add(t) what i do occasionally is move one C from one T to another eg. Team t = C0.t t.Remove(C0) C1.t = C0.t C1.t.Add(C0) if (t.Count < 0) managedList.Remove(t) how ever this seems to work the first few times, but then i end up with a bunch of empty T's which dont get removed from the managed list, and then a bunch of C's point to some T's that are not in the managedList. I also find my self having to reassign changed objects back to arrays eg. list.Add(new X); X x = list[0]; x.someValue++; list[0] = x; so in both situations i could fix with pointers probably, but is there a proper c# way to go about this?
Skizz
Skizz
Quote:

Team t = C0.t
t.Remove(C0)
C1.t = C0.t // <--- this line is wrong, it should be C0.t = C1.t
C1.t.Add(C0)


Skizz
supagu
supagu
okay i changed that around *whoops*

but i still have the problem of the following code never running:

if (t.Count < 0)
managedList.Remove(t)

and the second scenario still occurs (same problem i guess)
supagu
supagu
sorry i meant
<= 0
alunharford
alunharford
Quote:
Original post by supagu
I also find my self having to reassign changed objects back to arrays
eg.
list.Add(new X);
X x = list[0];
x.someValue++;
list[0] = x;


Well I don't understand your problem in the first case; maybe you should try posting a short program that demonstrates the problem...

However, the second case is much easier: Just change your code to:

list.Add(new X());list[0].someValue++;


(I suspect this isn't a problem anyway. A decent optimising compiler should spot that... I've not tried it though.)

Alun Harford

Topic Locked

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

Sign in to reply to this topic.