Thread: Problems with classes

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    98

    Problems with classes

    I recently started writing a game in XNA, and I have run into a problem. I have a class called baseObject which I am using for all the objects in my game. But the problem is, if I create two instances of it, whatever changes I make to the one also affect the other. Here's some of my code:

    Code:
    baseObject temp1 = new baseObject();
    baseObject temp2 = new baseObject();
    
    ...
    
    temp1.objectPosition = new Vector(0.0f, 5.0f, 0.0f);
    Now, temp2's position will be the same as temp1's.
    Why?

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Did you make the class's variables static? Because if you did, all instances share them.

    I can't imagine any other way of how that would happen.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    No, I've got no static variables or classes anywhere. My dad says there is a specific name for what is happening, and that there is a way to disable it, but he doesnt know how.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    My dad says there is a specific name for what is happening
    PEBKAC, but I don't think that's what your dad means.

    There is absolutely no way modifying the instance variables of one object also modifies those of another object unless you've created some aliasing. Remember that in C#, you're dealing mostly with references to objects. For example, "temp2 = temp1" would not copy the values over, it would make temp2 reference the same object as temp1. Any change to the object through temp1 would subsequently be visible through temp2 too, but that's because there's just one object.

    What exactly is in these three dots?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    Well, somewhere in my code I did have temp2=temp1, but even without this it still modifies both. I'll some more code tomorrow, because I am away from my computer now.
    Its a really odd problem. I've never had it before.

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    98
    It seems my problem was with "temp2 = temp1". I got it to work, finally. And I seem to think it's XNA's fault, because it has some weird error when loading two of the same models (which is why I used temp2=temp1). But I moved the offending code from the object loading code to the object drawing code and now it works perfectly!
    I just have another question, why does temp2=temp1 not copy the values over?

  7. #7
    Registered User
    Join Date
    Nov 2007
    Posts
    14
    because in c# every complex oblect is a reference to the object it self

    by doing "temp1 = temp2" you just copied the reference from temp2 to temp1 , that makes temp1 and temp2 connecting to the same object (or in fact - place in memory of that object)

    most complex classes have copy methods that copies the data from that object to the new one - and creating a new object with that data...

    i hope i got it right :P, im new at this too

  8. #8
    Registered User Aran's Avatar
    Join Date
    Aug 2001
    Posts
    1,301
    To augment Rinbowinblack's explanation:

    Class objects in C# are reference objects. That means that your "temp1" actually just points at an object somewhere on the heap. When you assign temp2 to be temp1, C# interprets this as "point temp2 to what temp1 is pointing at." This makes them point to the same underlying data in the heap, so changing temp1 is the same as changing temp2.

    Struct objects in C# are value objects. They work like you think Class objects should: When you say struct1 = struct2, the contents of struct2 are copied into struct1. When you subsequently modify struct2, it has no affect on struct1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with virtual function calls, please help
    By e66n06 in forum C++ Programming
    Replies: 12
    Last Post: 12-12-2007, 05:12 AM
  2. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  3. College Classes
    By nubby in forum C Programming
    Replies: 2
    Last Post: 10-07-2007, 12:32 AM
  4. Inheritance, overriding, problems
    By CodeMonkey in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2007, 01:26 AM
  5. Sharing a variable between classes of different .CPP files
    By divingcrab in forum C++ Programming
    Replies: 5
    Last Post: 07-07-2002, 02:57 PM