First of all, this is my first post to this forum, so before I start I would like to thank all of you that contribute their time and expertise toward helping others!
I am trying to figure out how to get a reference parameter to an object passed to a form class constructor and have access to it within other methods in the form class.
I can get the access to the objects' address in the constructor method using ref and modify the original object there with no problem. What I really need to do is get the same access to the object in the other methods in the form class. Seems no matter what I do this is impossible. Pointers variables are not allowed to point to reference types correct? I must be overlooking something very obvious as this must be a very common scenario. Any help would be much appreciated.
Bob
Loading
Robert WellsPosted May 17, 2010, 7:00 PM
Yes, the parameter I am sending is an instance of a class and therefore it is a reference type and the address of the object is sent. I can therefore modify the members of that instance in the called function and the changes are made to the original instance.
What I was trying to do at first without using ref was to create a new instance of the class in the called function and assign it directly to the original instance. This did not work (I think) because although I was assigning the address of the new instance to the address of the old instance the variable holding the address of the old instance in the calling function still had the same old address. I remind you this is what I think was going on.
Then I tried using ref on the parameter. I see this as a refererence to a reference to my object. Therefore i am getting the address of the variable in the calling function that holds the address of the object. When I assign the address of my new instance in the called function to this voila! The variable in my calling function actually points to the new instance. That is what I think is happening anyhow and if anyone knows this to be incorrrect please please let me know!
So i now can create a new instance of the class in my called function and assign that intance to the variable I called the function with. The problem is this happens in the constructor function when I have not selected which object I need to return. I need to do the assignment in a onButtonClicked function after I have selected the item I want to return. I don't know to get the access I need to the passed reference outside of the constructor. Use the address operator and unsafe section?? I instead found an article on passing objects using delegates and that works fine but I'm not sure what is the best (safest and cleanest) approach to use. Does anyone have any ideas or completely different approaches?
Thank you,
Bob
Jaish MathewsPosted May 15, 2010, 3:49 AM
If I didn't misunderstand you, C# is passing arguments byref Default. Below is the example to prove that.
private void Form1_Load(object sender, EventArgs e)
{
Dictionary<string, string> coll = new Dictionary<string, string>();
coll.Add("1", "One");
foreach (string s in coll.Values)
{
MessageBox.Show(s, "Before Modification");
}
Modify(coll);
foreach (string s in coll.Values)
{
MessageBox.Show(s, "After Modification");
}
}
private void Modify(Dictionary<string, string> col)
{
col.Add("2", "Two");
}