Assign a string as a reference to another string within an object

0

I am somewhat confused trying to do the following and I hope you can help me.

I have a class with a series of strings:

public class Ejemplo{
    private string nombre;
    private string id;
    private string descripcion;
    ...
}

What I need is to clone this object but keeping the reference of certain strings to the original. For this I have a Clone() method that returns a copy of the object:

public object Clone(){
    Ejemplo ejemplo = new Ejemplo();
    ejemplo.nombre = this.nombre;
    ejemplo.id = this.id;
    ejemplo.descripcion = this.descripcion;
    ...

    return ejemplo;
}

And then there are other variables that if I initialize them as a new object since I want them to be independent (hence the copy). By doing it in this way I get the copy right, but by modifying the original object I can not get these changes to be replicated in the copy.

What I would want is that if I change the name or other strings in the original object it will also change in the copy. Right now he does this to me:

Ejemplo ejemplo = new Ejemplo();
ejemplo.nombre = "Hola";
Ejemplo ejemplo2 = ejemplo.Clone();
Console.WriteLine(ejemplo.nombre);   // Muestra "Hola"
Console.WriteLine(ejemplo2.nombre);  // Muestra "Hola"

ejemplo.nombre = "Adios";
Console.WriteLine(ejemplo.nombre);   // Muestra "Adios"
Console.WriteLine(ejemplo2.nombre);  // Muestra "Hola"

And what I would like to achieve is this:

Ejemplo ejemplo = new Ejemplo();
ejemplo.nombre = "Hola";
Ejemplo ejemplo2 = ejemplo.Clone();
Console.WriteLine(ejemplo.nombre);   // Muestra "Hola"
Console.WriteLine(ejemplo2.nombre);  // Muestra "Hola"

ejemplo.nombre = "Adios";
Console.WriteLine(ejemplo.nombre);   // Muestra "Adios"
Console.WriteLine(ejemplo2.nombre);  // Muestra "Adios"

I tried to assign it with ejemplo.name = String.Intern(this.name); but the same thing keeps happening to me.

Any idea how I could do this, what concept is failing me or is it possible to do it? Thanks in advance.

    
asked by Wyrncael 05.05.2018 в 21:05
source

2 answers

1

Try to create the following classes :

  • A reference class: In this class you will define all the properties you want to change via reference.
  • A class that only shares the reference of the original class: In this class you will simply define the class with which you will share the references and the properties that you only want to change with the reference of the class.

For example:

public class Data {

    public DataReference Reference { get; set; }
    public string Stage { get; set; }

    public Data(){
         this.Reference = new DataReference();
    }

    public Data ShallowCopy() {
         return this.MemberwiseClone() as Data;
    }
}

public class DataReference {
    public string Username { get; set; }
    public int Level { get; set; }
    public DataReference(){ }
}

The tests define the following output:

// Defines los valores iniciales antes de clonar el objeto original
Data data = new Data();
data.Reference.Username = "ZenzukyLzC";
data.Reference.Level = 150;
data.Stage = "Prologue";

Data cloneData = data.ShallowCopy(); // Clonas el objeto

Console.WriteLine(data.Reference.Username); // imprime "ZenzukyLzc"
Console.WriteLine(cloneData.Reference.Username); // Imprime "ZenzukyLzC"
Console.WriteLine(data.Reference.Level); // imprime 150
Console.WriteLine(cloneData.Reference.Level); // Imprime 150
Console.WriteLine(data.Stage); // imprime "Prologue"
Console.WriteLine(cloneData.Stage); // Imprime "Prologue"

data.Reference.Username = "Uzi-Zenzuky";
data.Reference.Level = 151;
data.Stage = "Stage one";

Console.WriteLine(data.Reference.Username); // imprime "Uzi-Zenzuky"
Console.WriteLine(cloneData.Reference.Username); // Imprime "Uzi-Zenzuky"
Console.WriteLine(data.Reference.Level); // imprime 151
Console.WriteLine(cloneData.Reference.Level); // Imprime 151
Console.WriteLine(data.Stage); // imprime "Stage One"
Console.WriteLine(cloneData.Stage); // Imprime "Prologue"

cloneData.Reference.Username = "NenoZky";
cloneData.Reference.Level = 152;
cloneData.Stage = "Stage Two";

Console.WriteLine(data.Reference.Username); // imprime "NenoZky"
Console.WriteLine(cloneData.Reference.Username); // Imprime "NenoZky"
Console.WriteLine(data.Reference.Level); // imprime 152
Console.WriteLine(cloneData.Reference.Level); // Imprime 152
Console.WriteLine(data.Stage); // imprime "Stage One"
Console.WriteLine(cloneData.Stage); // Imprime "Stage Two"

If you notice, the values that are in Reference change along with any reference of Data . While those that are typical of Data , only change in the particular object.

More info: Link

    
answered by 05.05.2018 / 23:18
source
1

To be able to pass values by reference or create variables or objects that are reference of other variables or values you need to use the keyword ref .

With the word ref you indicate reference at the moment of declaring variables or objects of classes.

This page of the Microsoft MSDN site tells us about the use of the word ref . They give us an example of how to use it with objects or structures:

ref VeryLargeStruct reflocal = ref veryLargeStruct;

Therefore, to pass one object as a reference to another and in this way handle the same variables, for example for this case, you can use this code:

Ejemplo ejemplo = new Ejemplo();
ejemplo.nombre = "Hola";
ref Ejemplo ejemplo2 = ref ejemplo;
Console.WriteLine(ejemplo.nombre);   // Muestra "Hola"
Console.WriteLine(ejemplo2.nombre);  // Muestra "Hola"

ejemplo.nombre = "Adios";
Console.WriteLine(ejemplo.nombre);   // Muestra "Adios"
Console.WriteLine(ejemplo2.nombre);  // Muestra "Adios"

UPDATE: To be able to pass variables as function parameters by reference you can use the reserved word out . out causes the arguments to be passed by reference, in this way you can modify your Clone() method so that it passes by reference an object and its attributes. For this you must do the static method and put as parameters your object to be cloned and the cloned object.

For example:

public static void Clone(Ejemplo ejemplo, out Ejemplo ejemplo2){
    // De esta manera asignarás los atributos del objeto que 
    // quieras que sean pasados por referencia.
    ejemplo2.nombre = ejemplo.nombre;
    ejemplo2.id = ejemplo.id;
    ejemplo2.descripcion = ejemplo.descripcion;
    ...
}

This should work to solve your problem.

Here is the Microsoft MSDN site page that talks about the use of ref and out :

ref (C # Reference)

Parameter modifier out (C # Reference)

    
answered by 05.05.2018 в 21:57