This is how:
string[] IDs = hdnFldSelectedValues.Trim().Split('|');
int[] Values = new int[IDs.Length];
for(int index = 0; index < Values.Length; index++)
Values[index] = int.Parse(IDs[index]);
This code will throw exception on first invalid numeric format of out of range. If invalid format should be converted to a special value indicating error (not recommended) use
int.TryParse
:
string[] IDs = hdnFldSelectedValues.Trim().Split('|');
int[] Values = new int[IDs.Length];
for(int index = 0; index < Values.Length; index++) {
int value;
if (!int.TryParse(IDs[index], out value))
value = -1;
Values[index] = value;
}
In last example, failure to parse a string will be converted to -1.
Again, throwing exception is better.
—SA