Actually that code is gong to produce something like "; expected" as it won't even compile. When I fix that problem your code appears to work ... unless of course the label text is completely blank.
You either need to start with the label containing the text "Result = 0" OR put a condition around the calculation of
labVal
, setting it to 0 if the condition fails e.g.
var labVal = label1.Text.Length > 0 ? Int32.Parse(Regex.Match(test, @"\d+").Value) : 0;
Edit:
Snippets of other approaches you could use
- This will get rid of the Result = from the label
label1.Text.Replace("Result = ", "")
- You could split the string e.g.
var res = label1.Text.Split(' ','=');
Console.WriteLine("{0}",res[res.Length-1]);
Finally, a couple of other comments
- you don't need the
else
part of your
if
-statement as you are just leaving the label text as is.
- You haven't included the "Result = " text when you are assigning to the label text
- It is bad practice to assign numeric values to text - use the .ToString() method or Format the string e.g.
label1.Text = string.Format("Result = {0}",result);