Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Console program that contains a constant static field to display text in the output main method

Tags:

c#

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.

like image 470
Rocky Avatar asked Dec 06 '22 13:12

Rocky


2 Answers

You aren't doing anything with the name that the user entered:

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

It should be this:

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

You also need to change the type of scoutName to be a string rather than an int.


You should also redesign your class. Use instance properties rather than 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; }
}
like image 158
Mark Byers Avatar answered Jan 10 '23 14:01

Mark Byers


You are not storing the name the user input, as you do with the other data.

like image 34
Oded Avatar answered Jan 10 '23 16:01

Oded