Variable = Variable? (re post)

R

Ron

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;
}
}
 
M

Marina

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.
 
P

Phillip Ian

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
 
R

Ron

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top