views:

3456

answers:

6

Hello. I a little problem.

Assuming we have a class InnerClass with attributes and getter/setter. We also have a class OuterClass containing the InnerClass.

e.g.

class InnerClass
{
    private int m_a;
    private int m_b;

    public int M_A
    {
        get
        {
             return m_a;
        }
        set
        {
             m_a = value;
        }
     }
}

class OuterClass
{
    private InnerClass innerClass
}

How would I implement a correct getter and setter for the innerClass member of OuterClass?

Thanks in advance!

+14  A: 

The syntax wouldn't be any different. Just...

public InnerClass InnerClass
{
    get { return innerClass; }
    set { innerClass = value; }
}

By the way, if you're using C# in .NET 3.5, you can use the automatic property generation feature if all you have is a simple property that just reads and writes to a backing store (like you have above). The sytax is similar to that of an abstract property:

public InnerClass InnerClass { get; set; }

This automatically generates a private member for storage, then reads from it in the get and writes to it in the set.

Adam Robinson
+1  A: 
public InnerClass InnerClass
    {
        get
        {
             return innerClass;
        }
        set
        {
             innerClass = value;
        }
     }
edosoft
A: 

It depends on how the inner class should work. The outer class might need to "own" the inner class, in which case:

public InnerClass InnerClass{
  get{ return innerClass; }
  set{ innerClass.CopyFrom(value); /* Pseudo method call */ }
}

By doing it this way, you prevent outside code from manipulating the instance unless explicitly through OuterClass..

James
Not really, since you're returning the instance in the getter. The only real way to do this (and this approach is a little smelly...) would be to use the same CopyFrom method to return a new instance in the getter.
Adam Robinson
I think you misunderstood. The point is that you can modify the instance, but you have to do it explicitly through the instance of OuterClass. ExampleOuterClass.InnerClass foo = new OuterClass.InnerClass();foo.M_A = 1;outerInstance.InnerClass = foo;//outerInstance.InnerClass.M_A equals 1foo.SomeProperty = 2;// outerInstance.InnerClass.M_A still equals 1outerInstance.InnerClass = foo;//outerInstance.InnerClass.M_A equals 2
James
A: 

If you mean accessing inner class members without exposing the inner class itself, you can use the following code. If you just want to expose this.innerClass, there is no difference to the way you expose the fields of InnerClass.

class OuterClass
{
    private InnerClass innerClass

    public int M_A
    {
        get
        {
            if (this.innerClass != null)
            {
                 return this.innerClass.M_A;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        set
        {
            if (this.innerClass != null)
            {
                 this.innerClass.M_A = value;
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
    }
}
Daniel Brückner
I'm not sure how this applies unless I missed part of the question...is that what he was asking for?
Adam Robinson
Just curious - why would you throw an invalid operation exception on the getter? I understand why you'd do this in the setter, but wouldn't you normally assume the getter to be passive?
BenAlabaster
I read the question "[...] getter and setter for the innerClass member of OuterClass?" with an additional "s" as "[...] innerClass memberS of OuterClass?", but regonized that only after finishing my answer.
Daniel Brückner
What would you return if innerClass is null? In a real world scenario there might be a good default value, but I don't know it from his question. And it is not uncommon to get an exception from a getter.
Daniel Brückner
+3  A: 

The most elegant way of doing this is to use implicit getters and setters:

class InnerClass
{
  public int a{ get; set; }
  public int b{ get; set; }
}

class OuterClass
{
  public InnerClass innerClass{ get; set; }
}

This is implicitly the same as:

class InnerClass
{
    private int _a;
    public int a
    {
        get
        {
            return _a;
        }
        set
        {
            _a = value;
        }
    }

    private int _b;
    public int b
    {
        get
        {
            return _b;
        }
        set
        {
            _b = value;
        }
    }
}

class OuterClass
{
    private InnerClass _innerClass;
    public InnerClass innerClass
    {
        get
        {
            return _innerClass;
        }
        set
        {
            _innerClass = value;
        }
    }
}

These two definitions are implicitly the same - minus quite a few keystrokes. In the first example, the compiler will implement the necessary private fields behind the scenes so you don't have to. The second, however gives you more control of what is going on.

BenAlabaster
+1 for implicit getters and setters
BobTheBuilder
...How is this different from my answer?
Adam Robinson
Also, he gives no indication of the version of C#/.NET that he's using. Implicit properties are a part of .NET 3.5/C# 3.0 only.
Adam Robinson
@Adam, sorry, when I posted my answer I didn't see yours there. I guess you posted as I was writing my answer.
BenAlabaster
A: 
Get method should return the InnerClass object's clone..I guess