Using C# for a console program in MicroSoft Visual Studio 2010, I have made some changes to this with some help from you guys and this console program is running correctly; however I need to implement a constant static field that will display the motto “To Obey the Girl Scout Law” in the output section of the main method. I know it must be something simple so please bear with me. When I include the public static const string Motto = “To Obey the Girl Scout Law” in the bass class, I get an error message – The constant ‘DemoScouts.GirlScout.Motto’ can not be static. The following is the complete code for this project:
public class GirlScout
{
public static const string Motto = "To Obey the Girl Scout Law";
public static string scoutName;
public static string enterName()
{
return scoutName;
}
public static int duesOwed;
public static int enterAmount()
{
return duesOwed;
}
public static int troopNumber;
public static int enterNumber()
{
return troopNumber;
}
}
class MainClass : GirlScout
{
static void Main()
{
Console.WriteLine();
Console.Write("Enter the Girl Scout's name: ");
GirlScout.scoutName = Console.ReadLine();
Console.WriteLine();
Console.Write("Enter their Troop Number: ");
string n = Console.ReadLine();
GirlScout.troopNumber = Int32.Parse(n);
GirlScout.enterNumber();
Console.WriteLine();
Console.Write("Enter the amount they Owe in Dues: $");
string d = Console.ReadLine();
GirlScout.duesOwed = Int32.Parse(d);
GirlScout.enterAmount();
Console.WriteLine();
// Seperate the input from the output:
Console.WriteLine();
Console.WriteLine(GirlScout.Motto);
Console.WriteLine("-----------------------------------------------");
Console.WriteLine();
// Display the new information:
Console.WriteLine("The name of the Girl Scout is: {0}", GirlScout.scoutName);
Console.WriteLine("The troop Number of the Girl Scout is: {0}", GirlScout.troopNumber);
Console.WriteLine("The amount of Dues Owed by this Girl Scout is: {0}", GirlScout.duesOwed);
// Keep the console window open in debug mode.
Console.ReadKey();
}
}
}
Any and all advice would be greatly appreciated.
Well, you never initialized the value of scoutName, so what exactly do you expect to print?
I think you’re missing a line of code like this:
I have to say, though, that your class is designed very poorly. You have public data members, and your methods seem to have no purpose or meaning – you should brush up on encapsulation, and make your variables private. Use methods to change/get their values.
If you need any help ask in the comments or in another question.