ill get right to the point. I am using regex to get a match from a settings file. It simply grabs the default values. I take the match, have it print the String match. then I use Convert.toInt32(match) and put that into an int tempval. Here is the code.
string[] settings = System.IO.File.ReadAllLines("Settings.txt");
MatchCollection settingsmatch;
Regex expression = new Regex(@"first number: (\d+)");
settingsmatch = expression.Matches(settings[0]);
MessageBox.Show(settingsmatch[0].Value);
int tempval = Convert.ToInt32("+" + settingsmatch[0].Value.Trim()); //here is the runtime error
numericUpDown1.Value = tempval;
here is the settings text file:
first number: 35
second number: 4
default test file: DefaultTest.txt
I know that the problem is the Convert because I commented out the numericupdown line and still got the error in runtime.
It is a Formatexeption error. I dont get it. I though that my match is a string so Convert should take it. Furthermore the messagebox.show is showing me a number. The number 35 to be exact. What else could cause this?
what you are doing is converting the value of entire match to Integer
. You need to convert the value of first group capture.
// convert the value of first group instead of entire match
// settingsmatch[0].Value is "first number: 35"
// settingsmatch[0].Groups[1].Value is "35"
int tempval = Convert.ToInt32("+" + settingsmatch[0].Groups[1].Value);