Home > Mobile >  how to solve "Unable to cast object of type 'System.Text.RegularExpressions.Match' to
how to solve "Unable to cast object of type 'System.Text.RegularExpressions.Match' to

Time:04-27

I get an error when I try to equalize the first int value to an object of type short by splitting the string expression into two using regex match.

My code

 var splitedResolution = sub.ResolutionValue.Split('*');
 var resolutionwidht = Regex.Match(splitedResolution[0], @"\b\d \.?\d?\b");   
 resolutionReportsub.Width =Convert.ToInt16(resolutionwidht);
                       

CodePudding user response:

The Match-method returns a Match-object. This object also has a Success-property to determine if the string matched your pattern.

In order to get the actually matched string use theMatchObject.Value:

var splitedResolution = sub.ResolutionValue.Split('*');
var resolutionwidht = Regex.Match(splitedResolution[0], @"\b\d \.?\d?\b");   
resolutionReportsub.Width =Convert.ToInt16(resolutionwidht.Value);
  • Related