user2371684
user2371684

Reputation: 1555

Access constructor value

I am trying to access value in my code. I have two classes, carex and engine.

The engine class has the following code:

public class Engine
{
    public string Size;
    public int HorsePower;
    public float FuelConsumtionRate;
    
    public Engine()
    {
    }
    
    public Engine(string cylinder, int hp, float fuelRate)
    {
        Size = cylinder;
        Console.WriteLine($"Engine type: {cylinder}");
        HorsePower = hp;
        Console.WriteLine($"Horse power: {hp} hp");
        FuelConsumtionRate = fuelRate;
        Console.WriteLine($"Fuel consumption: {fuelRate} l/h");
    }
}

The carex class has the following code:

public class CarEx
{
    string Manfacturer;
    string RegistrationNr;
    float Fuel;
    float Speed;
    bool IsRunning;
    
    public CarEx(string manuf, float fuel, string regNr)
    {
        Manfacturer = manuf;
        this.Fuel = fuel;
        RegistrationNr = regNr;
        Console.WriteLine("_____________________________________________________");
        Console.WriteLine($"Manufacturer; {manuf}, Fuel amount: {fuel}l, License: {regNr}");
        Console.WriteLine("______________________________________________________");
    }
    
    public void ChooseEngineType()
    {
        Engine v4 = new Engine("v4", 200, 0.7f);        
    }

    public void FillFuel(float amount)
    {
        Fuel += amount;
        /*            
        Fuel -= 0.7f;
        */
    }
        
    public static void RunCar()
    {
        CarEx car1 = new CarEx("Saab", 10, "1234DD");
        car1.CallCustomer();
        Console.WriteLine($"Maker: {car1.Manfacturer}");
        Console.WriteLine($"Registration nr: {car1.RegistrationNr}");
    }
}

I would like to replace the hard coded fuel consumption rate

Fuel -= 0.7f;

which I commented out with the value set in the engine constructor so its not hard coded any more.

What am I missing out on?

Upvotes: 1

Views: 93

Answers (3)

user2371684
user2371684

Reputation: 1555

Added a new constructor for fuel rate in Engine class

public class Engine
        {
            public static string Size;
            public static int HorsePower;
            public static float FuelConsumtionRate;

            //Constructor to set fuel reate
            public Engine(float fuelRate)
            {
                FuelConsumtionRate = fuelRate;
            }
            public Engine(string cylinder, int hp, float fuelRate)
            {
                Size = cylinder;
                Console.WriteLine($"Engine type: {cylinder}");
                HorsePower = hp;
                Console.WriteLine($"Horse power: {hp} hp");
                FuelConsumtionRate = fuelRate;
                Console.WriteLine($"Fuel consumption: {fuelRate} l/h");
            }

And in the carex class:

public class CarEx
    {
        string Manfacturer { get; set; }
        string RegistrationNr { get; set; }
        float Fuel;
        float Speed;
        bool IsRunning;

        public CarEx(string manuf, float fuel, string regNr)
        {
            Manfacturer = manuf;
            this.Fuel = fuel;
            RegistrationNr = regNr;
            Console.WriteLine("_____________________________________________________");
            Console.WriteLine($"Manufacturer; {manuf}, Fuel amount: {fuel}l, License: {regNr}");
            Console.WriteLine("______________________________________________________");
        }

        public void ChooseEngineType()
        {
            Engine v4 = new Engine("v4", 200, 0.7f);

        }

public void Accelerate()
        {
Speed += 6.0f;

//Fuel rate set for v4 object
Fuel -= Engine.FuelConsumtionRate;
}

Upvotes: 0

Peter B
Peter B

Reputation: 24247

Your ChooseEngineType method creates an Engine object, but it is stored in a local variable which ceases to exist as soon as the method ends.

Change that variable to a field or property Engine engine at class level, and then you can use engine and its property engine.FuelConsumtionRate in all methods.

(PS, the proper spelling of that property would be FuelConsumptionRate)

Upvotes: 2

Rekkon
Rekkon

Reputation: 337

Assuming that the fuel consumption rate is constant, you can use a constant that is privately accessible in the class you've declared by doing this:

private const float fuelConsumptionRate = 0.7f;

and then use it in your code so that you do not hard-code the changes. Otherwise, if the rate is bound to change, you'll need to store either the Engine itself (most recommended), or just the consumption rate in a private field like this:

private Engine engine;
// or
private float fuelConsumptionRate;

// the rest of your class' code
public void ChooseEngineType()
{
    float rate = 0.7f;
    Engine v4 = new Engine("v4", 200, rate);
    engine = v4;
    // or
    fuelConsumptionRate = rate;
}

Upvotes: 1

Related Questions