开发者

How to fill up a nested class?

I have the following class:

publi开发者_如何学编程c class ExternalClass
{
    public string Name {get;set;}

    public class InternalClass
    {
        public int SomeNumber {get;set}
    }
}

How do I insert data, for example the number 123, into the property SomeNumber of InternalClass?

Do I need to pass things through the constructor of ExternalClass? Do I need to create a property that points out to this.InternalClass and have it instantiated, the InternalClass, in the constructor for ExternalClass?

Is it also possible to do this with object initializers somehow?


Remember that a class is a type, not an object.

I suspect that you tried to do something like this and it didn't work:

var x = new ExternalClass();
x.InternalClass.SomeNumber = 5;

If this is the case, then you were envisioning InternalClass as representing some complex property of the ExternalClass type. But in fact it is its own type: ExternalClass.InternalClass. The fact that it's nested within another class is simply a language feature, which exists for the purpose of allowing you to design classes whose logic can be encapsulated completely within the scope of an existing type.

So you were on the right track when you asked this:

Do I need to create a property that points out to this.InternalClass and have it instantiated, the InternalClass, in the constructor for ExternalClass?

That would certainly work. Then you'd have:

public class ExternalClass
{
    // Here you are definining a type.
    public class InternalClass
    {
        public int SomeNumber {get;set}
    }

    // And here are some properties providing access to
    // actual objects...
    public string Name { get; set; }

    // ...including an instance of the type you defined
    // above.
    public InternalClass InternalInstance { get; private set; }

    public ExternalClass()
    {
        InternalInstance = new InternalClass();
    }
}

Usage:

var x = new ExternalClass();
x.Name = "Nyla";
x.InternalInstance.SomeNumber = 5;

Also, in response to this question:

Is it also possible to do this with object initializers somehow?

Absolutely. However, this won't work:

var x = new ExternalClass
{
    Name = "Nyla",
    InternalInstance.SomeNumber = 5 // illegal
};

What you would have to do is either make the set for your InternalInstance property public (inadvisable):

var x = new ExternalClass
{
    Name = "Nyla",
    InternalInstance = new ExternalClass.InternalClass
    {
        SomeNumber = 5;
    }
};

Or, better, add a property to ExternalClass that points to the SomeNumber property of its InternalInstance property:

// added to ExternalClass
public int InternalNumber
{
    get { return InternalInstance.SomeNumber; }
    set { InternalInstance.SomeNumber = value; }
}

Then you could do this:

var x = new ExternalClass
{
    Name = "Nyla",
    InternalNumber = 5
};

Up to you.


It depends what you're trying to do. You could access it directly, for example:

ExternalClass.InternalClass dodgy = new ExternalClass.InternalClass();
dodgy.SomeNumber = 6;

However, this would be breaking the law of demeter, and is generally a bad idea. is there a reason InternalClass is public and exposed to the world? You might want to try making InternalClass private and delegating to it, something like this:

public class ExternalClass
{
    private InternalClass numberHolder;

    public ExternalClass() {
        numberHolder = new InternalClass();
    }

    public int MyNumber {
        get {
            return numberHolder.SomeNumber;
        }

        set {
            numberHolder.SomeNumber = value;
        }
    }

    private class InternalClass
    {
        public int SomeNumber { get; set; }
    }
}

Then you can do this:

ExternalClass numberHolder = new ExternalClass();
numberHolder.MyNumber = 3;

(Kind of pointless in this scenario but hopefully you get the idea).


External class needs to hold a reference to the internal class (if that is what you ar etrying to accomplish.

public class ExternalClass
{
    public string Name {get;set;}
    public InternalClass IClass{get;set;}

    public class InternalClass
    {
        public int SomeNumber {get;set}
    }
}

If you want to initialize the InternalClass while you create an object of the ExternalClass, you will have to provide a constructor as far as I know. Object initializers only let you initialize a objects properties. Not properties of a property.

With constructors the class can look like this:

public class ExternalClass
{
    public string Name {get;set;}
    public InternalClass IClass{get;set;}

    public ExternalClass( int number){
        IClass = new InternalClass(){ SomeNumber = number};
    }
    public class InternalClass
    {
        public int SomeNumber {get;set}
    }
}

And then, you can initialize the entire object like this:

ExternalClass eclass = new ExternalClass( somenumber){Name = name};

I would prefer a constructor that takes both somenumber and name, but that's a matter of preference.

ExternalClass can also contain a List of InteralClass object if you want to.


You can use the singleton pattern to instantiate the class when the class is needed.

public class ExternalClass
{
    public string Name {get;set;}
    private InternalClass m_NestedClass = null;
    public InternalClass
    {
      get
      {
           if (m_NestedClass == null) m_NestedClass = new InternalClass();
           return m_NestedClass;
      }
    }

    public class InternalClass
    {
        public int SomeNumber {get;set}
    }
}


public class ExternalClass
{ 
    private InternalClass m_NestedClass = new InternalClass();

    public InternalClass
    {
      get
      {
           return m_NestedClass;
      }
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