4

I would like to create a class with properties that have subproperties...

In other words, if I did something like:

Fruit apple = new Fruit();

I would want something like:

apple.eat(); //eats apple
MessageBox.Show(apple.color); //message box showing "red"
MessageBox.Show(apple.physicalProperties.height.ToString()); // message box saying 3

I would think it would be done like:

class Fruit{
    public void eat(){
        MessageBox.Show("Fruit eaten");
    }
    public string color = "red";
    public class physicalProperties{
        public int height = 3;
    }

}

...but, if that were working, I wouldn't be on here...

TheBoyan
  • 6,802
  • 3
  • 45
  • 61
mowwwalker
  • 16,634
  • 25
  • 104
  • 157
  • 2
    so what's the problem then batman? – Prisoner Dec 23 '11 at 09:01
  • ..but, if that were working, I wouldn't be on here... – mowwwalker Dec 23 '11 at 09:02
  • `apple` doesn't have `physicalProperties` now, only `Fruit` does. It's just a type now. And you definitely can't access its `height`, because `physicalProperties` doesn't have a `height`, only its instances do. – harold Dec 23 '11 at 09:04
  • LOL! Did anyone else notice the personification of the apple in this example? i.e. "since when can apples eat stuff?" :P – Chiramisu Jun 28 '12 at 01:25

6 Answers6

7

So close! Here's how your code should read:

class Fruit{
    public void eat(){
        MessageBox.Show("Fruit eaten");
    }
    public string color = "red";
    public class PhysicalProperties{
        public int height = 3;
    }
    // Add a field to hold the physicalProperties:
    public PhysicalProperties physicalProperties = new PhysicalProperties();
}

A class just defines the signature, but nested classes don't automatically become properties.

As a side note, there's also a couple things I'd recommend, to follow .NET's "best practices":

  • Don't nest classes unless necessary
  • All Public members should be Properties instead of Fields
  • All Public members should be PascalCase, not camelCase.

I'm sure there's plenty of documentation on best practices too, if you're so inclined.

Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
  • 1
    Don't you need to create a instance else height won't be accessible – V4Vendetta Dec 23 '11 at 09:09
  • If I'm not supposed to nest classes, how do I add properties to the properties? – mowwwalker Dec 23 '11 at 09:12
  • @Walkemeo Nested classes is just referring to the namespace ... I shouldn't have even brought it up, it's more of a preference. Read more here: http://stackoverflow.com/questions/804453/using-inner-classes-in-c-sharp – Scott Rippey Dec 23 '11 at 09:21
  • 1
    @Walkerneo - You can define the class outside the "Fruit" class, but still create an instance of it within the Fruit class. Much as String is defined elsewhere, but you still have a String instance in the Fruit class. – Rawling Dec 23 '11 at 09:26
  • Also, what if I want that property to return a value if none of its members are called? – mowwwalker Dec 23 '11 at 10:23
  • @Walkerneo I'm not sure I correctly understand your question. If none of its members are called, then `apple.physicalProperties` returns a `PhysicalProperties` instance, the same way `apple.color` returns a `String` instance. – Scott Rippey Dec 27 '11 at 20:39
2
class Fruit{
    public void eat(){
        MessageBox.Show("Fruit eaten");
    }
    public string color = "red";
    //you have declared the class but you havent used this class
    public class physicalProperties{
        public int height = 3;

    }
    //create a property here of type PhysicalProperties
    public physicalProperties physical;
}
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
0

Can you extrapolate the class, then reference it?

class Fruit{
    public void eat(){
        MessageBox.Show("Fruit eaten");
    }
    public string color = "red";
    public physicalProperties{
        height = 3;
    }

}

public class physicalProperties{
        public int height = 3;
    }
Flater
  • 12,908
  • 4
  • 39
  • 62
0
class Fruit{
    public class physicalProperties{
        public int height = 3;
    }
}

Fruit.physicalProperties does indeed declare another type, but:

  1. It will be private, only available to the members of Fruit unless you declare it public.
  2. Creating a type does not create an instance of that type, or member of Fruit to allow users of Fruit to access it.

You need to add:

class Fruit {
  private physicalProperties props;
  public physicalProperties PhysicalProperties { get; private set; }

and in the (default) constructor of Fruit assign PhysicalProperties an instance of physicalProperties.

Richard
  • 106,783
  • 21
  • 203
  • 265
0

You will need to expose the property as it is contained for the instance of the class physicalProperties so maybe you could do like

public Fruit()
{
    physical = new physicalProperties();
}

And a property which gives it back

public int Height { get { return physical.height;}}

OR

public physicalProperties physical;

so that you can do apple.physical.height

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
0
class Fruit{
    public void eat(){
        MessageBox.Show("Fruit eaten");
    }
    public string color = "red";
    public class physicalProperties{
        public int height = 3;
    }
    // create a new instance of the class so its public members
    // can be accessed
    public physicalProperties PhysicalProperties = new physicalProperties();
}

You can then access the sub-properties like so:

Fruit apple = new Fruit();
MessageBox.Show(apple.PhysicalProperties.height.ToString());
Intrepid
  • 2,781
  • 2
  • 29
  • 54