You have mixed up the definition and the declaration . The definition of a nested class does not instantiate. Also, the classes you define look as if they all use the same properties. So you have to 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 need to create 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. As noted earlier, declarations of 8 instances of the same class in a class may indicate a “bad” design. You can create a Dictionary<>
for it.
Here is the 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);