C# - The name 'BoyorGirl' does not exist in the current context - c#

I'm trying to create a text adventure in C#, it's very different from XNA coding, I am triying to display the gender of the player, but it's in a different field. I get this error:
The name 'BoyorGirl' does not exist in the current context
(The error is at line 113, column 73).
And this is the script: (I put a // where the error appears)
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
....
Console.WriteLine("\t\t Press Enter to continue...");
Console.ReadLine();
Console.Clear();
Start_Game();
}
static public void Start_Game()
{
int StartMenu;
Console.WriteLine(#"Welcome Adventurer..
Are you ready for an adventure?
#1 Start Game
#2 Help
#3 Exit");
StartMenu = int.Parse(Console.ReadLine());
switch (StartMenu)
{
case 1:
Start_Adventure();
break;
case 2:
Help_Menu();
break;
case 3:
Console.WriteLine("Goodbye.");
System.Threading.Thread.Sleep(1000);
Environment.Exit(0);
break;
default:
Console.WriteLine("This is not an option..");
System.Threading.Thread.Sleep(2000);
Console.Clear();
Start_Game();
break;
}
}
static public void Start_Adventure()
{
Console.Clear();
Console.WriteLine("You're a normal..\nBoy/Girl?");
string BoyorGirl;
BoyorGirl = Console.ReadLine();
BoyorGirl = BoyorGirl.ToLower();
switch (BoyorGirl)
{
case "boy":
BoyorGirl = "Boy";
Console.Clear();
input_name();
break;
case "girl":
BoyorGirl = "Girl";
Console.Clear();
input_name();
break;
default:
Console.WriteLine("This is not an option..");
System.Threading.Thread.Sleep(2000);
Console.Clear();
Start_Adventure();
break;
}
}
public static void input_name()
{
Console.WriteLine("You're just a normal {0}, called.. input your name please.");
string name;
name = Console.ReadLine();
Console.Clear();
Console.WriteLine("You're just a normal {0}, called {1}..", BoyorGirl, name); //The error appears here.
}
static public void Help_Menu()
{
}
}
}
I also want to get rid of the statics, but I don't think that's possible. Any idea on how to fix this?

