Class reference affecting structure

-2

I have a small problem and I can not resolve it.

I have a class (array 0 ~ 999) called pTest , it is already instantiated all right.

After I instantiate I copy a structure that is from the player (contains name, level information, etc.) of another class to bind within pTest .

For example:

pTest[0].Player = g_kNPCGener.kMonster[0].MOB;

If I do this in a method:

pTest[0].Player = g_kNPCGener.kMonster[0].MOB;
pTest[1].Player = g_kNPCGener.kMonster[0].MOB;

pTest[0].Player.Name = "Grim";

pTest[1].Player.Name will also be set to "Grim".

Structure pTest[0].Player is the same structure (struct) of Monster.List[0].NPC ;

public class TmController 
{
    public MobTest[] pTest = new MobTest[1000];

    // methods..

    public void init()
    {
        pTest[0].Player = g_kNPCGener.kMonster[0].MOB;

        pTest[0].Mode = 1;

        pTest[1].Player = g_kNPCGener.kMonster[0].MOB;

        pTest[1].Mode = 1;
    }

    public TmController()
    {
        for (int i = 0; i < pTest.Length; i++)
        {
            pTest[i] = new MobTest();
        }
    }
}

public class MobTest : TmController
{
    public STRUCT_MOB Player;

    public int Mode;

    public MobTest()
    {
        Player = STRUCT_MOB.Clear();

        Mode = 0;
    }
}

If you change the Mode property of the object pTest[0] , for example: pTest[0].Mode = 5 , it does not change pTest[1].Mode .

How do I fix this?

    
asked by anonymous 15.10.2018 / 16:49

1 answer

0

From Microsoft Docs:

  

In C # there are two types of values, the "reference types" ( reference types ) and the" value types "( value types ). Reference type variables store references to your data (objects), while value type variables directly contain your data.

Structs are value types, unlike classes, which are reference types, which means that assigning the value of a variable of type struct to another variable of the same type, the values of this structure will be copied to the new variable, but , only structure members that are also value types will be copied, in the case of structure members that are reference types, only the object reference will be copied.

What happens is that type string is a reference type, not a value type, so by "copying" a structure that contains members of type string from one variable to another, you are only by copying the reference of its string field, which means that both structures refer to the same string object.

To solve this, you might have to implement a Copy() method in your structure, which would know how to make a new copy of the structure correctly. Something like this:

public struct STRUCT_MOB
{
   public int X, Y;
   public string Name;

   public STRUCT_MOB Copy()
   {
      STRUCT_MOB newMob = this;
      newMob.Name = string.Copy(this.Name);
      return newMob;
   }
}

So instead of doing this:

    pTest[0].Player = g_kNPCGener.kMonster[0].MOB;
    pTest[0].Mode = 1;
    pTest[1].Player = g_kNPCGener.kMonster[0].MOB;
    pTest[1].Mode = 1;

You would do so:

    pTest[0].Player = g_kNPCGener.kMonster[0].MOB;
    pTest[0].Mode = 1;
    pTest[1].Player = g_kNPCGener.kMonster[0].MOB.Copy();
    pTest[1].Mode = 1;

Sources:

  

link
link
link

    
15.10.2018 / 17:21