Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / July 2005

Tip: Looking for answers? Try searching our database.

Variable = Variable? (re post)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ron - 21 Jul 2005 19:36 GMT
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 - 21 Jul 2005 19:41 GMT
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.

>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
[quoted text clipped - 43 lines]
> }
> }
Phillip Ian - 21 Jul 2005 19:43 GMT
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
Ron - 21 Jul 2005 20:45 GMT
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

>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
[quoted text clipped - 43 lines]
> }
> }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.