在C#中引用嵌套类对象

时间:2016-03-21 11:05:36

标签: c# string class nested

我希望有一个包含多个嵌套类的类,这样当我创建一个新的父类时,每个嵌套类的对象都会被创建,我可以全局引用每个嵌套类中的变量。

这是我目前的代码:

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 = ""; }

}

然后我用:

创建一个新的父对象
StockChecklist theCurrentList = new StockChecklist();

但是如何访问嵌套对象'tag','value'和'reference'?我希望像StockChecklist.qty1p1.tag ='changedval999999999'这样简单的东西;

C#可以这样吗?

3 个答案:

答案 0 :(得分:7)

您混淆了定义声明。定义嵌套类不会创建实例。您定义的类看起来都使用相同的属性。因此,您应该定义一个类并声明多个实例。

您可以使用以下方法解决此问题:

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 = "" };
}

在C#6.0之前,您必须在构造函数中创建实例。

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

注意:就像已经注意到的一些评论一样,在一个类中声明同一个类的8个实例可能会指向“糟糕”的设计。您可以为其创建Dictionary<>

这是一个字典版本:(奖金)

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

使用它:(C#6.0)

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

(C#6.0)

Trace.WriteLine(stock["uniqueval88356"]?.value);

答案 1 :(得分:0)

你应该这样做:

  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 = ""; }

public class StockChecklist
{
    public qty1p1 _qty1p1;
    public qty1p2 _qty1p2;
.
.
.
}

然后你可以像:

一样使用它
StockChecklist theCurrentList = new StockChecklist();
theCurrentList._qty1p1.tag = 'changedval999999999';

答案 2 :(得分:-1)

你可以这样做:

public class Parent
{

    public class child1 { public string name = "a"; public int Value = 1;}
    public class child2 { public string name = "b"; public int Value = 2;}
    public class child3 { public string name = "c"; public int Value = 3;}
    public class child4 { public string name = "d"; public int Value = 4;}
    public class child5 { public string name = "e"; public int Value = 5;}
    public child1 c1;
    public child2 c2;
    public child3 c3;
    public child4 c4;
    public child5 c5;
    public Parent() {
        this.c1 = new child1();
        this.c2 = new child2();
        this.c3 = new child3();
        this.c4 = new child4();
        this.c5 = new child5();
    }
}

class Program
{
    static void Main(string[] args)
    {

        Parent p1 = new Parent();
        Console.WriteLine(p1.c1.name);
        Console.WriteLine(p1.c2.name);
        Console.WriteLine(p1.c3.name);
        Console.WriteLine(p1.c4.name);
        Console.WriteLine(p1.c5.name);
        Console.ReadLine();

    }