I need to store both common parameters and user parameters in one list. For this, I use nested classes. Here for example

class Parameters { public class UserParameters { string testUserParameters; } public class AllPrameters { string testAllParameters; } } 

But when I create an object of class Parameters. I can not assign him the parameters of nested classes. For example, testUserParameters

 class Test { static void x() { Parameters g = new Parameters(); } } 

What to do? If it is not clear why I am doing this, I explain. I need to store the program parameters in a single configuration file.

Example: let's say I need to keep company employees. I keep general data in one configuration file: number of employees, number of departments, name of all departments ...

And also store data about each person. Name, department, age ... And here I create an object when I need general data. I fill in the object with only general data, and when I fill in the data for an employee, I fill in the object with only data for an employee and store it in the general list.

  • There is no field or property in the Parameters class. So what exactly do you want to assign? - Grundy
  • I want to assign "g" options to nested classes. Here's how to do it? - polsok
  • you create only an instance of the Parameters class. There are no fields in this class. Accordingly, there is nothing to appoint - Grundy

4 answers 4

You have described the subclasses, but only, you yourself will declare them as members of the class at least for a start:

 class Parameters { public class UserParameters { string testUserParameters; } public class AllPrameters { string testAllParameters; } public UserParameters up; public AllPrameters ap; } 
  • thanks, it didn't get to me myself - polsok

I do not quite understand why doing nested classes, or rather what benefits you will get from this.

In your case, I would do this:

 public class UserParameters { public string testUserParameters { get; set; } } public class AllPrameters { public string testAllParameters { get; set; } } public class Parameters { public Parameters() { userParameters = new UserParameters(); allPrameters = new AllPrameters(); } public UserParameters userParameters; public AllPrameters allPrameters; } 

//call

 Parameters g = new Parameters(); g.userParameters.testUserParameters = "test"; 

    Members of your nested classes are not exposed to visibility modifiers. That is, by default they are private . Accordingly, you cannot access these members from the outside, including from the external class Parameters (the external class does not have access to private members of the internal, while the internal one can access private members of the external class).

    In general, the question arises of the need for nested classes in this case. Nested classes are a rather specific entity, and the real need for them occurs infrequently. Try yourself to answer the question: what exactly in your case can be implemented (or easier to implement) using nested classes, but not using ordinary, non-nested? I suspect nothing

    • changed all parameters to public did not help - polsok
    • @polsok and what exactly is happening? Compile, runtime errors or what? - DreamChild
    • Well, you yourself can run it in the visual. When you try to fill the parameters "g". You will not jump out any options for nested classes. - polsok
    • @polsok you now actually have one empty class containing two almost empty nested classes. You have no initialization code for these nested classes, no constructors, no methods. In the code you give you create an instance of the outer class and nothing else. You don’t have any initialization there at all, except for invoking the default constructor, respectively, you don’t have these "parameters" from your "g" from anywhere (he and the "parameters" have no way). - DreamChild
    • ok, can you fix my example? - polsok

    Are you talking about the list, would you like IDictionary? That is, name / value pairs for parameters.

     class Parameters { IDictionary<string, string> _userParameters; IDictionary<string, string> _allParameters; //... } 

    UPD:

    First, now you do not have lists.

    Secondly, why do you need inner classes? They are usually used to hide some service types within the public type. If aggregation is needed, then it is done differently - through the membership of instances of one type in another. (this is not javascript)

    Thirdly, there is no direct relationship “one type - one file”; affinity for (de) serialization is expressed through aggregation.

     class A { } class B { } class C { public A a { get; set; } public B b { get; set; } } var jsonData = File.ReadAllText("myFile.json"); var c = JsonConvert.DeserializeObject<C>(jsonData); 
    • I can not imagine how it can be used here. Added an example in the question for a better understanding - polsok
    • @polsok Added something. - free_ze