Storing reference to reference

M

marco_segurini

Hi,

the 'user' class describes what is my target:
I want to save a reference to a class instance and a reference to the
reference to the class instance calling a 'user' member function and
later
use them in another member function to restore the original value.

Is there any C# programming technique that may help me?

Thanks for any help.
Marco.

class MyClass
{
int i_;
public MyClass(int val) { i_ = val; }
public void Dump() { System.Console.WriteLine(i_); }
};

class user
{
//ref MyClass refMyClass_ = null; <--- temp ref-ref
MyClass iOrg_ = null;
public user(ref MyClass i)
{
//refMyClass_ = ref i; // <--- here I want to save the ref-ref
iOrg_ = i;
i = new MyClass(5);
}

~user()
{
//refMyClass_ = iOrg_; //<--- here I want to use ref and ref-ref to
restore the original value
}
};

class Program
{
static void Main(string[] args)
{
MyClass i = new MyClass(1);
i.Dump();
{
user u = new user(ref i);
i.Dump();
}
i.Dump();
}
}

/////
Output I get
1
5
5

Output I wish:
1
5
1
 
N

Nick Malik [Microsoft]

It would really help me (and perhaps others) to understand what you are
doing if you give us either a description of the business problem you are
trying to solve, or a description of the design you are trying to implement.

I did see that you want to restore the state of an object. One fairly
simply way is using XML Serialization. In a generic fashion, you will want
to implement the Memento design pattern.

HTH

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
 
D

Dmitriy Lapshin [C# / .NET MVP]

Nick,

What the original poster wants to accomplish is replacing the instance of
MyClass passed to the User constructor with another one created inside the
constructor. Then, when the User instance gets disposed, the poster wants to
restore the 'status quo'.

Pretty cumbersome technique and a bad programming practice, I think. More
than that, it just won't work this way - unlike C++, C# does not have a
'pointer to a pointer' concept - well, unless one resorts to unsafe
programming. Therefore, my verdict is to re-think the whole approach - and
indeed to describe the business problem rather then trying to 'fix' an
initially wrong technical solution to that problem.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Nick Malik said:
It would really help me (and perhaps others) to understand what you are
doing if you give us either a description of the business problem you are
trying to solve, or a description of the design you are trying to
implement.

I did see that you want to restore the state of an object. One fairly
simply way is using XML Serialization. In a generic fashion, you will
want
to implement the Memento design pattern.

HTH

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
marco_segurini said:
Hi,

the 'user' class describes what is my target:
I want to save a reference to a class instance and a reference to the
reference to the class instance calling a 'user' member function and
later
use them in another member function to restore the original value.

Is there any C# programming technique that may help me?

Thanks for any help.
Marco.

class MyClass
{
int i_;
public MyClass(int val) { i_ = val; }
public void Dump() { System.Console.WriteLine(i_); }
};

class user
{
//ref MyClass refMyClass_ = null; <--- temp ref-ref
MyClass iOrg_ = null;
public user(ref MyClass i)
{
//refMyClass_ = ref i; // <--- here I want to save the ref-ref
iOrg_ = i;
i = new MyClass(5);
}

~user()
{
//refMyClass_ = iOrg_; //<--- here I want to use ref and ref-ref to
restore the original value
}
};

class Program
{
static void Main(string[] args)
{
MyClass i = new MyClass(1);
i.Dump();
{
user u = new user(ref i);
i.Dump();
}
i.Dump();
}
}

/////
Output I get
1
5
5

Output I wish:
1
5
1
 
N

Nick Malik [Microsoft]

Thank you Dmitriy,

The fact that the request is so unusual may explain why I didn't understand
it. Thanks for the quick brief.

Of course, you are right. C# won't capture this information for you. That
said, it can be captured as long as the caller is aware of the capturing.

MyClassType myvar = new MyClassType(); // (1)
myvar = new MyClassType(myvar); // (2)
.... do useful stuff
myvar = myvar.GetInternalClassType(); // (3)

(1) this gives us the original object
(2) this stores a reference to the original one in a private member using a
different constructor
(3) this re-assigns the original reference and drops the reference to the
new MyClassType object, which is free to GC.

Inside the MyClassType class:

class MyClassType
{
private MyClassType internalRef;
public MyClassType()
{

}
public MyClassType(MyClassType myref)
{
internalRef = myref;
}
public MyClassType GetInternalClassType()
{
return internalRef;
}
}
--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
Dmitriy Lapshin said:
Nick,

What the original poster wants to accomplish is replacing the instance of
MyClass passed to the User constructor with another one created inside the
constructor. Then, when the User instance gets disposed, the poster wants to
restore the 'status quo'.

Pretty cumbersome technique and a bad programming practice, I think. More
than that, it just won't work this way - unlike C++, C# does not have a
'pointer to a pointer' concept - well, unless one resorts to unsafe
programming. Therefore, my verdict is to re-think the whole approach - and
indeed to describe the business problem rather then trying to 'fix' an
initially wrong technical solution to that problem.

--
Sincerely,
Dmitriy Lapshin [C# / .NET MVP]
Bring the power of unit testing to the VS .NET IDE today!
http://www.x-unity.net/teststudio.aspx

Nick Malik said:
It would really help me (and perhaps others) to understand what you are
doing if you give us either a description of the business problem you are
trying to solve, or a description of the design you are trying to
implement.

I did see that you want to restore the state of an object. One fairly
simply way is using XML Serialization. In a generic fashion, you will
want
to implement the Memento design pattern.

HTH

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
marco_segurini said:
Hi,

the 'user' class describes what is my target:
I want to save a reference to a class instance and a reference to the
reference to the class instance calling a 'user' member function and
later
use them in another member function to restore the original value.

Is there any C# programming technique that may help me?

Thanks for any help.
Marco.

class MyClass
{
int i_;
public MyClass(int val) { i_ = val; }
public void Dump() { System.Console.WriteLine(i_); }
};

class user
{
//ref MyClass refMyClass_ = null; <--- temp ref-ref
MyClass iOrg_ = null;
public user(ref MyClass i)
{
//refMyClass_ = ref i; // <--- here I want to save the ref-ref
iOrg_ = i;
i = new MyClass(5);
}

~user()
{
//refMyClass_ = iOrg_; //<--- here I want to use ref and ref-ref to
restore the original value
}
};

class Program
{
static void Main(string[] args)
{
MyClass i = new MyClass(1);
i.Dump();
{
user u = new user(ref i);
i.Dump();
}
i.Dump();
}
}

/////
Output I get
1
5
5

Output I wish:
1
5
1
 

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

Top