mathmax

Hello,

I would like to know if it is possible to get all references of an object instance.

For example :

Code Snippet

ClassA a1 = new ClassA()

ClassA a2 = a1

ClassA a3 = a1

I would like to have a generic mehtod that returns collection of classA containing a1, a2 and a3. Is possible How

Thank you in advance for your help,

mathmax



Re: Visual C# General How to get all references of an object instance ?

micvos

As far as I know this is not possible. There is a reference count that tells the garbage collector when the object can be released, but there is no list available where the references reside.




Re: Visual C# General How to get all references of an object instance ?

Peter Ritchie

micvos is correct. But, why would you want to do this




Re: Visual C# General How to get all references of an object instance ?

mathmax

Consider several references pointing to a single object instance. If one of them point to a new object instance, I would like that all the references also point to the new object instance. For axample, if I write :

Code Snippet

Personne p1 = new Personne("Maxime", 23);

Personne p2 = p1;

p1 = new Personne("Kevin", 25);

Console.WriteLine(p2);

it displays "ConsoleApplication1.Personne, maxime, 23" even though I would like to get "ConsoleApplication1.Personne, Kevin, 25". For that, I must have all references that point to the first instance in order to make them point to the second object instance.

An other question : Is it possible to use pointers with managed class How

I tried to write this :

Code Snippet
p2 * = &p1;

but I got this error message : Cannot take the ardess of a variable of a managed type.

Thanks for any help,

mathmax





Re: Visual C# General How to get all references of an object instance ?

houtexwebdev

think of it this way....

I have 15 variables, named i1 through i15 and they're all equal to 5

then I'd have to find a way of locating all integers in memory and set them equal to 3 instead..

That's really what these pointers are like.

The solution here would be to make an instance of that integer and place it in some context where everyone could have access to it and, when that value changes, they would all see the same value.

So you might want to think about something along the lines of either Creating a PersonWrapper that has a Person instance inside of it, but you con only access it through the Personwrapper's properties so that no one can have a reference to the actual Person instance, or you could create a PersonHolder that contained a CurrentPerson PRoperty and trust that people will refer to PersonHolder.CurrentPerson without ever saying Person p = PersonHolder.CurrentPerson





Re: Visual C# General How to get all references of an object instance ?

mathmax

Thank you for your answer, houtexwebdev

I cannot see the difference between the PersonWrapper and the PersonHolder. Are there static class Could show me a short example

Furthermore, I saw on msdn that there is a Pointer class. Perhaps it could be convenient for me, but I don't understand its usefulness...





Re: Visual C# General How to get all references of an object instance ?

houtexwebdev

I don't think that the Piinter class would get out anythig that would help you in this case.

Here is an example of what I was talking about earlier:

Code Snippet

public class Person

{

#region FirstName

private string _firstName;

public string FirstName

{

get { return _firstName; }

set { _firstName = value; }

}

#endregion

#region LastName

private string _lastName;

public string LastName

{

get { return _lastName; }

set { _lastName = value; }

}

#endregion

}

public class PersonWrapper

{

private Person _instance = new Person();

public string FirstName

{

get { return _instance.FirstName; }

set { _instance.FirstName = value; }

}

public string LastName

{

get { return _instance.LastName; }

set { _instance.LastName = value; }

}

public void SetPersonInstance(Person p)

{

_instance = p;

}

}

public class PersonHolder

{

#region ThePerson

private Person _thePerson;

public Person ThePerson

{

get { return _thePerson; }

set { _thePerson = value; }

}

#endregion

}

....or even

public Class Person{

private static Person _instance = new Person();

public static Person Instance{get{return _instance;}}

private Person(){

//makes sure no one can create an instace of this class

}

//properties etc...

}

Your other option would be





Re: Visual C# General How to get all references of an object instance ?

Pradeep C

If you need every object reference to point to the same object in memory, why don't you make the whole class static
P.S. this is not possible if you need only a few specified references to point to one object (and not all).