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..

2
  • I thought I am initializing the "part" and "member" by PartClass part = new PartClass(); and MemberClass member = new MemberClass(); inside DataClass. What am I missing?.. Commented Jan 15, 2020 at 16:05
  • No, you didn't initialize your properties, you defined new variables, and initialized them. It's part = new .., not PartClass part = new .., if you want it explicit, write this.part= new ... Commented Jan 15, 2020 at 16:17

3 Answers 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
        }
    }
};
Sign up to request clarification or add additional context in comments.

14 Comments

thank you very much - worked. Just a question, since I add as per your code the "part = new PartClass()" etc, why do I need the constructors in the first place? I mean, I could do the exact code by just having the classes without any link themeselves? Thank you indeed for your time and effort, appreciate..
Every class needs a constructor unless it is static or virtual.
@jdweng I agree, I asked because I have put all the constructors in the classes above. And a question, how to I access the class, I tried Console.WriteLine(myData.member.side.secondDetail); and cannot parse it (though your code compiles, minor typos fixed). Any ideas?
@Nick Nobody can put a constructor from one class in another class, what you mean is instance-creation. I gave you the hint below your question. You are not accessing your properties at all in the constructor of the container.
Oops accidentally deleted my comment... @jdweng sorry, you were right, you definitely need the constructors as you have them
|
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; } }

}

1 Comment

Today also just {get => _part ?? (_part = new PartClass()); }
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;

8 Comments

Thank you very much, worked as a charm. As a last favor, do you have any advices on the above implementation? have you spotted something trivial that I could have done better? your opinion matters. Thank you for your solution.
you should make the fields as privates and use set method to change value
There is still no myData.part = new PartClass(), it's impossible this works.
@Nick, change the constructor like this public DataClass() { part = new PartClass(); member = new MemberClass(); }
@Renato Coqueiro - impressive, however, I don't see the SideClass which is inside the MemberClass.. would you like to edit and let me get your approach?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.