This is my latest obstacle:
So far I have:
Console.Write("Enter player's position: ");
string sPosition = Console.ReadLine();
I have already defined a position enumerator. How do I convert sPosition into my enumerator?
I want to put Convert.ToEnum or Enum.parse.
Jan MontanoPosted Dec 29, 2008, 2:12 AM
you need to cast the integer value to Player.Position
Simon HortonPosted Dec 28, 2008, 6:25 PM
It falls apart at:
sPosition = Convert.ToString(position);
and:
Player.Position e = Convert.ToInt32(a);
Intellisense shows ConvertTo.everything except my predefined enumerator. Is there a solution now in c# or do I need to go back a step and ask the user to enter 1 or 2 or 3, etc
Simon HortonPosted Dec 28, 2008, 6:15 PM
Here's the code:
namespace
ConsoleApplication1{
class Player{
public enum Position : int { GK, RB, LB, CB, RM, LM, CM, ST }; public Position position; public string sPosition; public string firstName; public string lastName; public int squadNumber; public string sSquadNumber; public void SetDetails(Position posn, int sNumber, string fName, string lName){
position = posn;
sPosition =
Convert.ToString(position);firstName = fName;
lastName = lName;
squadNumber = sNumber;
sSquadNumber =
Convert.ToString(squadNumber);}
public string ToDetailsString(){
string s = position + " " + sSquadNumber + " " + firstName + " " + lastName + " "; return s;}
}
class Program{
static void Main(string[] args){
Console.Write("Enter player's position: "); string a = Console.ReadLine(); Player.Position e = Convert.ToInt32(a); Console.Write("Enter player's number: "); int b = Convert.ToInt32(Console.ReadLine()); Console.Write("Enter player's first name: "); string c = Console.ReadLine(); Console.Write("Enter player's surname: "); string d = Console.ReadLine(); Player p1 = new Player();p1.SetDetails(e, b, c, d);
Console.WriteLine("Player: " + p1.ToDetailsString()); Console.Read();}
I appreciate the design of the program is poor but I'm just trying to get my head around converting value types.
Bechir BejaouiPosted Dec 27, 2008, 7:16 PM