That variable only exists in the scope of the Start_Adventure() method.
If you want to use it in that method, you need to either delcare it globally or pass it into the method.
public static void input_name(string BoyorGirl)
{
//Do Work Here
}
OR
namespace ConsoleApplication1
{
class Program
{
private static string BoyOrGirl;
...
if you go with the second option, remember to remove this
string BoyorGirl;
from Start_Adventure()

Because BoyorGirl is a local variable to your Start_Adventure method. You need to pass that variable if you want to use it:
BoyorGirl = "Girl";
Console.Clear();
input_name(BoyorGirl);
// ....
public static void input_name(string BoyorGirl) {
}

Change your code to following:
static public void Start_Adventure()
{
...
input_name(BoyorGirl);
...
}
public static void input_name(string BoyorGirl)
{
Console.WriteLine("You're just a normal {0}, called.. input your name please.");
string name;
name = Console.ReadLine();
Console.Clear();
Console.WriteLine("You're just a normal {0}, called {1}..", BoyorGirl, name); //The error appears here.
}
The string BoyorGirl was not known in your input_name-method.
So you have to pass the string to the method or use a global var.

You defined the variable BoyorGirl in the scope of the method Start_Adventure(), it will not be available outside of that scope.
Therefore, you have to either change the variable to be a member of the class Program, or pass it to the input_name() method as an argument.

A variable declared inside a method is local to that specific method and cannot be accessed from elsewhere. As such, input_name has no idea what BoyorGirl is, because it's only found in Start_Adventure.
What you could do is to pass the variable to input_name:
public static void input_name(string BoyorGirl)
and
input_name(BoyorGirl);

Related

I want to know how to invoke a string input from another method

I'm new in c#, and I'm trying to make a text game in a console application. I did this code for an example because I want to know how to invoke a string input from another method
class Program
{
static void Main(string[] args)
{
b();
}
public static void b()
{
stops();
c();
}
public static string stops ()
{
string stop = Console.ReadLine();
//here i get the user input
Console.WriteLine(stop);
return stop;
}
public static void c()
{
string stop = stops();
Console.WriteLine("i need this:" + stop);
// here i want the user input
Console.ReadLine();
}
}
in this case, I didn't receive in the console the user input stop
I want to hold in my game the user name, but I'm using a lot of methods. In one method I ask the user his name, but I can't use his string in another method.
Now I'm trying to simplify my question, in the next example, the console application doesn't print here Console.WriteLine("your name is: " + name); this input string name = Console.ReadLine();
class Program
{
static void Main(string[] args)
{
Console.WriteLine("what is your name:");
userName();
string name = userName();
Console.WriteLine("your name is: " + name);
Console.ReadLine();
}
public static string userName ()
{
string name = Console.ReadLine();
return name;
}
}
The problem in your simple example is that you are calling the method twice - the first time you aren't capturing the output, and the second time you are. If you simply remove the first call to userName(), your code should work fine.
Here's a modified example:
static void Main()
{
string name = GetUserName();
Console.WriteLine("Hello, " + name + ". Nice to meet you!");
Console.ReadKey();
}
public static string GetUserName()
{
Console.Write("Please enter your name: ");
string name = Console.ReadLine();
return name;
}
Output
Now, if you want to use the userName throughout your program, you might consider setting it as a class-level variable. This means that all the methods in your class will have access to it.
Notice also that now you don't have to capture the output of the GetUserName method, because it will set a variable that you have access to already. We just call GetUserName, and then the GreetUser method has access to it:
// Variable set at this scope will be accessible to the whole class
private static string userName;
static void Main()
{
GetUserName();
GreetUser();
Console.ReadKey();
}
public static void GetUserName()
{
Console.Write("Please enter your name: ");
userName = Console.ReadLine();
}
public static void GreetUser()
{
Console.WriteLine("Hello, " + userName + ". Nice to meet you!");
}
I think you already answer your own question in your code.
In this case, when you have stops() that return the user input and c() that calls stops() again, which will ask for user input again. Based on your question, you want to take a string input from another method, which you already did in the c(). If you are trying to ask user input once only, then you can remove the stops() method inside the b() method.
class Program
{
static void Main(string[] args)
{
Init();
}
public static void Init()
{
AskUserInput();
}
public static string stops()
{
string stop = Console.ReadLine();
//here i get the user input
Console.WriteLine(stop);
return stop;
}
public static void AskUserInput()
{
string stop = stops();
Console.WriteLine("i need this:" + stop);
}
}
Also, as Rob mentioned, it's good practice to name your method with proper name. As later on when you start dealing with large project, this will help you a lot.
I also removed the last Console.ReadLine as I'm not sure what it does.
I believe the code after you edited your question is just fine with one slightly change. You don't have to call twice the method userName(). The first time the program is waiting for your input is from when you are not assigning the returned value to any variable. Then it is waiting for input again and there is where stores de variable and give you an output.
You just have to comment the line before string name ...
Here is your code working: fiddle

Public Int Error

I am trying to create a menu that goes to different sections without using a class but I am receiving errors using public int.
public int menu()
Console.WriteLine("Select a category to view");
Console.WriteLine("");
Console.WriteLine("1.Groceries");
Console.WriteLine("2.Electronics & Appliances");
Console.WriteLine("3.Exit");
Console.ReadKey();
int User = int.Parse(Console.ReadLine());
switch (User)
{
case 1:
Console.WriteLine("...........Groceries...............");
break;
case 2:
Console.WriteLine("..............Electronics & Appliances............");
break;
case 3:
Console.WriteLine("...........Exit...............");
break;
}
Turning your function into a static void method solves the problem. And you are missing the { after the menu(). Following your paradigm the code would be:
namespace ConsoleApplication1
{
class Program
{
static void menu()
{
Console.WriteLine("Select a category to view");
Console.WriteLine("");
Console.WriteLine("1.Groceries");
Console.WriteLine("2.Electronics & Appliances");
Console.WriteLine("3.Exit");
Console.ReadKey();
int User = int.Parse(Console.ReadLine());
switch (User)
{
case 1:
Console.WriteLine("...........Groceries...............");
break;
case 2:
Console.WriteLine("..............Electronics & Appliances............");
break;
case 3:
Console.WriteLine("...........Exit...............");
break;
}
}
static void Main(string[] args)
{
menu();
}
}
}

C# Constructors and Contexes

I am trying to create a console application that converts centimeters to meters
Here are my objectives
Store number of centimeters entered in an attribute
Use a default constructor to store zero in the attribute that stores the number of centimeters meters entered
Use a primary constructor to accept and store number of centimeters entered.
A function call getMeters to return the number of meters
A function called Remainder to get the number of centimeter remanding after conversion
A function called Printout that will display the results
The application should carry on accepting values for conversion until the user decides to end it.
What I have so far:
using System;
namespace Conv
{
public class Centimeter
{
public int cmvar;
public Centimeter()
{
cmvar = 0;
}
}
//primary const to be added
public class MeterToCenti
{
public static void Main()
{
char choice;
char n = 'n';
do
{
Console.WriteLine("Do you want to continue? (y/n)");
//choice = Console.ReadLine();
choice = Console.ReadKey().KeyChar;
Centimeter c = new Centimeter();
Console.WriteLine("enter value in centimeters");
c.cmvar = int.Parse(Console.ReadLine());
Printout();
}
while(choice.CompareTo(n) == 0);
}
public static void getMeterst()
{
int meters = c.cmvar / 100;
}
public static void Remainder ()
{
int cmremain = c.cmvar % 100;
}
public static void Printout()
{
Console.WriteLine("{0} Meters and {1} Centimeters", meters, cmremain);
}
}
}
I am getting errors regarding:
prog.cs(24,5): warning CS0168: The variable `meters' is declared but never used
prog.cs(41,11): error CS0103: The name `c' does not exist in the current context
prog.cs(41,2): error CS0103: The name `meters' does not exist in the current context
prog.cs(47,24): error CS0103: The name `c' does not exist in the current context
prog.cs(53,61): error CS0103: The name `meters' does not exist in the current context
prog.cs(53,69): error CS0103: The name `cmremain' does not exist in the current contex
Any help would be appreciated.
In some programming languages, a context is usually defined by { and }, meaning that given this:
{
int a = ...
}
a exists specifically within that block. Assuming that no other variable named a has been declared outside the braces, doing something like so:
{
int a = ...
}
print(a)
Will result in a fault, since a no longer exists.
In your case for instance, you are declaring the following variable: Centimeter c = new Centimeter();. Notice that this is enclosed within the do...while scope, so it exists only in there. Thus, when you try to access your variable from another method, which has its own scope, you get the exception you are getting.
To start solving the issue, you should move the 3 methods getMeterst, Remainder and Printout in their appropriate class, which is Centimeter.
I would recommend you start by looking at some tutorials, since you have other issues with your code.
As pointed out by #user2864740, different languages treat scopes differently. Taking Javascript in consideration:
function hello()
{
{
var i = 44;
}
{
alert(i);
}
}
Yields an alert with the value of 44.
However, the code below does not compile in C#:
private static void Hello()
{
{
int i = 0;
}
{
Console.WriteLine(i); //The name i does not exist in the current context.
}
}
you've got a lot of issues with scope in your code, when you declare something, it can (as a rule) only be accessed inside whatever set of brackets you declare it in, so when you try and access c and cmremainand things like that without specifying their location or accessing them correctly you get errors like this.
I have working code below, but feel free to ask any extra questions as to 'why' this works.
using System;
namespace Conv
{
public class Centimeter
{
public int cmvar;
public Centimeter()
{
cmvar = 0;
}
}
//primary const to be added
public class MeterToCenti
{
public static void Main()
{
char choice;
char n = 'n';
do
{
Centimeter c = new Centimeter();
Console.WriteLine("enter value in centimeters");
c.cmvar = int.Parse(Console.ReadLine());
Printout(c);
Console.WriteLine("Do you want to continue? (y/n)");
choice = Console.ReadKey().KeyChar;
Console.WriteLine();
}
while (choice != n);
}
public static int getMeters(Centimeter c)
{
int meters = c.cmvar / 100;
return meters;
}
public static int Remainder(Centimeter c)
{
int cmremain = c.cmvar % 100;
return cmremain;
}
public static void Printout(Centimeter c)
{
Console.WriteLine("{0} Meters and {1} Centimeters", getMeters(c), Remainder(c));
}
}
}
you need to learn oriented object programming before programming any oop language.
Basic is good but polute your mind by not thinking object, but sequencial...
Here is your code fixed
using System;
namespace Conv
{
public class Centimeter
{
public int cmvar;
public Centimeter()
{
cmvar = 0;
}
public Centimeter(int mm)
{
cmvar = mm;
}
public int getMeterst()
{
return cmvar / 100;
}
public int Remainder()
{
return cmvar % 100;
}
public void Printout()
{
Console.WriteLine("{0} Meters and {1} Centimeters", this.getMeterst(), this.Remainder());
}
}
public class MeterToCenti
{
public static void Main()
{
char choice;
char n = 'n';
do
{
Console.WriteLine("Do you want to continue? (y/n)");
choice = Console.ReadKey().KeyChar;
Console.WriteLine(); // for pure design needs
Centimeter c = new Centimeter();
Console.WriteLine("enter value in centimeters");
c.cmvar = int.Parse(Console.ReadLine());
c.Printout();
}
while (choice != n);
}
}
}

List containing class objects not working

I am working on a small C# program that will store student records in a list. I need to use a class to hold each student record, which is the top class in my code sample below.
If the user chooses to create a new record, I place their input into a studentRecord variable and then add it to the recordList. However, when I attempt to display the number of student records currently available using Count(), the program does nothing except re-display the menu, as if totally ignoring my command to display. I think something is wrong with how/where I declared the list, or with how I create a new StudentRecord object each time the menu is run.
Also, all three methods must stay in the first class. Is there any way to fix this?
public class StudentRecord
{
//Declare the various fields of the class
private string strFirstName;
private string strLastName;
private int intCourses;
private int intCreditHours;
List<StudentRecord> lstRecords = new List<StudentRecord>();
//Declare the properties of the class, since the fields are private
public string StrFirstName
{
get
{
return strFirstName;
}
set
{
strFirstName = value;
}
}
public string StrLastName
{
get
{
return strLastName;
}
set
{
strLastName = value;
}
}
public int IntCourses
{
get
{
return intCourses;
}
set
{
intCourses = value;
}
}
public int IntCreditHours
{
get
{
return intCreditHours;
}
set
{
intCreditHours = value;
}
}
//Declare a default constructor
public StudentRecord()
{
}
//Declare a constructor that takes the four necessary parameters, and set the class
// properties equal to the respective parameters
public StudentRecord(string firstName, string lastName, int courses, int creditHours)
{
strFirstName = firstName;
strLastName = lastName;
intCourses = courses;
intCreditHours = creditHours;
}
//Declare a method to perform the adding a student record function
public void mtdAddStudentRecord()
{
//These variables temporarily hold the various user inputs
string strInputFirstName;
string strInputLastName;
int intInputCourses;
int intInputCreditHours;
//Prompt the user to enter the student's first name
Console.Write("Please enter the first name: ");
strInputFirstName = Console.ReadLine();
//Prompt the user to enter the student's last name
Console.Write("Please enter the last name: ");
strInputLastName = Console.ReadLine();
//Prompt the user to enter the student's number of courses
Console.Write("Please enter the number of courses: ");
intInputCourses = int.Parse(Console.ReadLine());
//Prompt the user to enter the student's completed credit hours
Console.Write("Please enter the number of completed credit hours: ");
intInputCreditHours = int.Parse(Console.ReadLine());
//Add the new student record to the list, using the paramaters of the second
// class constructor
lstRecords.Add(new StudentRecord(strInputFirstName, strInputLastName,
intInputCourses, intInputCreditHours));
}
//Declare a method to perform the display student information option
public void mtdDisplayStudentInformation()
{
Console.WriteLine("Capacity: {0}", lstRecords.Count);
}
//Declare a method to perform the edit student information option
public void mtdEditStudentInformation()
{
//TODO
}
}
public class Program
{
public static void Main(string[] args)
{
//Declare and initialize a variable to store the user menu choice
string strMenuChoice;
strMenuChoice = "";
//Perform the necessary menu option while the user has NOT chosen to exit
do
{
//Call the reset method to clear the screen and display the header
mtdResetConsole();
//Display a menu to the user
Console.Write("Please choose an option to perform:" +
"\n - A) Display a list of existing students" +
"\n - B) Add a new student record" +
"\n - C) Edit an existing student record" +
"\n - D) Exit the program" + "\n\n" + " ");
//Store the user reply
strMenuChoice = Console.ReadLine().ToLower();
//Create a new StudentRecord object, and use it to call the various methods
// as chosen by the user
StudentRecord studentRecord = new StudentRecord();
//Determine which option was chosen, and take the appropriate action
switch (strMenuChoice)
{
case "a":
//TODO - Necessary code for option A
mtdResetConsole();
//Perform the "view student records" method
studentRecord.mtdDisplayStudentInformation();
break;
case "b":
//TODO - Necessary code for option B
mtdResetConsole();
//Perform the "add student record" method
studentRecord.mtdAddStudentRecord();
break;
case "c":
//TODO - Necessary code for option C
break;
case "d":
//Exit the program
Environment.Exit(0);
break;
default:
mtdResetConsole();
Console.WriteLine("Error" + "\n" +
" - Please choose a valid option from the list");
//Pause the code from executing for 2.5 seconds, so that the error
// message will be displayed
System.Threading.Thread.Sleep(2500);
break;
}
}
while (strMenuChoice.ToLower() != "d");
}
//Declare a method to reset the console with a blank screen and header
public static void mtdResetConsole()
{
Console.Clear();
Console.WriteLine("CONESTOGA STUDENT RECORDS" + "\n");
}
}
First, put this outside do while
StudentRecord studentRecord = new StudentRecord();
Putting this inside do while means that the object will reset in every loop.
2nd, try to put something like Console.ReadLine() on mtdDisplayStudentInformation method to hold the screen.
public void mtdDisplayStudentInformation()
{
Console.WriteLine("Capacity: {0}", lstRecords.Count);
Console.ReadLine();
}
Kendall, try to separate the logic of your "Records" and your "List of records".
In the case bellow, you have a "list of records" for each record you create.
Suggestions are you create a static variable to make sure it is always the same list on all "Records" or you separate as bellow:
public class StudentRecord
{
//Declare the various fields of the class
private string strFirstName;
private string strLastName;
private int intCourses;
private int intCreditHours;
...
}
public class Program
{
private List<StudentRecord> records = new List<StudentRecord>();
public static void Main(string[] args)
{
...
}
}

I need a simple and elegant user validation technique for a c# console application

Coming from a procedural background, I'm running into a conceptual block while designing a menu-based console application and user input validation. My goal is to display a menu that launches other processes. I want to limit user input to 1, 2, or 3 at the menu.
In a procedural language, I would do something like this pseudocode:
10 print "Make a choice"
20 choice = [dataFromKeyboard]
30 if choice < 4 && choice > 0
40 then 10
50 else 60
60 process valid choices
and no matter what I try, I can't get that out of my head while designing an OO program. Consider (simplified to include only 3 menu items):
class Menu
{
public static void Main(String[] args)
{
DisplayMenu thisdm = new DisplayMenu;
int menuChoice = thisdm.displayMenu();
ProcessMenu thispm = new ProcessMenu();
thispm.processMenu(menuChoice);
}
}
class DisplayMenu
{
public int displayMenu()
{
Console.WriteLine("1 - foo3");
Console.WriteLine("2 - foo2");
Console.WriteLine("3 - foo3");
Console.WriteLine("choose");
String choice = Console.ReadLine();
int intChoice = Convert.ToInt32(choice);
return intChoice;
}
}
class ProcessMenu
{
public void processMenu(int choice)
{
switch(choice)
{
case 1:
foo1();
break;
case 2:
foo2();
break;
case 3:
foo3();;
break;
default:
Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
break;
}
}
}
So here's where I'm stuck. I just can't wrap my head around a simple and elegant way validate my user input that's from an OO rather than procedural standpoint.
Assuming I do the validation in the DisplayMenu, I would be validating after the input is read. But if it turns out to be invalid, how do I re-ask for valid input, since I've already called displayMenu method from Main?
I've been playing with while loops for about an hour, something like this:
intChoice = 0;
[print the menu]
while ((intChoice<1) || (intChoice>3))
Console.WriteLine("Please make a valid choice from the menu");
choice = Console.ReadLine();
etc.
but can't seem to find the sweet spot where I can control user input.
I suspect it's because I'm thinking to procedurally, and not object-oriented enough. Anyone have any tips or input to help me wrap my head around this?
Expanding on #AlexeiLevenkov's suggestion of "turning your classes 90 degrees", I went a step further and created this example of a "Modular" console Application:
class Program
{
static void Main(string[] args)
{
//Retrieve all Module types in the current Assembly.
var moduletypes = Assembly.GetExecutingAssembly()
.GetTypes()
.Where(x => x.IsSubclassOf(typeof(ConsoleModule)));
//Create an instance of each module
var modules = moduletypes.Select(Activator.CreateInstance)
.OfType<ConsoleModule>()
.OrderBy(x => x.Id)
.ToList();
int SelectedOption = -1;
while (SelectedOption != 0)
{
//Show Main Menu
Console.Clear();
Console.WriteLine("Please Select An Option:\n");
modules.ForEach(x => Console.WriteLine(string.Format("{0} - {1}", x.Id, x.DisplayName)));
Console.WriteLine("0 - Exit\n");
int.TryParse(Console.ReadLine(), out SelectedOption);
//Find Module by Id based on user input
var module = modules.FirstOrDefault(x => x.Id == SelectedOption);
if (module != null)
{
//Execute Module
Console.Clear();
module.Execute();
Console.WriteLine("Press Enter to Continue...");
Console.ReadLine();
}
}
}
ConsoleModule class:
public abstract class ConsoleModule
{
public int Id { get; set; }
public string DisplayName { get; set; }
public abstract void Execute();
}
Some sample Modules:
public class EnterUserNameModule : ConsoleModule
{
public EnterUserNameModule()
{
Id = 2;
DisplayName = "User Name";
}
public static string UserName { get; set; }
public override void Execute()
{
Console.WriteLine("Please Enter Your Name: ");
UserName = Console.ReadLine();
}
}
public class HelloWorldModule: ConsoleModule
{
public HelloWorldModule()
{
Id = 1;
DisplayName = "Hello, World!";
}
public override void Execute()
{
Console.WriteLine("Hello, " + (EnterUserNameModule.UserName ?? "World") + "!");
}
}
public class SumModule: ConsoleModule
{
public SumModule()
{
Id = 3;
DisplayName = "Sum";
}
public override void Execute()
{
int number = 0;
Console.Write("Enter A Number: ");
if (int.TryParse(Console.ReadLine(), out number))
Console.WriteLine("Your number plus 10 is: " + (number + 10));
else
Console.WriteLine("Could not read your number.");
}
}
Result:
It uses a little bit of reflexion to find all types deriving from ConsoleModule in the current assembly, then shows a menu with all these options (which are actually properties in this class), and calls the Execute() method when an appropiate option is selected. Much more towards OO way of thinking.
Make your processMenu function return some kind of indicator. You could use exceptions for this instead, but that's overkill.
public bool processMenu(int choice)
{
....
}
If the choice was acceptable, then return true, otherwise return false. Then:
public static void Main(String[] args)
{
DisplayMenu thisdm = new DisplayMenu;
ProcessMenu thispm = new ProcessMenu();
int menuChoice;
do {
menuChoice = thisdm.displayMenu();
} while( !thispm.processMenu(menuChoice) );
}
The way you are doing should be changed. Anyhow, for the same as your question, this works out:
DisplayMenu thisdm = new DisplayMenu();
int menuChoice = -1;
while (menuChoice < 1 || menuChoice > 3)
{
Console.WriteLine("enter valid choice");
menuChoice = thisdm.displayMenu();
}
ProcessMenu thispm = new ProcessMenu();
thispm.processMenu(menuChoice);
the code like:
class Program
{
static void Main(string[] args)
{
DisplayMenu thisdm = new DisplayMenu();
ProcessMenu thispm = new ProcessMenu();
thisdm.displayMenu();
int menuChoice = thispm.GetChoice();
thispm.processMenu(menuChoice);
Console.Read();
}
}
class DisplayMenu
{
public void displayMenu()
{
Console.WriteLine("1 - foo3");
Console.WriteLine("2 - foo2");
Console.WriteLine("3 - foo3");
Console.WriteLine("choose");
}
}
class ProcessMenu
{
public int GetChoice()
{
String choice = Console.ReadLine();
int intChoice = Convert.ToInt32(choice);
while (!Validate(intChoice))
{
Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
choice = Console.ReadLine();
intChoice = Convert.ToInt32(choice);
}
return intChoice;
}
public void processMenu(int choice)
{
switch (choice)
{
case 1:
//foo1();
break;
case 2:
//foo2();
break;
case 3:
//foo3(); ;
break;
default:
//Console.WriteLine("Invalid selection. Please select 1, 2, or 3.");
break;
}
}
private int[] forChoices=new int[]{1,2,3};
private bool Validate(int choice)
{
if(forChoices.Contains(choice))
{
return true;
}
return false;
}
}

Categories