I'm trying to recreate Brackey's Classes tutorial from memory (checked afterwards, of course) and I've run into an issue concerning the order/placement of a fragment. Here's the code:
class Wizard
{
public static string name;
public static string spell;
public static float experience;
public static int slots;
public Wizard(string _name, string _spell)
{
name = _name;
spell = _spell;
slots = 2;
experience = 0f;
}
public void CastSpell()
{
if (slots > 0)
{
Console.WriteLine($"{name} casted {spell}.");
slots--;
experience = 0.5f;
}
else
{
Console.WriteLine($"{name} is out of spells!");
}
static void Meditate() //Used static void because public didn't work for some reason?
{
Console.WriteLine($"{name} meditates and regains all their spells!");
slots = 2;
}
}
}
Wizard wizard1 = new Wizard("Wiz Name", "Spellum lazyum");
wizard1.CastSpell();
My problem lies in the placement of these last two lines. When I have them inside the Wizard class, it gives me the error Invalid token '(' in class, record, struct, or interface member declaration
. Outside, it throws Top-level statements must precede namespace and type declarations.
Am I correct in thinking that the latter might happen because of the 'removal' of the Program class in .NET 6.0? I think I have an okay understand of classes, but clearly I'm missing something. Sorry if this is a simple fix; I didn't get much sleep last night.
Thank you!!
CodePudding user response:
Your problem is the last two lines. These lines are executable code. Executable code must be inside a method.
Executable code is also allowed as top-level statements with specific syntax, hence the error message, but that is not relevant to your use case.
I don't know when you want to execute those two lines of code, but you probably do, so you should move them to the correct location
CodePudding user response:
You have a couple of small problems. You want to support more than one wizard, so you probably want the fields to be instance rather than static. Your Meditate()
is defined as a local function inside CastSpell
which is not what you want. Your example usage code needs to be inside another class & method. Though you could place that within top level statements.
class Wizard
{
public string name;
public string spell;
public float experience;
public int slots;
public Wizard(string _name, string _spell)
{
name = _name;
spell = _spell;
slots = 2;
experience = 0f;
}
public void CastSpell()
{
if (slots > 0)
{
Console.WriteLine($"{name} casted {spell}.");
slots--;
experience = 0.5f;
}
else
{
Console.WriteLine($"{name} is out of spells!");
}
}
public void Meditate() //Used static void because public didn't work for some reason?
{
Console.WriteLine($"{name} meditates and regains all their spells!");
slots = 2;
}
}
public static class Program
{
public static void Test()
{
Wizard wizard1 = new Wizard("Wiz Name", "Spellum lazyum");
wizard1.CastSpell();
}
}
CodePudding user response:
No. The error is not related to NET.6.0.
Code cannot exist directly within a class while being outside of methods (or field/property initializers with respect to expressions). Hence the first error message.
Code can exist as top-level statements, but as the 2nd error message tells you, any top-level statements must precede type declarations. The declaration of a class is a type declaration. Thus your top-level statements do not precede the declaration of your Wizard class, hence error.