|
If I write following code:
int i = 1;
object o = i;
object a = o;
Is 'o' a reference to 'i' or
is 'o' a copy of 'i' ???
Is 'a' a reference to 'o' or
is 'a' a copy of 'o' ???
(Sorry for my bad english )
Thanks for every answer!
|
|
|
|
|
jb_dani wrote:
Sorry for my bad english
It's fine.
int i = 1;
object o = i;
Console.WriteLine(o.ToString());
i=10;
Console.WriteLine(o.ToString());
Console.ReadLine();
Output is: "1 \n 1"
The "i" value is copied to object "o", so there is no longer a reference between the two.
HTH
Cheers,
Simon
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
|
|
|
|
|
Thanks, but i a second question (sorry)...
What's with the following code:
Test t1 = new Test();
Test t2 = t1;
What is 't2'? Is it also a copy of t1 or a reference?
Thanks!!!
MFG Daniel.
|
|
|
|
|
I'm not sure what those classes are, but:
class Test1
{
public int _i =-1;
public Test1(int i)
{_i=i;}
public override string ToString()
{return _i.ToString();}
}
Test1 t1 = new Test1(100);
Test1 t2 = t1;
Console.WriteLine(t1.ToString());
t2._i=200;
Console.WriteLine(t2.ToString());
Console.WriteLine(t1.ToString());
Console.ReadLine(); Proves that t2 and t1 point to the same location in mem.
Does that help you?
Cheers,
Simon
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
|
|
|
|
|
I how can i make a full copy of a object, so that when i change the member in object 1, the member in object 2 will not be changed???? So, as a deep copy in c++!!!
MFG Daniel.
|
|
|
|
|
I'm pretty sure there's a copy and a clone method for this type of thing, but I have a feeling that you're going to have to implement it yourself.
I'll have a look though.
modified: do a search for ICloneable.Clone Method in MSDN. The ICloneable interface looks like what you're looking for.
HTH
Cheers,
Simon
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
|
|
|
|
|
To get a deep copy in C# do this:
public static object Clone(object objectToClone)
{
if (objectToClone == null)
{
throw new ArgumentNullException("objectToClone", "Parameter can not be null.");
}
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream stream = new MemoryStream();
formatter.Serialize(stream, objectToClone);
stream.Seek(0, SeekOrigin.Begin);
return formatter.Deserialize(stream);
}
To deep copy an object with this method, use:
MyTest originalObject = new MyTest(a, lot, of, stuff);
MyTest cloneObject = (MyTest)Clone(originalObject);
The best part is that this is generic: extend MyTest with properties and you don't have to change the clone method.
Øyvind
|
|
|
|
|
SimonS wrote:
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
This beats them all.
|
|
|
|
|
O will be an Object that points to a new System.Int32 with the value of i, and if you change the value of O to 2, i will still be 1.
So the short answer is that it is a copy.
/Bo
|
|
|
|
|
And the follwing code:
Test t1 = new Test();
Test t2 = t1;
What is 't2'? Is it also a copy of t1 or a reference?
Thanks!!!
MFG Daniel.
|
|
|
|
|
Both t1 and t2 will be references to the new Test object created. No copy is made - and yes they are "pointers" - even if somebody tells you that C# ´don't have pointers.
/Bo
|
|
|
|
|
have you heard about boxing and unboxing?
i'm not 100% but pretty sure it goes like this:
* i is an int and placed on the stack.
* o is an object and therefore placed on the heap - i is "boxed" and the boxed copy is referenced by o - i believe, o is a COPY of i
* you can check with object.ReferenceEquals(a,o) that a and o point to the same object
as said before - i'm not 100 percent sure about it...(still a beginner to C# and .NET)
:wq
|
|
|
|
|
Oh boy am I tired, but glad I read your post because I was just getting ready to respond with the explanation you gave.
To my knowledge, any time you instansiate an object of a class in C# you are doing so on the heap, however when you use static methods, objects aren't required for this and therefore they process on the stack. Hopefully I am not mumbling here as I have been up for way to long.
HTH
Nick Parker
|
|
|
|
|
my experiences and deductions lead me to believe that any class inheriting from object is passed by reference.
READ MSDN
|
|
|
|
|
int and string both inherit from object, but are passed by val.
I think it has more to do with boxing than their hierarchy. I could be wrong though.
Cheers,
Simon
"Sign up for a chance to be among the first to experience the wrath of the gods.", Microsoft's home page (24/06/2002)
|
|
|
|
|
int is passed by value; string is passed by reference (but strings are immutable).
Any class that inherits from ValueType is passed by value; any other class is passed by reference. A struct is a special kind of class, it inherits from ValueType.
James
"Java is free - and worth every penny." - Christian Graus
|
|
|
|
|
To clear it all up since there have been either incorrect or misleading statements mentioned.
o is a reference to a copy of i; a is a reference to whatever o is referencing.
Here is a small program which illustrates that
using System;
namespace test
{
public class Test
{
public static void Main(string [] args)
{
int a = 1;
object o = a;
object o2 = o;
Console.WriteLine("a = {0}, o = {1}, o2 = {2}", a, o, o2);
a = 2;
Console.WriteLine("a = {0}, o = {1}, o2 = {2}", a, o, o2);
o = 3;
Console.WriteLine("a = {0}, o = {1}, o2 = {2}", a, o, o2);
}
}
} The output should be
a = 1, o = 1, o2 = 1<br />
a = 2, o = 1, o2 = 1<br />
a = 2, o = 3, o2 = 1
HTH,
James
"Java is free - and worth every penny." - Christian Graus
|
|
|
|
|
Hi James, sorry, but i have a question to you about your sample program.
In your sample program is 'a' an 'int' with the value '1' and 'o' a reference to a copy of 'a', is that right? And 'o2' is a reference to 'o', is that also right?
But why is the last 'Console.WriteLine' Function the output value of 'o2' '1' and not '3' ???
I don't understand why the value of 'o2' is '1' and not '3' like the value of 'o'.
(Sorry for my bad english )
MFG Daniel.
|
|
|
|
|
jb_dani wrote:
Sorry for my bad english
Thats ok, I was able to read everything just fine
jb_dani wrote:
In your sample program is 'a' an 'int' with the value '1' and 'o' a reference to a copy of 'a', is that right?
Correct.
jb_dani wrote:
And 'o2' is a reference to 'o', is that also right?
What happens here would be much clearer if we had the C++ syntax.
object *o, *o2;
int i;
i = 1;
o = __box(i);
o2 = o;
i = 3;
o = __box(i);
If you don't understand C++ I hope the comments clarify it for you
James
"Java is free - and worth every penny." - Christian Graus
|
|
|
|
|
Thank you very much!
(I am happy that my english is not so bad!)
MFG Daniel.
|
|
|
|
|
I had no any c# experience, but some java
maybe sometime copy, sometime reference
eg, reference:
----------------------------------------------
CMyCustObj i = new CMyCustObj()
object o = i;
object a = o;
---------------------------------------------
you can compare the address of i,o,a
|
|
|
|
|
hi,
i have a connection dialog and put some labels in there. there's a picturebox in front of each label with an icon signaling that the operation described in the corresponding label has been finished. should look something like:
* connecting
* receiving
* saving
copying
calculating
etc etc
this should tell the user, that the first three activities were finished and the fourth is in progress (it's all a little niftier, but you get the point).
now the problem: i have the function, that does all the operations in sequence and activates the pictures appropriately. but they don't show. only after all operations have been finished, all symbols are shown. i've set the pictures (which are of type System.Windows.Forms.PictureBox) to visible=false in the dialog-editor and just turn them on with visible=true during processing. when i enable/disable the corresponding labels, they DO change their state on the fly - so there IS some dialog-drawing function active in the background - why doesn't it draw the pictures, when it is able to redraw the labels?
moreover: i've put the operation-function in a different thread and signaled the eventchange via event/delegate to the dialog but it just doesn't help.
any suggestions? thank you boys (and girls?) i rely on you
Edit:
if someone wants to check out a minimum-demo-project about my problem he can download and try this
|
|
|
|
|
hmm
simply calling the forms Update() function solves the problem.
but why is the Label.Enabled-setter calling it itself and the PictureBox.Visible-setter doesn't?
anyways.
thx everyone for reading...
:wq
|
|
|
|
|
Hi there
Changing the pictures should not be a problem if you have events firing for your "functions". So if there are no events defined for them yet, just create an event for each "function". I did this a while back to show diferent pictures for "Offline, Connecting, Online". Pretty easy stuff once you grasp events.
Hope this helps
READ MSDN
|
|
|
|
|
ok. thx. would be another idea to split the long sequence into seperate functions. but i have to use a lot of variables in all of the parts of this long sequence - so splitting it up would result in a lot of !=null testing of class-members which only have the task to pass data from one private function to another ...
hmm. too much overhead in the code i think...
but thx anyway for the idea
:wq
|
|
|
|