Home C# - Declaration of class variables
Reply: 4

C# - Declaration of class variables

G. Yordanov
1#
G. Yordanov Published in 2017-12-07 21:01:31Z

How can I create a new instance of Boeing737 and use it later in the program. For example I want to be able to create 5 Boeings, do I have to define them like

Boeing737 boeing1 =  new Boeing737(name: "Boeing737" + Console.ReadLine(),fuel: int.Parse(Console.ReadLine()) , tons: 0);

Boeing737 boeing2 =  new Boeing737(name: "Boeing737" + Console.ReadLine(),fuel: int.Parse(Console.ReadLine()) , tons: 0);

and so on... Or is there easier way? Other question, to WHAT can I assign all the properties of boeing1 for example?

Here is my current code:

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("Insert the type of boeing that u are using");
        Boeing737 boeing = new Boeing737(name: "Boeing737" + Console.ReadLine(),fuel: int.Parse(Console.ReadLine()) , tons: 0);
        Console.WriteLine("{0} has {1} tons of fuel and weights {2}", boeing.Name, boeing.Fuel, boeing.Tons);
        Console.ReadKey();

    }
}

public class Planes
{
    public Planes(string name, int fuel, int tons)
    {
        Name = name;
        Fuel = fuel;
        Tons = tons;
    }
    public int Tons;
    public int Fuel;
    public string Name { private set; get; }
}
class Boeing737 : Planes
{
    public Boeing737(string name, int fuel, int tons) : base(name, fuel, tons)
    {
        Tons = 700;
    }
}

}

Gabriel Mello
2#
Gabriel Mello Reply to 2017-12-07 21:07:04Z

Use array initialization:

var boeings = new []
  {
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
    new Boeing737("name", 123, 1000),
  };
TRock
3#
TRock Reply to 2017-12-07 21:07:43Z

I would create a list of Boeing737 and I would not take the straight input from the console.

List<Boeing737> boeingList = new List<Boeing737>();
boeingList.add(new Boeing737() { param=value...});

Then later on you can access them by index, name, loop through them, etc.

I would also look into Linq

Tommaso Belluzzo
4#
Tommaso Belluzzo Reply to 2017-12-07 21:31:31Z

Well, let's start by improving your code a little bit:

// the class represents a single object, give it a
// singular name
public class Plane
{
    // get before set, it's not mandatory but give yourself
    // some basic coding rules to improve code maintainability
    // and readability
    // avoid public members, implementing properties is always
    // a good choice for future extensions and manipulations
    public int Fuel { get; private set; }
    public int Tons { get; private set; }
    public string Name { get; private set; }

    public Plane(string name, int fuel, int tons)
    {
        Name = name;
        Fuel = fuel;
        Tons = tons;
    }
}

// if your inheritance stops here, you could set a
// sealed attribute
public sealed class Boeing737 : Plane
{
    // no need to set the property twice, you are already
    // calling the base constructor, pass him the fixed
    // tons value of 700...
    public Boeing737(string name, int fuel) : base(name, fuel, 700)
    {
    }
}

Now, concerning instantiation, go for a the generic List<T> type, which is very easy to manage and will expand itself when you add more objects:

List<Boeing737> boeings = new List<Boeing737>
{
    new Boeing737("A", 5),
    new Boeing737("B", 5),
    new Boeing737("C", 5),
    new Boeing737("D", 5)
};

If you want to create a List that can contain different type of planes, stick to the upper level type:

List<Plane> planes = new List<Plane>
{
    new Boeing737("A", 5),
    new Boeing737("B", 5),
    new Boeing737("C", 5),
    new Boeing737("D", 5),
    new AirplaneX("D", 10, 350)
};

List can also be used together with LINQ to facilitate its manipulation and filtering (more info here). For example, sort by weight:

var planesSortedTons = planes.OrderBy(x => x.Tons).ToList();

Select only the planes with Fuel > 10:

var planesFuel10 = planes.Where(x => x.Fuel > 10).ToList();

On a side note, if you want to fill a huge list of data through console input, you need to build an infinite loop (for example a while (true)) and populate list by addition:

static void Main(string[] args)
{
    List<Boeing737> boeings = new List<Boeing737>();

    String input;

    while (true)
    {
        Consol.WriteLine("Enter name:");
        input = Console.ReadLine();

        if (input.ToLowerInvariant() == "stop")
            break;

        String name = input.Trim();

        Consol.WriteLine("Enter fuel:");
        input = Console.ReadLine();

        if (input.ToLowerInvariant() == "stop")
            break;

        Int32 fuel;

        try
        {
            fuel = Int32.Parse(input.Trim());
        }
        catch
        {
            Console.WriteLine("Wrong input, stopping!");
            break;
        }

        boeings.Add(new Boeing737(name, fuel));
    }
}
user3225726
5#
user3225726 Reply to 2017-12-07 21:36:46Z

What about using C# Arrays.

namespace ConsoleApps_examples { class Program { static void Main(string[] args) { //Console.WriteLine("Insert the type of boeing that u are using");

        //string sname = Console.ReadLine();
        //int ifuel = int.Parse(Console.ReadLine());

        Console.WriteLine(" here are 5 different type of boeing:");

        string sname = "";
        int ifuel = 0;

        int i;
        int[] fuellist = new int[5] { 99, 98, 92, 97, 95 };
        var nameslist = new string[5]  { "XXA1", "XXA2", "XXA3","XXA4","XXA5"};

        //var arr3 = new string[] { "one", "two", "three" };

        for (i = 0; i < 5; i++)
        {
            ifuel = fuellist[i];
            sname = nameslist[i];
            Boeing737 boeing = new Boeing737(name: "Boeing737" + sname, fuel: ifuel, tons: 0);
            Console.WriteLine("{0} has {1} tons of fuel and weights {2}", boeing.Name, boeing.Fuel, boeing.Tons);

        }

        //Boeing737 boeing = new Boeing737(name: "Boeing737" + sname, fuel: ifuel, tons: 0);
        Console.ReadKey();

    }    


}


public class Planes
{
    public Planes(string name, int fuel, int tons)
    {
        Name = name;
        Fuel = fuel;
        Tons = tons;
    }
    public int Tons;
    public int Fuel;
    public string Name { private set; get; }
}
class Boeing737 : Planes
{
    public Boeing737(string name, int fuel, int tons) : base(name, fuel, tons)
    {
        Tons = 700;
    }
}

}

this is the output: List of 5 types of Boeing planes:

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.31447 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO