martin petrov
martin petrov

Reputation: 123

WriteLine method C#

I am trying to make a program where you enter a value for an int and then using methods adding or subtracting the number "10" from that integer. The problem is that when I write: Console.WriteLine("After addition, your value is now {0}",CustomClass.CustomClass'Method); The compiler tells me that I cannot covert object to a method. Here is my code:

public class CarClass
{

    public static string carModel;

    public static int carSpeed;


    public static void Accelerate()
    {
        carSpeed = carSpeed + 10;
    }
public static void Brake()
    {
        carSpeed = carSpeed - 10;
    }




}
class MainClass
{
    public static void Main(string[] args)
    {

        Console.WriteLine("Enter car model: ");
        CarClass.carModel = Console.ReadLine();

        Console.WriteLine("Enter initial speed: ");
        CarClass.carSpeed = Convert.ToInt32(Console.ReadLine());

        Console.WriteLine("Starting speed of {0} is {1} km/h.", CarClass.carModel, CarClass.carSpeed);
        Console.WriteLine("Now some speed changing...");



Console.WriteLine("After accelerating, new speed of {0} is {1} km/h.",CarClass.carModel,CarClass.carSpeed + CarClass.Accelerate); // here the speed should be starting speed + 10

Console.WriteLine("After accelerating, new speed of {0} is {1} km/h.",CarClass.carModel,CarClass.carSpeed + CarClass.Accelerate); //here the speed should be the previous speed + 10


        Console.ReadLine();

    }
}
}

I am not sure if this is the correct way of making the acceleration because I need it to store the previous value and then add "10" again. I'll be very thankful to you guys if you help me solve that. Thanks in advance.

Upvotes: 0

Views: 221

Answers (1)

Charles Mager
Charles Mager

Reputation: 26213

Your issue is this expression:

CarClass.carSpeed + CarClass.Accelerate

carSpeed is an integer, Accelerate is a method. You can't add numbers and methods together, it doesn't make any sense.

What you want to do is execute the method, which will update carSpeed. Then you can just print the value of carSpeed. So something like:

CarClass.Accelerate();

Console.WriteLine("After accelerating, new speed of {0} is {1} km/h.", 
    CarClass.carModel, CarClass.carSpeed);

Upvotes: 3

Related Questions