Results 1 to 6 of 6

Thread: [2.0] Object References Oddity

  1. #1

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60

    [2.0] Object References Oddity

    I'm doing a side-application from my main project.

    I have two Structs
    structAnimation
    which holds List<structFrames>Frames

    When I add structAnimation to a hashtable, then add some structFrames to my Frames list.. whenever I go back to call the instance from a hashtable, the new changes aren't there! I have to re-add it to the hashtable to get it to update.

    Very weird. I'm wondering what the deal is. This project has had a few other oddities going on. I'm practically doing the same things as my main project, but i'm getting odd results now and then

  2. #2
    Hyperactive Member drattansingh's Avatar
    Join Date
    Sep 2005
    Posts
    395

    Re: [2.0] Object References Oddity

    Something somewhere is deleting or changing that values. Those sort of problems you have to trace to find out exactly when and why it is changing. Are you sure it's changing the values in the first place? - Jennifer

  3. #3
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    Re: [2.0] Object References Oddity

    It's one of the major differences between classes and structs in C#. classes are reference types; structs are value types. I would wager that when you add a structure to a hashtable, you add a copy, not a reference. If you want to see this behavior, you should try using a class instead of a struct.

    Hope this helps.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  4. #4

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60

    Re: [2.0] Object References Oddity

    I changed them to classes, and it still seems to do the same thing.

    The weird thing is, only SOME values aren't being saved, rather than everything not being saved.

    One of my classes I read an XML file and throw in some widths/heights/X/Y values.. All of them read properly, and if I walk through it, everything is correct. But once it gets out of my XML reading function.. some values don't get saved.

    I create about 6 classes.. after i parse the XML, I attempt to read the values and the last frame's HEIGHT value isn't correct.. it's just the old value (whatever variable was before).. So it's as if it's not getting set, despite that it IS getting set, according to the debugging.

    I'm walked through and I havn't found anything that is changing the value, either. Everytime I load it though, it's always the same instance and same height causing the problem... can't find anything

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [2.0] Object References Oddity

    A structure shouldn't have a member that's a List. Structures are supposed to be simple types. You should definitely be using a class.

    If you have multiple references to the same object and make changes through one of those references then the other will reflect those changes because they both refer to the same object. For instance, if your mother puts on a red dress and then your father checks what his wife is wearing, he's gonna find a red dress. There's no way it can be otherwise because "your mother" and "your father's wife" refer to the same person (or maybe not, but you get the idea). Reference types in .NET are exactly the same, so if you're using an instance of a class then you absolutely must be seeing this behaviour. If it doesn't appear that you are then it must be because you're doing something wrong in your code. Show us the code you're using when this situation arises and we'll see if we can see the issue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member MasterEvilAce's Avatar
    Join Date
    Jul 2002
    Posts
    60

    Re: [2.0] Object References Oddity

    Found it. A friend looked over my code and found it. When you type in a textbox, it updates the variables. However, whenever i update the textboxes with the variables, the textboxes were updating and resetting the variables.

    Fixed now thanks

Posting Permissions

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



Click Here to Expand Forum to Full Width