Saturday, March 29, 2008

Converting XNA PlayerIndex to string and int

It might be obvious, but I needed it today for the networking stuff to pass player data around. I wanted to convert the current Playerindex to a string or int and vice versa.:


PlayerIndex pix = PlayerIndex.Two;
string ps = pix.ToString(); // "Two"
int pi = (int)pix; // 1
pix = (PlayerIndex) Enum.Parse(typeof (PlayerIndex), "Three");
ps = pix.ToString(); // "Three"
pi = (int) pix; // 2
pi = 0;
pix = (PlayerIndex) pi;// "One"


No comments:

CSharpCodeFormatter