C # console program containing a constant static field for displaying text in the main output method

Using C # for the console program in MicroSoft Visual Studio 2010, I made some changes to this with some help from you guys, and this console program works correctly; however, I need to implement a constant static field, which will display the motto “Obey the law of a scout girl” in the output section of the main method. I know this should be something simple, so please bear with me. When I turn on the public static constant string Motto = "Obey the law of the scout girl" in the bass class, I get an error message - the constant "DemoScouts.GirlScout.Motto" cannot be static. Below 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 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(); } } 

}

We welcome all and all sorts of advice.

+4
source share
4 answers

Well, you never initialized the scoutName value, so what exactly do you expect to print?

I think you are missing a line of code like this:

 GirlScout.scoutName = Console.ReadLine(); 

I must say, however, that your class is designed very poorly. You have public data elements, and your methods do not seem to have any purpose or meaning - you should clear the encapsulation and make your variables private. Use methods to change / get your values.

If you need help with comments or another question.

+1
source

You do nothing with the name the user entered:

 Console.Write("Enter the Girl Scout name: "); Console.ReadLine(); 

It should be like this:

 Console.Write("Enter the Girl Scout name: "); GirlScout.scoutName = Console.ReadLine(); 

You also need to change the scoutName type to string , not int .


You must also change the design of your class. Use instance properties, not static fields.

 public class GirlScout { public string Motto { get; set; } public string ScoutName { get; set; } public int DuesOwed { get; set; } public int TroopNumber { get; set; } } 
+6
source

You do not save the username, like other data.

+4
source

I do not see the destination of GirlScout.scoutName ...

Side note: please do not use static properties (not counting the purpose of the destination). Either create an object with normal properties, or do not use them at all ...

+4
source

All Articles