Click here to Skip to main content
15,909,741 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
For a Car object you should be able to specify a year model with set method and bring up the year model with a get method. The car should have its two states "The car is driving" and "The car is stationary", which can be read with the status () method. Initially, the car has a "standstill" condition, but it changes if you call the start () method. You can stop the car with the stop () method and then of course change the status to "stand still.

In console it should come out something like " Have now bought me a car of the 2017 model
The car is stationary
Trying to start the car
Must check if you got started ... The car is driving
Trying to get a stop on the car
Will see if I managed to stop it too ... The car is stationary "

So basically, the code is like done, but i don't know why it is not working... i got the get/set year to work, but i don't know how to get status, start and stop to work.

Any help? :)

What I have tried:

C#
 using System;
using System.Collections.Generic;
using System.Text;
using Bilen;
namespace Car
{
    class Program
    {
        static void Main(string[] args)

        {
            Bil theCar = new Bil();
            theCar.Color = "red";
            theCar.License = "bob1337";
            theCar.Make = "Ferrari";

            Console.WriteLine(theCar.Color);
            Console.WriteLine(theCar.License);
            Console.WriteLine(theCar.Make);

            Console.ReadLine();
            Console.ReadLine();
            Console.ReadLine();


            theCar.setYear(2017);
            Console.WriteLine("Got my self a car of  " + theCar.getYear() + " Year model");
            Console.WriteLine(theCar.status());
            Console.WriteLine("Trying to start the car..");
            theCar.Start();
            Console.WriteLine("Need to check if you started..." + theCar.status());
            Console.WriteLine("Trying to stop the car..");
            theCar.stop();
            Console.WriteLine("Lets see if the car stopped... " + theCar.status());
            Console.ReadKey();


        }
    }
    class Bil
    {


        public string Make { get; set; }        
        public string License { get; set; }
        public string Color { get; set; }

        private int year;
        private string status = "Car is not moving";
        public static void Start()
        {
            status = "Car is moving";
        }
        public static string status()
        {
            return status;
        }
        public static void stop()
        {
            status = "Car is not moving";
        }
      
        public void setYear(int arg)
        {
            Year = arg;
        }
        public int getYear()
        {
            return Year;
        }
    }
}


 Errors i get = 

Severity Code Description Project File Line Suppression State Suppression State
Error CS1656 Cannot assign to 'status' because it is a 'method group' Car 61 Active
Error CS0102 The type 'Bil' already contains a definition for 'status' Car 55 Active
Error CS0176 Member 'Bil.status()' cannot be accessed with an instance reference; qualify it with a type name instead Car 28 Active
Error CS0176 Member 'Bil.Start()' cannot be accessed with an instance reference; qualify it with a type name instead Car 30 Active
Error CS0176 Member 'Bil.status()' cannot be accessed with an instance reference; qualify it with a type name instead Car 31 Active
Error CS0176 Member 'Bil.stop()' cannot be accessed with an instance reference; qualify it with a type name instead Car 33 Active
Error CS0176 Member 'Bil.status()' cannot be accessed with an instance reference; qualify it with a type name instead Car 34 Active
Error CS1656 Cannot assign to 'status' because it is a 'method group' Car 53 Active
Error CS0428 Cannot convert method group 'status' to non-delegate type 'string'. Did you intend to invoke the method? Car 57
Posted
Updated 23-Mar-20 7:36am
v3

You need to go back to the beginning of your course and read it through again.
When you declare a method using the static keyword, it means that it is not a property of a Car instance it is a property of the Car class: it is shared by all instances and does not need an instance to be used.

For example with a real car: "What colour is it?" is a question that only works if you ask it about a specific car: "What colour is my car?", "What colour is your car?", "What colour is this car?", "What colour is that car?". YOu can't ask "What colour is a car?" because all cars are not the same colour - you need an instance to ask the question:
C#
public class Car
   {
   public Color GetColour() { ... }
   ...
   }

Car myCar = new Car("Mercedes A Class", "VIN: XXXXXXXXXXXX");
Console.WriteLine(myCar.GetColour());
On the other hand, you can ask "How many wheels has a car?" and expect to get "4" as the result, because all cars have four wheels (if they had two they would be a motorbike!). The "number of wheels" is a property of the car class, not a property of teh car instance, and as such can be static:
C#
public class Car
   {
   public Color GetColour() { ... }
   public static int GetWheelsCount() { return 4; }
   ...
   }

Car myCar = new Car("Mercedes A Class", "VIN: XXXXXXXXXXXX");
Console.WriteLine(myCar.GetColour());
Console.WriteLine(Car.GetWheelsCount());

A static method can't access anything to do with a specific instance of a Car because it is generic to all Car objects - as a result, you can't access it via a Car instance like you can GetColour to "remind you" that it isn't anything to do with that instance.

So you need to go through your fields and methods and decide which ones are specific to a particular Car, and which are general to the whole class, and remove the static keyword from instance related ones.

Make sense?
 
Share this answer
 
Comments
bouya98 7-Dec-19 7:47am    
Makes sense actually!
You defined status method 2 times in the class
C#
    class Bil
    {


        public string Make { get; set; }        
        public string License { get; set; }
        public string Color { get; set; }

        private int year;
        private string status = "Car is not moving";
        public static void Start()
        {
            status = "Car is moving";
        }
        public static void  status()
        {
            return status;
        }
        public static void stop()
        {
            status = "Car is not moving";
        }
        public static void status()
        {
            return status;
        }
        public void setYear(int arg)
        {
            Year = arg;
        }
        public int getYear()
        {
            return Year;
        }
    }
}

