C#在哪里添加方法

时间:2014-05-20 19:37:32

标签: c# methods

我对C#很感兴趣 - 刚刚开始一天前。我必须做一个简单的C#数据库,包括音乐专辑等。我得到的问题是我不能调用我刚刚做的方法,有人可以帮助我在main中包含这个方法吗?

void addnew()
        {
            int ID = currid;
            string AlbNm;
            string Art;
            string RelDstring;
            int RelD;
            string TrAmnstring;
            int TrAmn;
            string Loc;
            int Rat;
            int ratswitch;
            string ratswitchstring;
            Console.Clear();
            Console.WriteLine("Podaj nazwe albumu");
            AlbNm = Console.ReadLine();
            Console.WriteLine("Podaj nazwe wykonawcy");
            Art = Console.ReadLine();
            Console.WriteLine("Podaj rok wydania");
            RelDstring = Console.ReadLine();
            bool ifintreld = int.TryParse(RelDstring, out RelD);
            bool correctyear = RelD < 2014 && RelD > 1900;
            while (ifintreld == false)
            {
                Console.WriteLine("Podano bledny rok wydania, uzyj liczb calkowitych.");
                RelDstring = Console.ReadLine();
            }
            RelD = Convert.ToInt32(RelDstring);
            while (correctyear == false)
            {
            Console.WriteLine("Podano bledny rok wydania, uzyj daty z zakresu 1900-2014");
            RelDstring = Console.ReadLine();
            while (ifintreld == false)
            {
                Console.WriteLine("Podano bledny rok wydania, uzyj liczb calkowitych.");
                RelDstring = Console.ReadLine();
            }
            RelD = Convert.ToInt32(RelDstring);
            }
            Console.WriteLine("Podaj ilosc utworow");
            TrAmnstring = Console.ReadLine();
            bool ifinttramn = int.TryParse(TrAmnstring, out TrAmn);
            while (ifinttramn == false)
            {
                Console.WriteLine("Podano bledna liczbe utworow, uzyj liczb calkowitych.");
                TrAmnstring = Console.ReadLine();
            }
            RelD = Convert.ToInt32(RelDstring);
            Console.WriteLine("Podaj sciezke do pliku");
            Loc = Console.ReadLine();
            Console.WriteLine("Podaj ocene [1-5]");
            ratswitchstring = Console.ReadLine();
            bool ifintrat = int.TryParse(ratswitchstring, out ratswitch);
            while (ifintrat == false)
            {
                Console.WriteLine("Podano bledna ocene, uzyj liczb calkowitych z zakresu 1-5.");
                ratswitchstring = Console.ReadLine();
            }
            ratswitch = Convert.ToInt32(ratswitchstring);
            while (ratswitch != 1 || ratswitch != 2 || ratswitch != 3 || ratswitch != 4 || ratswitch != 5)
            {
                Console.WriteLine("Podano bledna ocene, uzyj liczb calkowitych z zakresu 1-5.");
                ratswitchstring = Console.ReadLine();
                while (ifintrat == false)
                {
                    Console.WriteLine("Podano bledna ocene, uzyj liczb calkowitych z zakresu 1-5.");
                    ratswitchstring = Console.ReadLine();
                }
            }
            Rat = ratswitch;
         }

VS在静态主要方面呼吁非静态方法,但是使用那个currid和currid ++它不能是静态的(至少我认为是这样; p) 谁能告诉我如何在我的控制台应用程序中运行此方法?

5 个答案:

答案 0 :(得分:2)

我假设这一切都在一个“程序”类中。要解决“静态”问题,只需创建该类的实例(或任何类addnew):

   var p = new Program();
   p.addnew();

可以使currid变为静态;唯一的缺点是 Program的所有实例都会使用相同的currid变量。由于这只是一个学习练习,无论如何都无关紧要。

答案 1 :(得分:1)

一种解决方案是创建此方法所在的类的实例。因此,如果该类名为TestClass,则执行以下操作:

new TestClass().addnew();

答案 2 :(得分:0)

看起来你是从静态方法调用非静态方法 如果您可以创建该类的实例,您将能够将该方法作为属性

获取到main

答案 3 :(得分:0)

<如果您的班级名称是Program,您可以在main中使用非静态函数,如下所示

    static void Main(string[] args)
    {
        Program abc = new Program();
        abc.addnew();
    }

答案 4 :(得分:0)

您必须将所有变量和方法设置为静态才能在Main方法中使用它们,或者创建一个新类并在Main方法中使用该对象。