È mescolato definizione e dichiarazione. La definizione di una classe nidificata non crea un'istanza. Anche le classi che definisci hanno l'aspetto di utilizzare tutte le stesse proprietà. Quindi, dovresti definire una classe e dichiarare più istanze.
È possibile risolvere questo problema con:
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 è necessario creare le istanze nel costruttore.
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; }
}
nota: Come alcuni commenti già notato, dichiarando 8 istanze della stessa classe all'interno di una classe potrebbe puntare sul design 'poveri'. Potresti creare un Dictionary<>
per questo.
Ecco una versione dizionario: (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;
}
}
}
utilizzarlo come: (C# 6,0)
StockChecklist stock = new StockChecklist();
Info info;
if (stock.TryGetByTag("uniqueval23456", out info))
{
Trace.WriteLine($"{info.tag} = {info.value}");
}
O (C# 6.0)
Trace.WriteLine(stock["uniqueval88356"]?.value);
E 'molto chiaro il motivo per cui * * avete tutte le classi nidificate cripticamente-e-non convenzionale-nome. Puoi dare più contesto per ciò che stai cercando di ottenere?È molto probabile che esista una soluzione migliore. Si noti che la creazione di un'istanza della classe esterna * non * crea automaticamente un'istanza di qualsiasi classe nidificata. –
Puoi trovare la risposta qui https://msdn.microsoft.com/en-us/library/ms173120.aspx –
Puoi usare una classe Dizionario invece di creare 6 classi che hanno lo stesso scopo che è quello di memorizzare alcuni valori –