C#在何处添加方法

C#在何处添加方法,c#,methods,C#,Methods,我刚到C#-一天前才开始。我必须做一个简单的C#数据库,由音乐专辑等组成。我遇到的问题是,我不能调用一个我刚刚调用的方法,有人能帮我把这个方法包含在main中吗 void addnew() { int ID = currid; string AlbNm; string Art; string RelDstring; int RelD; s

我刚到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;
         }
void addnew()
{
int ID=currid;
字符串AlbNm;
弦乐艺术;
串重串;
国际关系;
弦约束;
国际电车公司;
字符串Loc;
int大鼠;
内部速率开关;
字符串转换字符串;
Console.Clear();
Console.WriteLine(“Podaj-nazwe-albu”);
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=转换为32(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=转换为32(RelDstring);
}
控制台。WriteLine(“Podaj ilosc utworow”);
TrAmnstring=Console.ReadLine();
bool-ifinttramn=内锥虫(TrAmnstring,out-TrAmn);
while(ifinttramn==false)
{
Console.WriteLine(“Podano bledna liczbe utworow,uzyj liczb calkowitych”);
TrAmnstring=Console.ReadLine();
}
RelD=转换为32(RelDstring);
Console.WriteLine(“Podaj sciezke do pliku”);
Loc=控制台.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=转换为32(ratswitchstring);
而(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=Rat开关;
}
VS在static main中呼吁使用非静态方法,但使用currid和currid++它不能是静态的(至少我认为是这样;p)
有人能告诉我如何在我的控制台应用程序中运行此方法吗?

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


它可以使
currid
变为静态;唯一的缺点是
Program
的所有实例都将使用相同的
currid
变量。因为这只是一个学习练习,所以无论哪种方式都无关紧要。

一个解决方案是创建此方法所在类的实例。因此,如果该类被称为TestClass,您可以:

new TestClass().addnew();

看起来您正在从静态方法调用非静态方法
如果您可以创建该类的实例,您将能够将该方法作为属性放入main中

,前提是您的类名是Program,您可以在main中使用非静态函数,如下所示

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

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

这取决于此方法的位置和调用位置。签名为
void addNew()
,缺少访问修饰符(例如
public
private
等)。默认情况下,它们是
内部的
,这可能已经足够好了,但您应该明确。你的电话号码在哪里?我知道你是新来的,但这让我的眼睛流血。看看@Jonesy,因为老鼠和老鼠开关?就我个人而言,我很感兴趣。另外,我认为你需要对其中一些进行
if
陈述。你有几个无限循环,这是对的,这是我现在的主要问题/神奇的解决方案我没想到。。。我知道我让它工作的方法不好,你的眼睛可能会流血,但试着理解我。两天前我被告知要提供一个项目。我必须在3小时内完成,我没有从我的老师那里得到任何关于这个项目的帮助…@user3658127是的,你的程序还有其他一些问题,但这应该会让你继续。发现了一些错误,当你在控制台中看到它们时,查找它们比在代码中要容易得多:D
    static void Main(string[] args)
    {
        Program abc = new Program();
        abc.addnew();
    }