Object = Object - sets a reference?

Hi,

I understand that if you do

object1 = object2;

in c# that sets object1 to reference object2, so in essence they are one and the same.

I'm trying to do this with two objects stored in a hashtable.

I have a function that returns an object stored in a hashtable, so doing somehting like this

public void SetValue(string Name)
{
obj = GetItem(Name);
obj.value = 123;
}

will set the hashtable object's item value to 123. This works fine.

However, I'd like to set one hashtable object to a reference of another item, like this

public void LinkItems(string HostName, string DonorName)
{
host = GetItem(HostName);
donor = GetItem(DonorName);

host = donor;
}

However, it seems this doesn't link them. Writing the results of host.equals(donor)in the function returns true, indicating in the function they're linked. Yet this isn't reflected in the hashtable.

Do I need to do something else

Thanks.


Answer this question

Object = Object - sets a reference?

  • Makover

    Your problem that there are objects and references to them.

    In your code there are exists some objects   references to them can be retrieved by GetItem.

    By host and donor  are two NEW variables thich store a reference to original objects.

    host = GetItem(HostName);
    donor = GetItem(DonorName);

    You can do anything you want with thouse variables. They are independent each other and even more - they are independent from references stored in GetItem.

    So:
    host = donor;

    Will simply change this reference variable value to point to new location.


  • papabear73

    Hi,

    Yes, object1 = object2 sets object1 as a reference on object2...
    IF its not a struct... when you do that in a struct, you actually create a "clone" or a copy of the object and pass it to the reciever...


    In your sample, (assuming your not using structs) the host object first gets the reference on the HostName. then donor gets the reference on the DonorName. Then here comes the problem, you dereferenced host and point it in donor. Hence Both honor and donor points to a single object in the hashtable not link them...

    I guess linking them would require you to use the original object (not a reference to the object) and point it to the donor object...

    Or you could try doing it in unsafe code and use pointers...



    cheers,


    Paul June A. Domag

  • ggponti

    Hi,
     thanks alot.

    For a moment then I thought I wa storing structs - but looking back it is a class.

    What I'm doing is having one large hashtable store lots of instances of objects - however the originals go out of scope straight away as all teh work in tucked inside functions. So I have functions to add an object to hashtable, get it out etc.

    What I'm aiming to do is make the host hastable entry link to the donor hashtable entry for another seamlessly. SO, if after running that code I then just got out the hastable item for HostName again I'd actually get out the hashtable entry for DonorName. hmm even my head is getting confused here Tongue Tied

    Thanks anyway for your help.

  • Object = Object - sets a reference?