Variable = Variable? (re post)

Discussion in 'ASP .Net' started by Ron, Jul 21, 2005.

  1. Ron

    Ron Guest

    I re posted beacause I copied and pasted my code incorrectly that it
    probably needed a re start with the correct code. I also striped out the
    Clone() stuff because that really had no bearing on the problem Im haveing.
    My goal is to overwrite the Group object in the collection with the one that
    is passed in the method. For some reaseon it doesnt overrite it. But I
    tested by just assiging the 'Name' property instead of overwriting he entire
    object and it worked; Everytime I use the object afterwards it has the new
    name. The second example where the object is complete overwritin doesn't
    work I can't understand why.

    NOTE: this method resides in the Global.asax.

    -------------------
    This works...
    -----------------
    e.Group.Name = "Changed Name";

    GroupCollection groups = Application [ "EHIMOnline_Groups" ] as
    GroupCollection;

    if ( groups != null )
    {
    Group group = groups.Find ( e.Group.GroupID );

    if ( group != null )
    {
    group.Name = e.Group.Name;
    }
    }

    -------------------------------------
    This Doesn't
    --------------------------------------
    e.Group.Name = "Changed Name";

    GroupCollection groups = Application [ "EHIMOnline_Groups" ] as
    GroupCollection;

    if ( groups != null )
    {
    Group group = groups.Find ( e.Group.GroupID );

    if ( group != null )
    {
    group = e.Group;
    }
    }
     
    Ron, Jul 21, 2005
    #1
    1. Advertising

  2. Ron

    Marina Guest

    The problem is that 'group' is just a variable. The variable points to an
    oject in memory.

    All your code doing in example #2 is point 'group' to a different location
    in memory. The place it originally pointed to still exists, and still has
    its data. It has no bearing on that object. In fact, something in the
    collection is still pointing to this object in memory.

    So now you have 'group' and 'e.Group' pointing to the same object in memory.
    That object only exists once.

    The collection remains unchanged (unless e.Group happens to be pointing to
    one of the groups in it).

    I recommend you read up on how objects and references work, and how value
    types work, and the difference between the 2.

    "Ron" <> wrote in message
    news:...
    >I re posted beacause I copied and pasted my code incorrectly that it
    >probably needed a re start with the correct code. I also striped out the
    >Clone() stuff because that really had no bearing on the problem Im haveing.
    >My goal is to overwrite the Group object in the collection with the one
    >that is passed in the method. For some reaseon it doesnt overrite it. But
    >I tested by just assiging the 'Name' property instead of overwriting he
    >entire object and it worked; Everytime I use the object afterwards it has
    >the new name. The second example where the object is complete overwritin
    >doesn't work I can't understand why.
    >
    > NOTE: this method resides in the Global.asax.
    >
    > -------------------
    > This works...
    > -----------------
    > e.Group.Name = "Changed Name";
    >
    > GroupCollection groups = Application [ "EHIMOnline_Groups" ] as
    > GroupCollection;
    >
    > if ( groups != null )
    > {
    > Group group = groups.Find ( e.Group.GroupID );
    >
    > if ( group != null )
    > {
    > group.Name = e.Group.Name;
    > }
    > }
    >
    > -------------------------------------
    > This Doesn't
    > --------------------------------------
    > e.Group.Name = "Changed Name";
    >
    > GroupCollection groups = Application [ "EHIMOnline_Groups" ] as
    > GroupCollection;
    >
    > if ( groups != null )
    > {
    > Group group = groups.Find ( e.Group.GroupID );
    >
    > if ( group != null )
    > {
    > group = e.Group;
    > }
    > }
    >
     
    Marina, Jul 21, 2005
    #2
    1. Advertising

  3. Ron

    Phillip Ian Guest

    I'm certainly no c# expert, but is it a good idea to name your class
    and your instance variable the same? Seems like in your second
    example, the word "group" in the line "group = e.Group;" is a bit
    indeterminate. Does it refer to the class or the instance? In the
    first example, the fact that you are referring to a member probably is
    enough of a clue.

    Just a thought.

    -Phil
     
    Phillip Ian, Jul 21, 2005
    #3
  4. Ron

    Ron Guest

    Cool. I was under the impression that if I assigned the 'group' var with
    the new group it would update the references like a chain.
    So the collection would point to list[0] -> group -> new group.

    As you pointed out thats not the case. I ended up adding a replace method
    to my collection which would insert the new object and remove the old one.
    Which works how i need it to.

    Thanks!
    Ron


    "Ron" <> wrote in message
    news:...
    >I re posted beacause I copied and pasted my code incorrectly that it
    >probably needed a re start with the correct code. I also striped out the
    >Clone() stuff because that really had no bearing on the problem Im haveing.
    >My goal is to overwrite the Group object in the collection with the one
    >that is passed in the method. For some reaseon it doesnt overrite it. But
    >I tested by just assiging the 'Name' property instead of overwriting he
    >entire object and it worked; Everytime I use the object afterwards it has
    >the new name. The second example where the object is complete overwritin
    >doesn't work I can't understand why.
    >
    > NOTE: this method resides in the Global.asax.
    >
    > -------------------
    > This works...
    > -----------------
    > e.Group.Name = "Changed Name";
    >
    > GroupCollection groups = Application [ "EHIMOnline_Groups" ] as
    > GroupCollection;
    >
    > if ( groups != null )
    > {
    > Group group = groups.Find ( e.Group.GroupID );
    >
    > if ( group != null )
    > {
    > group.Name = e.Group.Name;
    > }
    > }
    >
    > -------------------------------------
    > This Doesn't
    > --------------------------------------
    > e.Group.Name = "Changed Name";
    >
    > GroupCollection groups = Application [ "EHIMOnline_Groups" ] as
    > GroupCollection;
    >
    > if ( groups != null )
    > {
    > Group group = groups.Find ( e.Group.GroupID );
    >
    > if ( group != null )
    > {
    > group = e.Group;
    > }
    > }
    >
     
    Ron, Jul 21, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Oleg
    Replies:
    0
    Views:
    1,174
  2. Sridhar Hegde
    Replies:
    2
    Views:
    2,531
    Ajeetha Kumari
    Sep 13, 2004
  3. khawar
    Replies:
    2
    Views:
    1,416
    vMike
    Oct 28, 2003
  4. John Dalberg
    Replies:
    8
    Views:
    641
    John Dalberg
    Sep 23, 2004
  5. Vishal
    Replies:
    1
    Views:
    678
    Steve C. Orr [MVP, MCSD]
    Dec 21, 2004
Loading...

Share This Page