DHooper

heres an example, i have four objects:
Object start;
Object mid1= start;
Object end;
Object mid2 = end;

using mid1 and mid2, how do i make start and end reference the same thing(ie, when i change start, end changes also) is this even possible

simply setting mid1 = mid2 does not work as it removes mid1's previous reference, essentially setting up this relationship:
start=mid1=mid2
end(unrelated)

any help would be greatly appreciated.


Re: Visual C# General Reference issue

marco.ragogna

If you have an object

Object obj;

and you want that two other objects referes to it, you can do the following:

Object obj1 = obj;

Object obj2 = obj;

But probably, I didn't get the problem, can you explain your purpose better





Re: Visual C# General Reference issue

OmegaMan

You never initialize up an object for start to reference such as

Object start = new Object();

now you have a reference to an object. For start before initialization is a reference that sits on the stack and in your example it refers to nothing. Let me explain




Re: Visual C# General Reference issue

DHooper

thanks for your reply,

im working on a node based editor (think mac's quartz composer)

it my previous example, start and end are variables that would be stored in 2 difference nodes while mid1 and mid2 are in a "jack" class. the jacks are children of the node and simplify the connection process. upon connecting the nodes, mid1 and mid2 are already referencing the parents nodes variables. this is hard to explain so heres a picture:

image

so the issue becomes how you change a variable without changing its reference.




Re: Visual C# General Reference issue

DHooper

look at my previous post - i explain it a little better - the real issue happens when you have a object already referencing something and you want that somthing to reference something_new via a change to object.




Re: Visual C# General Reference issue

Mark Dawson

Hi,

if I understand what you are asking correctly, you can achieve what you want by using the ref keyword to pass a parameter to a function by reference rather thn by value, therefore inside the function you can change what the original reference points to e.g.:

using System;

using System.Collections.Generic;

using System.Text;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

MyClass start = new MyClass("start");

MyClass end = new MyClass("end");

Foo(ref start, ref end);

//Output => start.Name=end, end.Name=end

Console.WriteLine("start.Name={0}, end.Name={1}", start.Name, end.Name);

}

private static void Foo(ref MyClass mid1, ref MyClass mid2)

{

mid1 = mid2;

}

}

class MyClass

{

private string name;

public MyClass(string name) { this.name = name; }

public string Name { get { return this.name; } }

}

}

Mark.






Re: Visual C# General Reference issue

Peter Ritchie

You can't do what you want with references, you need another level of abstraction. I suggest using a key and store the shared data in a collection of some sort, like a Dictionary.




Re: Visual C# General Reference issue

DHooper

i think Peter understands better what im doing,
i responded to your post on my other thread, if you could explain how your idea would work out with multiple data types and if it would be fast that would be great.




Re: Visual C# General Reference issue

DHooper

heres a better question that isn't so complex.

say i have this setup:

Bitmap first = Image.fromfile("someimage.jpg");
Bitmap second = first;

is it possible to do something like this:
second = new Bitmap(300,200);
and have it change first





Re: Visual C# General Reference issue

mwalts

That pretty much the same question, answer is still no, not directly.

Now if you contained that object in some sort of wrapper class then that is a different story

public Class MyNode
{
private Bitmap m_image;
public MyNode(Bitmap saveMe)
{
m_image = saveMe;
}
public Bitmap Image
{
get{ return m_image;}
set(m_image = value;}
}
}



Now something like this
Bitmap first = Bitmap.FromFile("someImage.bmp");
MyNode node = new MyNode(first);
//Now use node.Image as you would have used first
MyNode secondNode = node;

//Much later
secondNode.Image = new Bitmap(300,200);
//Now node.Image has changed as well

I did this all in this forums editor, so I don't promise it's 100% accurate but that is the general idea




Re: Visual C# General Reference issue

DHooper

mwalts, thanks a lot, that will work for me.