0

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

S. Nog
  • 61
  • 10
  • 1
    Don't abbreative your parameter names. `mk, mdl, yr, sld` aren't clear. Follow normal C# conventions, use `make, model, year, whenSold`. And if `whenSold` represents a date, don't story it as a string! – mason Jun 23 '17 at 20:36

3 Answers3

3
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"));
    }
}
  • That's not enough. The ReadData is in the Car class not in the Program class. It needs Car.ReadData. – Steve Jun 23 '17 at 20:37
  • @Steve ah, yeah. Thanks. –  Jun 23 '17 at 20:37
  • Hi. I just tried this, and nothing changed. ReadData is still not recognized by public void main. – S. Nog Jun 23 '17 at 20:38
  • @S.Nog left out the other piece you needed. Added. –  Jun 23 '17 at 20:41
  • Actually the default access modifier is internal not private. Not that it matters much in this case – Jesper Niedermann Jun 23 '17 at 20:49
  • 1
    @JesperNiedermann naw, that's for classes. For methods, that's `private`. It's always the most restricted for the given context. So, I guess I'm calling myself a liar--nested classes without access modifiers are private... But classes defined in a namespace without an access modifier are definitely internal. –  Jun 23 '17 at 20:51
  • 1
    Wow. You are right. I have been explicit about access modificers for so many years that I had forgotten that methods are not the same in this case. Thank you for clarifying. – Jesper Niedermann Jun 23 '17 at 21:07
1

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();
Gordon Beeming
  • 585
  • 2
  • 11
  • 1
    +1, this is the best answer so far because it teaches the proper use of OOP - any time you are defining a static method inside a type that takes an argument of that same type, you should just make it an instance method instead. I'd even suggest changing the name from `ReadData` as that has nothing to do with the method's behavior - it's actually printing data out. `ReadData` would make more sense if it was a static factory method that returned a new `Car` that was already constructed based on a byte stream or other mechanism. – ozeanix Jun 23 '17 at 21:18
0

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);
        }
    }
}
mrsauravsahu
  • 2,046
  • 2
  • 15
  • 21