In status method, you should replace void with string.
 
Share this answer
 
Comments
bouya98 6-Dec-19 21:52pm    
I changed it, i replaced void with string, and also removed one method. I still get errors=

Severity Code Description Project File Line Suppression State Suppression State
Error CS1656 Cannot assign to 'status' because it is a 'method group' Car 61 Active
Error CS0102 The type 'Bil' already contains a definition for 'status' Car 55 Active
Error CS0176 Member 'Bil.status()' cannot be accessed with an instance reference; qualify it with a type name instead Car 28 Active
Error CS0176 Member 'Bil.Start()' cannot be accessed with an instance reference; qualify it with a type name instead Car 30 Active
Error CS0176 Member 'Bil.status()' cannot be accessed with an instance reference; qualify it with a type name instead Car 31 Active
Error CS0176 Member 'Bil.stop()' cannot be accessed with an instance reference; qualify it with a type name instead Car 33 Active
Error CS0176 Member 'Bil.status()' cannot be accessed with an instance reference; qualify it with a type name instead Car 34 Active
Error CS1656 Cannot assign to 'status' because it is a 'method group' Car 53 Active
Error CS0428 Cannot convert method group 'status' to non-delegate type 'string'. Did you intend to invoke the method? Car 57
Patrice T 6-Dec-19 22:00pm    
Use Improve question to update your question.
So that everyone can pay attention to this information.
include updated code.
bouya98 6-Dec-19 22:04pm    
Done :)
Patrice T 6-Dec-19 22:27pm    
you did not updated your code.
bouya98 6-Dec-19 22:31pm    
How about now???
i am not sure why you kep start and stop as static, if you are keeping those method static then it can be access directly via class not by object. but i see you have an object created so make sure you access start and stop, set year all via object. set and get year as method can be modfied bit, i see you are o achive encapsulation here by not exposing properties out side like year etc, you can change them to property. here is code which is cleaned little bit for you, you can learn using this code and understand constructor concept when not to expose such property(like color) outside if object can not sustain without it , pass it by constructor.
using System;
using System.Collections.Generic;
using System.Text;
namespace Car
{
    class Program
    {
        static void Main(string[] args)
        {
            Bil theCar = new Bil("red", "bob1337", "Ferrari");

            Console.WriteLine(theCar.Color);
            Console.WriteLine(theCar.License);
            Console.WriteLine(theCar.Make);

            Console.ReadLine();
            Console.ReadLine();
            Console.ReadLine();

            theCar.SetYear(2017);
            Console.WriteLine("Got my self a car of  " + theCar.Year + " Year model");
            Console.WriteLine(theCar.Status);
            Console.WriteLine("Trying to start the car..");
            theCar.Start();
            Console.WriteLine("Need to check if you started..." + theCar.Status);
            Console.WriteLine("Trying to stop the car..");
            theCar.stop();
            Console.WriteLine("Lets see if the car stopped... " + theCar.Status);
            Console.ReadKey();


        }
    }

    class Bil
    {
        public string Make { get; private set; }
        public string License { get; private set; }
        public string Color { get; private set; }

        private int year;
        private string status = "Car is not moving";

        public Bil(string color, string lic, string make) : this()
        {
            Color = color;
            License = lic;
            Make = make;
        }

        public Bil()
        {
            status = "Car is not moving";
        }



        public string Status
        {
            get
            {
                return status;
            }
            set
            {
                status = value;
            }           
        }

        public int Year
        {
            get
            {
                return year;
            }
        }

        public void Start()
        {
            status = "Car is moving";
        }

        public void stop()
        {
            status = "Car is not moving";
        }

        public void SetYear(int value)
        {
            year = value;
        }
    }
}
 
Share this answer
 
using System;
using System.Collections.Generic;
using System.Text;
namespace Car
{
class Program
{
static void Main(string[] args)
{
Bil theCar = new Bil("red", "bob1337", "Ferrari");

Console.WriteLine(theCar.Color);
Console.WriteLine(theCar.License);
Console.WriteLine(theCar.Make);

Console.ReadLine();
Console.ReadLine();
Console.ReadLine();

theCar.SetYear(2017);
Console.WriteLine("Got my self a car of " + theCar.Year + " Year model");
Console.WriteLine(theCar.Status);
Console.WriteLine("Trying to start the car..");
theCar.Start();
Console.WriteLine("Need to check if you started..." + theCar.Status);
Console.WriteLine("Trying to stop the car..");
theCar.stop();
Console.WriteLine("Lets see if the car stopped... " + theCar.Status);
Console.ReadKey();


}
}

class Bil
{
public string Make { get; private set; }
public string License { get; private set; }
public string Color { get; private set; }

private int year;
private string status = "Car is not moving";

public Bil(string color, string lic, string make) : this()
{
Color = color;
License = lic;
Make = make;
}

public Bil()
{
status = "Car is not moving";
}



public string Status
{
get
{
return status;
}
set
{
status = value;
}
}

public int Year
{
get
{
return year;
}
}

public void Start()
{
status = "Car is moving";
}

public void stop()
{
status = "Car is not moving";
}

public void SetYear(int value)
{
year = value;
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900