I'm new to programming, and am trying to learn C# using a Mono compiler for BBEdit's "TextWrangler" program (something like NotePad++, but for Mac). My question, however, is pretty straightforward: I am trying to code a very simple program in C# that uses a class from a different .cs file.
And I am struggling with it. If the classes were in the same .cs file, I have no issues; otherwise, the bash terminal keeps giving me errors. Here's my code:
Program.cs:
// Client class, where Main method calls the Car method
using System;
using Auto;
namespace Auto
{
class Program
{
public static void Main(string[] args)
{
//Instantiate car class
Car car = new Car("Toyota", "Corolla", "Sedan", 2014, 25000);
Console.WriteLine(car.Maintenance());
Console.Read();
}
}
}
Car.cs:
// "Car" class (does NOT include the Main method)
using System;
namespace Auto
{
public class Car
{
//Create various attributes for Car Class with getters and setters
public string Make { get; set; }
public int Year { get; set; }
public string Model { get; set; }
public string Type { get; set; }
public int Millage { get; set; }
public DateTime PurchaseDate { get; set; }
//Constructor to instantiate Car object that pass all the necessary attribute values
public Car(string make, string model, string _type, int year, int millage)
{
Make = make;
Model = model;
Type = _type;
Millage = millage;
}
//Maintenance method will check all the inspection that the mechanic has to do based on the millage of the car
public string Maintenance()
{
int index = 1;
string maintenanceList = string.Empty;
if (Millage > 5000)
maintenanceList += (index++).ToString() + ". Oil Change " +Environment.NewLine;
if (Millage > 10000)
{
maintenanceList += (index++).ToString() + ". Check Brakes " + Environment.NewLine;
maintenanceList += (index++).ToString() + ". Rotate Tires " + Environment.NewLine;
maintenanceList += (index++).ToString() + ". Lube Hinges, Lock, and Latches" + Environment.NewLine;
}
if (Millage > 15000)
maintenanceList += (index++).ToString() + ". Replace air Cleaner element" + Environment.NewLine;
if (Millage > 30000)
maintenanceList += (index++).ToString() + ". Replace Dust and Pollen Filter" + Environment.NewLine;
return maintenanceList;
}
}
}
So there you go. The code's simpler than it seems- but whenever I try to compile these files, both saved under the same folder in my Finder, I get these results:
Program.cs:
Compilation failed: 1 error(s), 0 warnings
Program.cs(14,13): error CS0246: The type or namespace name `Car' could not be found. Are you missing an assembly reference?
Program.cs(15,31): error CS0841: A local variable `car' cannot be used before it is declared
Car.cs:
error CS5001: Program `Car.exe' does not contain a static `Main' method suitable for an entry point
So yeah, I'm stuck. I've poured over many sources on introductory class building in C#, but my luck has yielded nothing. Any help would be very much appreciated.