Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - class instantiating other classes?

I'm a C# beginner and am struggling a little bit with how classes relate to one another.

I am trying to code up a very simple elevator simulation. I have a class for Elevator:

class Elevator
{
    public int currentFloor;

    public Elevator()
    {
        currentFloor = 0;
    }

    public void ascend()
    {
        currentFloor++;
    }

    public void descend()
    {
        currentFloor--;
    }
}

Very simple. This works, I can instantiate a new elevator object and have it go up and down, etc...

Now, I want to create a building object, so I created a new class for Buildings. However, I am now stuck - how do I add variable amounts of elevator objects to my buildings? For example, I might want to instantiate a building with 3 elevators, or another with 5...

I started creating a solutiomn where the building class has a List of elevators I can dynamically add to, but that seems so obtuse. So what I am looking for is something like:

Building office = new Building();
office.elevator1 = new Elevator();
office.elevator2 = new Elevator();

which obviously doesn't work because I don't have elevator1 and elevator2 declared in the Building class. What is the best/cleanest way to accomplish what I am looking to do? Also, what is this called? I Googled a ton of terms - class belongs to another class, instantiating a class from another class, similar terms with object instead of class... I've also looked over some of the elevator simulator code out there, but couldn't find anything dynamic like I'm looking for...

like image 478
SC'1 Avatar asked Oct 04 '12 20:10

SC'1


People also ask

What C is used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What is C in C language?

What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.


2 Answers

Having a List<Elevator> is quite appropriate here; it describes the real-world model very well.

Perhaps it would be better if it were an Elevator[] (in the sense that perhaps it should not be possible to change the number of installed elevators after the building has been erected), but that's not absolute.

In any case, the collection of elevators should be exposed as a read-only property of appropriate type because it doesn't make sense to swap it with another one.

like image 54
Jon Avatar answered Sep 27 '22 23:09

Jon


You can add member of type equal to List<Elevator> nd inject inside constructor

Sample

public class Building
{
    private List<Elevator> yourList;
    public Building(List<Elevator> value)
    {
      yourList = value;
    }
}

Use case :

var list = new List<Elevator>();
list.Add
.....
var building = new Building(list);
like image 38
Aghilas Yakoub Avatar answered Sep 27 '22 23:09

Aghilas Yakoub