简体   繁体   中英

C# consoleApp Cannot call static method in separate class

I've been trying to make this function for the past few hours and hopefully master the use of classes. I have searched vigorously for answers but most of them do not seem to work in my case.

I created a class, public class Car as well as a constructor & method static void ReadData()

Now my goal here is to introduce a new instance of Car and call upon it so that console reads it out. The main problem is that under public void main it refuses to recognize my ReadData() method.

Here is my code:

namespace ConsoleApp13
{
class Program
{

    public void Main(string[] args)
    {

        Car crossOver = new Car("BMW", "X4", 2015, "6/23/17");

        ReadData(crossOver);  // debugger says that ReadData does not exist 
       {
       Console.ReadLine();
       }
    }

}

public class Car
{
    private string make;
    private string model;
    private int year;
    private string whenSold;

    public Car(string mk, string mdl, int yr, string sld)
    {
        make = mk;
        model = mdl;
        year = yr;
        whenSold = sld;
    }
    static void ReadData(Car Car)
    {
        Console.WriteLine("Make: " + Car.make);
        Console.WriteLine("Model: " + Car.model);
        Console.WriteLine("Year: " + Car.year);
        Console.WriteLine("Sold at: " + Car.whenSold);
    }
   }
  }

I've tried several different ways of putting static before the scopes but it always ends in some error or the console application immediately exits out without reading the strings

static void ReadData(Car Car)

You didn't add an access modifier . The default is private . That means you can't access it outside of the class. You have to tell the compiler that other code can call this method.

public static void ReadData(Car Car)

Also, to call a static method that is defined on a Type, you must use the Type's name to call it.

Car.ReadData(someCar);

When you get the hang of that, you can actually get away without doing this by using a new feature of C# 6, static usings

using static Car; // imports all static methods into the current context
{
    public void Main()
    {
        ReadData(new Car("lol"));
    }
}

You are missing Car in your code as well as the access modifier as @Will pointing out above. Should be

Car.ReadData(crossOver);

or with more changes what you could do is just make it public instead of static, remove the Car parameter adn then use this. instead of referencing the Car parameter and then call the code like this

Car crossOver = new Car("BMW", "X4", 2015, "6/23/17");
crossOver.ReadData();

As the ReadData static function is part of the Car class, you have to make the function call as:

Car.ReadData(crossover);

The reason that the Main is not recognizing ReadData is because it is looking for its definition in the Program class.

Your entire code:

namespace ConsoleApp13
{
    class Program
    {
        static void Main(string[] args)
        {

            Car crossOver = new Car("BMW", "X4", 2015, "6/23/17");
            Car.ReadData(crossOver);
            Console.ReadLine();
        }
    }

    public class Car
    {
        private string make;
        private string model;
        private int year;
        private string whenSold;

        public Car(string mk, string mdl, int yr, string sld)
        {
            make = mk;
            model = mdl;
            year = yr;
            whenSold = sld;
        }
        public static void ReadData(Car Car)
        {
            Console.WriteLine("Make: " + Car.make);
            Console.WriteLine("Model: " + Car.model);
            Console.WriteLine("Year: " + Car.year);
            Console.WriteLine("Sold at: " + Car.whenSold);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM