RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

What "new" means

_Epila_

Sorceror
What "new" means

Ive been thinking about what the real meaning of "new" in c#
the basic meaning that i have is when you use the new command it means a new location at the memory, like this example

Mobile a = new Mobile();
Mobile b = a;
b.Name = "foo";

this means, that i will create a new mobile a then set another reference to mobile a, wich will be called b, so when i set b.Name = "foo" it will also modify the Name in a, right?

so what i want, is to create 2 identical Mobiles, but with different names

Mobile a = new Mobile();
Mobile b = new Mobile();
b = a;
b.Name = "foo";

this will not work, because when you set b = a it will also reference the memory pointer from a to b

so, how i create 2 identical mobiles but with different names, not one Mobile with two references?
Will i have to copy each attribute like Name, Str..one by one ?
 

Jeff

Lord
_Epila_;861665 said:
Ive been thinking about what the real meaning of "new" in c#
the basic meaning that i have is when you use the new command it means a new location at the memory, like this example

Mobile a = new Mobile();
Mobile b = a;
b.Name = "foo";

this means, that i will create a new mobile a then set another reference to mobile a, wich will be called b, so when i set b.Name = "foo" it will also modify the Name in a, right?

so what i want, is to create 2 identical Mobiles, but with different names

Mobile a = new Mobile();
Mobile b = new Mobile();
b = a;
b.Name = "foo";

this will not work, because when you set b = a it will also reference the memory pointer from a to b

so, how i create 2 identical mobiles but with different names, not one Mobile with two references?
Will i have to copy each attribute like Name, Str..one by one ?

Yes, for RunUO you will need to copy each Property... however, there is a interesting concept in programming called Shallow Cloning and Deep Cloning. You can use these in C#, but you would have to implement them. It's not really something I have time to go into detail about, but here are some links explaining it.

Shallow Copy : Object.MemberwiseClone Method (System)
Deep Copy : C# Tutorial - Deep Cloning A Connected Graph Of Objects | Switch on the Code
 
Top