问题
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 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?
回答1:
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();
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.
回答2:
One solution is to create an instance of the class in which this method is located. So if the class is called TestClass, you do:
new TestClass().addnew();
回答3:
It looks like you are calling nonstatic method from a static method If you can create an instance of that class you will be able to get that method into main as a property
回答4:
Provided that your class name is Program you can use nonstatic function inside main as following
static void Main(string[] args)
{
Program abc = new Program();
abc.addnew();
}
回答5:
You either have to make all your variables and methods static in order to use them in the Main method, or create a new class and use the object in the Main method.
来源:https://stackoverflow.com/questions/23768457/c-sharp-where-to-add-a-method