How do I convert a string to an integer in c#???????? Weird

void somefunction()
{
string mystringy = "20";
int myinty =12;

myinty = (int)myStringy; //This throws an error??

myinty = Convert.ToInt32(mystringy); //This throws an error???
}

Am I doing something wrong with any of the above?
Or is there another method I’m missing??

ok myinty = int.Parse(mystringy);

This doesn’t throw an error so I’m going to guess that this is the way to go.

Am i correct?

That is correct.

Well, it will generate an error if you try to parse something like “ABC”. Probably better to use int.TryParse() unless you’re 100% sure the string can never include anything but numbers.

–Eric

Good point

Eric got all technical on me :lol:

Yeah, he is correct though.