问题描述
我刚接触C#-就在一天前开始.我必须做一个包含音乐专辑等的简单C#数据库.我遇到的问题是我不能调用我刚刚做过的方法,有人可以帮助我将此方法包括在main中吗?
I'm fresh to C# - just started a day ago. I have to do a simple C# database consisting of music albums etc. The problem I got is that I can't call a method i just did, can anyone help me to include this method in 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)谁能告诉我如何在我的控制台应用程序中运行此方法?
VS cries for nonstatic method in static main, but with that currid and currid++ it cannot be static (at least i think so ;p)Can anyone tell me how to run this method in my console app?
推荐答案
我假设所有这些都在单个程序"类中.要解决静态"问题,只需创建该类的实例(或其中的任何类addnew):
I'm assuming this is all in a single "Program" class. To solve the "static" problem, just create an instance of that class (or whatever class addnew is in):
var p = new Program(); p.addnew();
可能可以使currid变为静态;唯一的缺点是的所有实例将使用相同的currid变量.由于这只是一种学习练习,所以无论哪种方式都没有关系.
It may work to make currid static instead; the only drawback is all instances of Program would use the same currid variable. Since this is just a learning exercise it doesn't matter either way.
这篇关于C#在哪里添加方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!