4

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.

daOnlyBG
  • 595
  • 4
  • 20
  • 49
  • 3
    possible duplicate of [How do I compile all files in a directory with mono?](http://stackoverflow.com/questions/1794127/how-do-i-compile-all-files-in-a-directory-with-mono) – Heinzi Aug 24 '14 at 11:42
  • 4
    Class Program and method Main must be public. Currently it's private, and not accessible – rufanov Aug 24 '14 at 12:00
  • rufanov, I mistakenly must have deleted the "public" when I posted the question above. However, the public is indeed in my original code, and it's still not working. I did look at the thread Heinzi posted (thanks, btw) and I was able to get both files to compile that way... however, does that mean I'll have to make a subfolder and copy+paste each class whose methods get referenced each time I make a new app? I was hoping there was some other way.. – daOnlyBG Aug 25 '14 at 01:16
  • Unrelated hint: Instead of doing multiple `string += ` use a `StringBuilder` class. Thats what its there for. – John Alexiou Aug 26 '14 at 01:39

1 Answers1

3

Your errors in the "Program.cs" file are not relevant here. They will disappear as soon as you've fixed the one in the "Car.cs" file.

  1. there is an error in "Car.cs"
  2. it does not compile
  3. there is no DLL generated
  4. it cannot be found when compiling the "Program.cs" file

Fix one, the other will be fixed too.

The question remains: how to fix the error in "Car.cs". This is certainly a problem in the file attributes in your project file.

Some possible answers can be found here be most certainly here when pixaloop wrote:

Change the Output Type under the Project > Properties to that of a “Class Library”. By default, this setting may have been set to a “Console Application”.

As a side note, you should try MonoDevelop for your C# development in MacOS. It will ease your project/files/dll management.

Community
  • 1
  • 1
Askolein
  • 3,250
  • 3
  • 28
  • 40
  • 1
    Askolein, here's the code I ended up putting in the Terminal, which resulted in a .dll being created, and the Program.cs file being compiled into an executable: 'gmcs /target:library /out:CarLibrary.DLL Car.cs' 'gmcs /out:Program.exe /reference:CarLibrary.DLL Program.cs' I think this is what you're referring to? I am refraining from using MonoDevelop because I don't want to depend on a program to fill in any "compilation understanding holes"; in other words, I'd like to see how a program compiles and runs entirely. I know this might be a little slow, but I'd rather learn now than later. – daOnlyBG Aug 25 '14 at 22:45
  • 1
    Definitely! Thanks for your help! I recommend anyone learning C# to look at the links all those who contributed to this question. – daOnlyBG Aug 26 '14 at 19:26