Class reference affecting structure

Asked

Viewed 80 times

-2

I’m having a little problem and I can’t seem to solve it.

I have a class (matrix 0~999) called pTest, she is already instantiated everything straight.

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

For example:

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

If I do so in a method:

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

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

the pTest[1].Player.Name will also be worth "Grim".

The 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;
    }
}

remembering that if I change the property Mode of the object pTest[0], for example: pTest[0].Mode = 5, it makes no change in pTest[1].Mode.

How to solve this?

1 answer

0

Microsoft Docs:

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

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

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

To solve this you might have to implement a method Copy() in its structure, that knew 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 like 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:

https://stackoverflow.com/q/11336935/8133067
https://social.msdn.microsoft.com/Forums/en-US/633c7c7b-4167-4c90-835e-ee6e8b566156/
https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/structs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.