Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing nested class objects in C#

I want to have a class with several nested classes, so that when I create a new parent class, an object of each nested class is created and I can reference the variables within each nested class globally.

Here is my current code:

public class StockChecklist
{
  public class qty1p1 { public string tag = "uniqueval23456"; public string value = ""; public string reference = ""; }
  public class qty1p2 { public string tag = "uniqueval3736"; public string value = ""; public string reference = ""; }

  public class qty2 { public string tag = "uniqueval97357"; public string value = ""; public string reference = ""; }

  public class qty3p1 { public string tag = "uniqueval88356"; public string value = ""; public string reference = ""; }
  public class qty3p2 { public string tag = "uniqueval62346"; public string value = ""; public string reference = ""; }
  public class qty3p3 { public string tag = "uniqueval09876"; public string value = ""; public string reference = ""; }
  public class qty3p4 { public string tag = "uniqueval62156"; public string value = ""; public string reference = ""; }

  public class qty4 { public string tag = "uniqueval25326"; public string value = ""; public string reference = ""; }

}

then I create a new parent object with:

StockChecklist theCurrentList = new StockChecklist();

but how do access the nested objects 'tag', 'value' and 'reference'? I was hoping for something simple like StockChecklist.qty1p1.tag = 'changedval999999999';

Is something like this possible with C#?

like image 564
Lee Avatar asked Mar 21 '16 11:03

Lee


1 Answers

You mixed up definition and declaration. Defining a nested class doesn't create an instance. Also the classes you define looks like they all use the same properties. So, you should define one class and declare multiple instances.

You can fix this with:

C# 6.0

public class Info
{
    public string tag { get; set; }
    public string value { get; set; }
    public string reference { get; set; }

}

public class StockChecklist
{
    public Info qty1p1 { get; } = new Info { tag = "uniqueval23456", value = "", reference = "" };
    public Info qty1p2 { get; } = new Info { tag = "uniqueval3736", value = "", reference = "" };

    public Info qty2 { get; } = new Info { tag = "uniqueval97357", value = "", reference = "" };

    public Info qty3p1 { get; } = new Info { tag = "uniqueval88356", value = "", reference = "" };
    public Info qty3p2 { get; } = new Info { tag = "uniqueval62346", value = "", reference = "" };
    public Info qty3p3 { get; } = new Info { tag = "uniqueval09876", value = "", reference = "" };
    public Info qty3p4 { get; } = new Info { tag = "uniqueval62156", value = "", reference = "" };
    public Info qty4 { get; } = new Info { tag = "uniqueval25326", value = "", reference = "" };
}

Pre C# 6.0 you have to create the instances in the constructor.

public class StockChecklist
{

    public StockChecklist()
    {
        qty1p1 = new Info { tag = "uniqueval23456", value = "", reference = "" };
        qty1p2 = new Info { tag = "uniqueval3736", value = "", reference = "" };

        qty2 = new Info { tag = "uniqueval97357", value = "", reference = "" };

        qty3p1 = new Info { tag = "uniqueval88356", value = "", reference = "" };
        qty3p2 = new Info { tag = "uniqueval62346", value = "", reference = "" };
        qty3p3 = new Info { tag = "uniqueval09876", value = "", reference = "" };
        qty3p4 = new Info { tag = "uniqueval62156", value = "", reference = "" };
        qty4 = new Info { tag = "uniqueval25326", value = "", reference = "" };
    }

    public Info qty1p1 { get; private set; }
    public Info qty1p2 { get; private set; }

    public Info qty2 { get; private set; }

    public Info qty3p1 { get; private set; }
    public Info qty3p2 { get; private set; }
    public Info qty3p3 { get; private set; }
    public Info qty3p4 { get; private set; }
    public Info qty4 { get; private set; }
}

note: Like some comments already noted, declaring 8 instances of the same class within a class could point on 'poor' design. You could create a Dictionary<> for it.


Here is a dictionary version: (bonus)

public class Info
{
    public string tag { get; set; }
    public string value { get; set; }
    public string reference { get; set; }

}

public class StockChecklist
{
    private Dictionary<string, Info> _infoDict = new Dictionary<string, Info>();

    private void AddToDict(Info info)
    {
        _infoDict.Add(info.tag, info);
    }

    public StockChecklist2()
    {
        AddToDict(new Info { tag = "uniqueval23456", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval3736", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval97357", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval88356", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval62346", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval09876", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval62156", value = "", reference = "" });
        AddToDict(new Info { tag = "uniqueval25326", value = "", reference = "" });
    }

    public bool TryGetByTag(string tag, out Info info)
    {
        return _infoDict.TryGetValue(tag, out info);
    }

    public Info this[string tag]
    {
        get
        {
            Info info;

            if (!_infoDict.TryGetValue(tag, out info))
                return null;

            return info;
        }
    }
}

Use it like: (C# 6.0)

StockChecklist stock = new StockChecklist();
Info info;
if (stock.TryGetByTag("uniqueval23456", out info))
{
    Trace.WriteLine($"{info.tag} = {info.value}");
}

Or (C# 6.0)

Trace.WriteLine(stock["uniqueval88356"]?.value);
like image 88
Jeroen van Langen Avatar answered Oct 16 '22 15:10

Jeroen van Langen