Celeste Soueid
Celeste Soueid

Reputation: 318

Field returning 0 while IDE debug shows it at the proper initialised value

I'm using reflection to create instances of all classes derived from ItemPart as follows:

        //Create UI from reflection
    private void DisplayParts(string partGroup)
    {
        List<ItemPart> validParts = GetPartsList(partGroup);
        foreach(ItemPart part in validParts)
        {
            GameObject obj = Instantiate(UIController.ObjectPrefabs[UIController.ObjectPrefabsEnum.ButtonDescription], UI_PartsList.transform);
            DescriptionButton button = obj.GetComponent<DescriptionButton>();
            button.Title.text = part.PartName;
            button.Description.text = part.Description;
            button.ActivateAction = delegate { DesignBench.CreatePart(part); };
        }
    }
    
    //Get classes derived from ItemPart and return a list of instances
    private List<ItemPart> GetPartsList(string partGroup)
    {
        List<Type> types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(s => s.GetTypes()).Where(p => typeof(ItemPart).IsAssignableFrom(p) && p != typeof(ItemPart) ).ToList();

        List<ItemPart> validParts = new List<ItemPart>();

        foreach(Type type in types)
        {
            ItemPart part = (ItemPart)Activator.CreateInstance(type);
            if (part.PartGroup == partGroup)
            {
                validParts.Add(part);
            }
        }
        return validParts;
    }

After accessing variables in this class, I've found while the IDE is claiming they are correctly at their initialised values, they are reading by the program as being 0:

public class Handle : ItemPart 
{
//...
        public new PartModifiableStats ModifiableStats = new PartModifiableStats()
        {
            SizeX = new ModifiableStat<float>() { IsEnabled = false, Value = 5 },
            SizeY = new ModifiableStat<float>() { IsEnabled = true, Value = 20, Bounds = new Vector2(10, 150) },
            Material = new ModifiableStat<Materials.Material>() { IsEnabled = true, Value = Materials.MaterialDict[Materials.MaterialTypes.Iron] }
        };
//...
}

https://puu.sh/GcS8h/9a3c26de2b.png https://puu.sh/GcS8L/4911fbd3d8.png

Supposedly, Activator.CreateInstance(type) uses the type's empty constructor, which if I'm not mistaken, should also properly initialise all the variables in the type it is creating. Why is this value still reading as zero, and why is there a discrepency between what the program is reading and what the IDE believes the value to be?

        public struct ModifiableStat<T>
        {
            public bool IsEnabled;
            public T Value;
            public Vector2 Bounds;
        }
        public struct PartModifiableStats
        {
            public ModifiableStat<float> SizeX;
            public ModifiableStat<float> SizeY;
            public ModifiableStat<Materials.Material> Material;
        }

Upvotes: 1

Views: 31

Answers (1)

TheGeneral
TheGeneral

Reputation: 81513

This is because you have not overriding PartModifiableStats, you are making a new property/field in the child class. Both values get initialized and they can be accessed via a cast.

Take this nonsensical example to see exactly what is happening

public class Parent
{
   public int Value { get; set; }= 10;
}

public class Child : Parent
{
   public  int Value { get; set; } = 20;
}

...

var part = new Child();

Console.WriteLine("Child Value = " + part.Value);
Console.WriteLine("Parent Value = " + ((Parent)part).Value);

((Parent)part).Value = 25;
  
Console.WriteLine("Child Value = " + part.Value);
Console.WriteLine("Parent Value = " + ((Parent)part).Value);

Output

Child Value = 20
Parent Value = 10
Child Value = 20
Parent Value = 25

What you are likely after is something more like this. Where the value is overridden and the same value persists with the cast

public class Parent
{
   public virtual int Value { get; set; }= 10;
}

public class Child : Parent
{
   public override int Value { get; set; } = 20;
}

Output

Child Value = 20
Parent Value = 20
Child Value = 25
Parent Value = 25

Upvotes: 2

Related Questions