Dendei
Dendei

Reputation: 571

strings and longs and classes as properties

im currently trying to learn about properties and i understand the benefit of using them but i dont know how to handle Classes in class.

Se example

public class Office
{

    public long Identifier { get; set; }
    public string Address { get; set; }
    public long EmployesCount { get; set; }

    public Rooms Rooms
    {
        get { return _rooms; }
        set { _rooms = value; }
    }

    private Rooms _rooms = new Rooms();
}

public class Rooms
{
    public long Identifier { get; set; }
    public double Width { get; set; }
    public double Length { get; set; }
    //and so on
}

if i dont set the private Rooms to new i'll get nullreference exeception. so is this good practice or should i just declare the rooms class like this instead.

public Rooms Rooms = new Rooms();

is there a point to make the class "Rooms" as property?

Upvotes: 0

Views: 59

Answers (4)

CodingIntrigue
CodingIntrigue

Reputation: 78535

All non-static classes need to be set to an instance of an object, somewhere, if you want to call methods against that object.

Depends on who you want to initialize that object. If you want whoever is using your class to do it, just leave as:

public Rooms Rooms {get;set;}

Then someone can do:

Office o = new Office();
o.Rooms = new Rooms();

If you want to make sure Rooms is never null, just initialize it in your Office constructor:

public Office() {
  this.Rooms = new Rooms();
}

public Rooms Rooms {get;set;}

In the above case, we can use:

Office o = new Office();
// Rooms will be initialized when we first use it
o.Rooms.Length = 15;

Upvotes: 4

Parv Sharma
Parv Sharma

Reputation: 12705

from wat i percieve you are trying to do. An office can contain more then one room with different length and width so make a Room class whoes structure will be simillar to your Rooms class

public class Room
{
    public long Identifier { get; set; }
    public double Width { get; set; }
    public double Length { get; set; }
    //and so on
}

and then in the office class make property as list of rooms

public class office
{
 public List<Room> Rooms{get; set;}
 public Office()
 {
  Rooms = new List<Room>()
 }
}

Upvotes: 1

knittl
knittl

Reputation: 265221

Use the constructor to initialize any properties that might not be set. That way you can still use an auto-property for Rooms:

public class Office {
    public long Identifier { get; set; }
    public string Address { get; set; }
    public long EmployesCount { get; set; }

    public Rooms Rooms { get; set; }

    public Office() {
      this.Rooms = new Rooms();
    }
}

Upvotes: 1

Samuel
Samuel

Reputation: 6490

You can do it in two way. If you need to ensure that Rooms exist, then either use your code above or convert it back to an auto property

public Rooms Rooms{ get; set; }

and perform the initialization in the constructor.

Edit: If you are exposing the member then a property makes sense. If you are only using it in your class I would fall back to plain protected/private class member.

By the way. A classes called Rooms may indicate an issue here. A class is typically singular. When you wrap something I would suggest RoomCollection as wrapper class or simply IList or IEnumerable

Upvotes: 0

Related Questions