473,396 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Storing reference to reference

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
Nov 16 '05 #1
3 4680
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" <ma***********@virgilio.it> wrote in message
news:a3*************************@posting.google.co m...
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

Nov 16 '05 #2
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 [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:1r********************@comcast.com...
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" <ma***********@virgilio.it> wrote in message
news:a3*************************@posting.google.co m...
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



Nov 16 '05 #3
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 [C# / .NET MVP]" <x-****@no-spam-please.hotpop.com> wrote
in message news:%2****************@TK2MSFTNGP09.phx.gbl...
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 [Microsoft]" <ni*******@hotmail.nospam.com> wrote in message
news:1r********************@comcast.com...
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" <ma***********@virgilio.it> wrote in message
news:a3*************************@posting.google.co m...
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


Nov 16 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Christopher | last post by:
Hi all, I am trying to store a reference to a variable in my data providerr class but cannot save a reference to it. Can anyone help me? I would like this code to allow me to store any field...
6
by: sangeetha | last post by:
Hello, I'm passing a RWList object of one complex data to function f(). In the function f() i'm receiving in that list as reference. In the function f(), i want to modify one member variable in...
0
by: Matt Burland | last post by:
I was wondering what the correct way to do this is, in C++ I'd just use a string* : public class A { private string myString // I actually want to keep a reference, but I'm not sure of...
3
by: A. Burch | last post by:
I posted early about Hashtable use and the shallow/deep copy and storing a reference answered that question. I've got a larger implementation of this, but am not sure why I'm getting this error....
9
by: Murat Ozgur | last post by:
Hello, I want to know about "creating new objects without assignment to a reference" like this : ... new Employee("John","Woo"); .... Is this a good programming practice ? How does...
2
by: Jeff User | last post by:
I want to convert each record read (with a datareader) to a string array and then place each of those arrays into another array (sResult) The problem here is that when the inner loop begins to...
4
by: Lucas Tam | last post by:
Hi all, I'm currently storing a reference to the entire thread object in my application. However, I'm wondering if there is a smaller object I can point to such as an Integer ThreadID? If...
0
by: Jonas Pohlandt | last post by:
Hello Group, I used something like this in a program of mine: public class WorkObject private _myproperty as string public property MyProperty as string() get,set trivial
5
by: homsan toft | last post by:
Hi, I'm (still) trying to return a pair<const Key, T> from iterator dereference. So I defined a proxy class in the obvious way: template<class KeyT, class DataT> struct ref_proxy { typedef...
275
by: Astley Le Jasper | last post by:
Sorry for the numpty question ... How do you find the reference name of an object? So if i have this bob = modulename.objectname() how do i find that the name is 'bob'
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.