1

I have a struct named MyHouse for example.

struct MyHouse
{
    int Light;
    int Water;
    int Food;

    public MyHouse(int Light, int Water, int Food)
    {
        this.Light = Light;
        this.Water = Water;
        this.Food = Food;
    }
}

City class:

class City
{
    private MyHouse house;
    public City(MyHouse house)
    {
        this.house = house;
    }
    public MyHouse GetHouse()
    {
        return house;
    }
}

In main for example I create

 City c = new City(new MyHouse(3,6,1));

where City is the class where struct MyHouse is. If I want to get the number of light from MyHouse, accessed from the City class how would I do?

7
  • 2
    c.GetHouse().Light; ? Commented Nov 2, 2015 at 12:15
  • 1
    MyHouse should also be a class. Commented Nov 2, 2015 at 12:19
  • @DieterB Light won't be accessible because of protection level Commented Nov 2, 2015 at 12:20
  • @TimSchmelter, beyond the "structs only have edge-case usages and this isn't one of them" reasoning, why do you suggest it be a class? Commented Nov 2, 2015 at 12:35
  • 1
    @DavidArno:just because a House is something that could be changed. It's a typical case for a class. Most types should be classes and nothing was mentioned that suggests using a struct. MSDN: "AVOID defining a struct unless the type has all of the following characteristics: It logically represents a single value, similar to primitive types (int, double, etc.). It has an instance size under 16 bytes. It is immutable. It will not have to be boxed frequently." Commented Nov 2, 2015 at 12:41

4 Answers 4

2

Add a property in MyHouse so that private data can be accessed :

private int light;
public int Light{
  get { return light; }
}

Then you can write :

int l = c.GetHouse().Light;

Of course GetHouse() could be turned into a property

public MyHouse House{
  get { return house; }
  set {light = house;}
}

Properties are "better" than methods, because :

  • They can easily be used while debugging - hover mouse above property and see value

  • They are often used by frameworks (Entity frameworks, graphical components). Propertie are used for automatic display of columns

Properties are "better" than direct access to fields because:

-If client code is used to access the property, some code can be added later to the property get or set without any change to the client -If they are virtual, they can be overriden in sub classes

Sign up to request clarification or add additional context in comments.

3 Comments

Sorry, somebody edited, while I was, so I didn't see the result. Sorry for collision. Underscore are ok but not necessary and not necessary so nice. It 's a matter of taste ;-)
No that's ok, it 's better to keep courteous, open minded and tolerant, especially on a forum ;-)
This is bad too set {light = house;} in House. Just use {get;set;}.
2

If in MyHouse you modify

int Light;

to be

public int Light {get; private set;}

You can get the light value from a City object like this:

c.GetHouse().Light

Explanation:

public changes the visibility of Light from private (which is the default) to public. A private class/struct member can only be accessed internally by the class itself. To make if available to other classes, like City in this case, I have made it public.

{get; private set;} turns Light from a simple field to a property, basically a field whose access is wrapped around a getter and a setter method. In your example the value of Light is only set in the constructor for MyHouse, so I've made the setter private. This means that only MyHouse can change the value of Light; however, City can still get the value.

2 Comments

In addition to the above, if you are using C# 6, you do not need the private set;. Light can be defined as public int Light {get;} and the constructor can still write to it.
That's true, and I should also add that the solution works with C# 3 or newer.
0

If you don't specify any property like public, protected or private, C# makes the variable private for "struct". So either you need to make the variable public so that you can access it directly or add a public method, to access the private variable.

2 Comments

No, do not make it public as you are then creating a mutable struct, which breaks one of the rules around what makes a good struct
Good practice does not mean a "must". I was saying that was a way to access the variable. One can do that if it fits his situation, but it's not a good way to go in general. Correct me if I am wrong @DavidArno
0

You can call this:

int i = c.GetHouse().Light;

But only if Light is public. Now it isn't and that prevents you from accessing the field.

You can make properties out of your fields. I would suggest to make Light a class too:

class MyHouse
{
    public int Light {get;set;}
    public int Water {get;set;}
    public int Food {get;set;}

    public MyHouse(int light, int water, int food)
    {
        this.Light = light;
        this.Water = water;
        this.Food = food;

    }
}

I'd also expect City to have a list of Houses, not just one. Maybe you should make GetHouse() a List<House> Houses property.

Comments

Your Answer

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