簡體   English   中英

如何從其他文件引用C#類?

[英]How can I reference a C# class from a different file?

我是編程新手,我正嘗試使用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();
        }
    }
}

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#入門類構建中投入了大量資源,但是我的運氣卻一無所獲。 任何幫助將不勝感激。

您在“ Program.cs”文件中的錯誤與此處無關。 在您將“ Car.cs”文件中的內容修復后,它們就會消失。

  1. “ Car.cs”中存在錯誤
  2. 它不編譯
  3. 沒有生成DLL
  4. 編譯“ Program.cs”文件時找不到

修復一個,另一個也將修復。

問題仍然存在:如何解決“ Car.cs”中的錯誤。 這肯定是項目文件中文件屬性中的問題。

一些可能的答案可以發現這里是最肯定在這里當pixaloop寫道:

將項目>屬性下的輸出類型更改為“類庫”的輸出類型。 默認情況下,此設置可能已設置為“控制台應用程序”。

作為附帶說明,您應該在MacOS中嘗試MonoDevelop進行C#開發。 這將簡化您的項目/文件/ dll管理。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM