我是编程新手,我正在尝试使用Mono编译器为BBEdit" TextWrangler"程序(类似于NotePad ++,但适用于Mac)。然而,我的问题非常简单:我正在尝试使用来自不同.cs文件的类在C#中编写一个非常简单的程序。
我正在努力。如果这些类在同一个.cs文件中,我没有问题;否则,bash终端不断给我错误。这是我的代码:
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;
}
}
}
所以你去吧。代码比看起来更简单 - 但每当我尝试编译这些文件时,都保存在我的Finder中的同一文件夹下,我得到了这些结果:
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
所以是的,我被困住了。我在C#的入门课程建设上倾注了很多资料,但我的运气一无所获。非常感谢任何帮助。
答案 0 :(得分:3)
您在" Program.cs"中的错误文件与此处无关。一旦你在#Car;" Car.cs"中修复了它们,它们就会消失。文件。
修复一个,另一个也将修复。
问题仍然存在:如何修复" Car.cs"中的错误。这肯定是项目文件中文件属性的问题。
当pixaloop写道时,here可能会找到一些可能的答案here:
更改项目下的输出类型>属于“类库”的属性。默认情况下,此设置可能已设置为“控制台应用程序”。
作为旁注,您应该在MacOS中尝试MonoDevelop进行C#开发。它将简化您的项目/文件/ DLL管理。