0

I have the following system: a "mother" class called DataClass, has properties and two of them are classes, PartClass and MemberClass. In particular, MemberClass has properties and one of them is a class, SideClass - with its own properties.

Here is the code sample:

using System;

namespace ConsoleApp
{
    public class DataClass
    {
        public int num { get; set; }
        public string code { get; set; }
        public PartClass part { get; set; }
        public MemberClass member { get; set; }

        public DataClass()
        {
            PartClass part = new PartClass();
            MemberClass member = new MemberClass();
        }
    }

    public class PartClass
    {
        public int seriesNum { get; set; }
        public string seriesCode { get; set; }

        public PartClass() { }
    }

    public class MemberClass
    {
        public int versionNum { get; set; }
        public SideClass side { get; set; }

        public MemberClass()
        {
            SideClass side = new SideClass();
        }
    }

    public class SideClass
    {
        public string firstDetail { get; set; }
        public string secondDetail { get; set; }
        public bool include { get; set; }

        public SideClass() { }
    }

Now, I am trying to initialize the DataClass and assign values to all properties, and this doesn't work. So, the DataClass "owns" the PartClass and the MemberClass and the MemberClass itself "sees" the SideClass which is the bottom class and sort of independent of all. Here the rest of code:

class Program
{

    static void Main(string[] args)
    {
    DataClass myData = new DataClass()
    {
        num = 13,
        code = "message",
        //from here downwards nothing works; error..
        part.seriesNum = 1,
        part.seriesCode = 7,
        member.versionNum = 9,
        member.side.firstDetail = "pass",
        member.side.secondDetail = "checked",
        member.side.include = true;
    }
}

}

I thought that by installing constructors and properties the DataClass instantiation would not have problems, but actually the DataClass does not see any non-trivial properties (properties referring to classes). Could someone help me please? Thank you..

4

3 回答 3

3

Use following :

DataClass myData = new DataClass()
{
    num = 13,
    code = "message",
    part = new PartClass()
    {
        seriesNum = 1,
        //here down nothing works; error
        seriesCode = "abc"
    },
    member = new MemberClass()
    {
        versionNum = 9,
        side = new Side()
        {
            firstDetail = "pass",
            secondDetail = "checked",
            include = true
        }
    }
};
于 2020-01-15T16:07:57.333 回答
2

To not have to create the instance of Member Class and Part Class manually every time, you can create the instances using get and set methods.

public class DataClass
{
    public int num { get; set; }
    public string code { get; set; }

    private PartClass _part;
    public PartClass part { get { if (_part == null) _part = new PartClass(); return _part; } set { _part = value; } }


    private MemberClass _member;
    public MemberClass member { get { if (_member == null) _member = new MemberClass(); return _member; } set { _member = value; } }

}
于 2020-01-15T16:22:50.877 回答
0

you should create an instance of DataClass then initialize fields

DataClass myData = new DataClass();

myData.num = 13,
myData.code = "message",
myData.part.seriesNum = 1,
myData.part.seriesCode = 7,
myData.member.versionNum = 9,
myData.member.side.firstDetail = "pass",
myData.member.side.secondDetail = "checked",
myData.member.side.include = true;
于 2020-01-15T16:08:44.250 回答