如何从其他文件中引用C#类?

如何从其他文件中引用C#类?,c#,bash,oop,mono,textwrangler,C#,Bash,Oop,Mono,Textwrangler,我是编程新手,正在尝试为BBEdit的“TextWrangler”程序(类似于NotePad++,但适用于Mac)使用Mono编译器学习C#。然而,我的问题很简单:我试图用C编写一个非常简单的程序,它使用不同.cs文件中的类 我正在与之斗争。如果类在同一个.cs文件中,我没有问题;否则,bash终端会不断地给我错误。这是我的密码: Program.cs: // Client class, where Main method calls the Car method using System;

我是编程新手,正在尝试为BBEdit的“TextWrangler”程序(类似于NotePad++,但适用于Mac)使用Mono编译器学习C#。然而,我的问题很简单:我试图用C编写一个非常简单的程序,它使用不同.cs文件中的类

我正在与之斗争。如果类在同一个.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();
        }
    }
}
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:

//  "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;
        }
    }
}
error CS5001: Program `Car.exe' does not contain a static `Main' method suitable for an entry point
好了。代码比看起来要简单-但每当我尝试编译这些文件时,它们都保存在Finder中的同一文件夹下,我都会得到以下结果:

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();
        }
    }
}
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:

//  "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;
        }
    }
}
error CS5001: Program `Car.exe' does not contain a static `Main' method suitable for an entry point
所以是的,我被卡住了。关于C#的入门课程建设,我倾注了很多资料,但我的运气一无所获。非常感谢您的帮助。

您在“Program.cs”文件中的错误与此无关。一旦你修复了“Car.cs”文件中的那个,它们就会消失

  • “Car.cs”中有错误
  • 它不编译
  • 没有生成DLL
  • 编译“Program.cs”文件时找不到该文件
  • 修好一个,另一个也会修好

    问题仍然存在:如何修复“Car.cs”中的错误。这当然是项目文件中的文件属性中的一个问题

    当pixaloop写道:

    将项目>属性下的输出类型更改为“类库”的输出类型。默认情况下,此设置可能已设置为“控制台应用程序”


    另外,您应该尝试在MacOS中进行C#开发。它将简化您的项目/文件/dll管理。

    类程序和主方法的可能副本必须是公共的。目前它是私有的,不可访问。当我发布上述问题时,我一定是错误地删除了“public”。然而,公众确实在我的原始代码中,它仍然不起作用。我确实看过海因茨发布的帖子(谢谢,顺便说一句),我能够以这种方式编译这两个文件。。。然而,这是否意味着我必须创建一个子文件夹,并在每次创建新应用程序时复制并粘贴其方法被引用的每个类?我希望有其他方法..不相关的提示:不要做多个
    string+=
    使用
    StringBuilder
    类。这就是它的用途。Askolein,这是我最后放入终端的代码,它导致了一个.dll被创建,Program.cs文件被编译成一个可执行文件:'gmcs/target:library/out:CarLibrary.dll Car.cs''gmcs/out:Program.exe/reference:CarLibrary.dll Program.cs'我想这就是你指的?我避免使用MonoDevelop,因为我不想依赖程序来填补任何“编译理解漏洞”;换句话说,我想看看程序是如何编译和运行的。我知道这可能有点慢,但我宁愿现在就学,也不愿以后学。当然!谢谢你的帮助!我建议任何学习C#的人都去看看所有对这个问题有贡献的人的链接。