C# - find the position of a string inside a predefined string array -
i have predefined string array letters a
q
:
string[] skippedareasarray = new string[] {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q"};
in textbox
inside windows form user can enter skippedareas this: a,b,c,d...
there validations , restrictions use letters , commas input guaranteed in format.
what taking user input , populate string array:
string[] skippedareasfromform = new string[17]; ... skippedareasfromform = (txtskippedareas.text).split(',');
now comes tricky part i'm seeking assistance. user must enter number of areas
example - 3. mean working a
, b
, , c
. if number of areas 2 can use a
, b
if number of areas 4 a
, b
, c
, d
available , on.
what need check if in array skippedareasfromform
populated user input there area doesn't match above criteria.
what mean in terms of coding - need take every element skippedareasfromform
, take it's integer value predefined skippedareasarray
, see if value equal or greater (>=) of value entered `number of areas. if there area outside scope of selected number error should shown.
what have right :
foreach (string tempareavalue in skippedareasfromform) { (int = 0; < skippedareasarray.length; i++) { if (tempareavalue == skippedareasarray[i]) { if ((i + 1) > entity.areascnt) { messagebox.show("you must use areas within number of areas scope!", "error", messageboxbuttons.ok, messageboxicon.error); txtskippedareas.focus(); return false; } } } }
for few test made works. first - @ least me seems on complicated. second - i'm not sure algorithm working need or i'm having correct results luck. third - i'm coding c# 2months , seems me excellent candidate linq expression - think better using linq , appreciate in transformation.
i think you're looking indexof
:
int index = skippedareasarray.indexof(tempareavalue); if (index >= entity.areascnt) { ... }
(you might want check index
being -1, occur if element wasn't in list @ all. also, consider duplicates - can user enter a
, a
, a
?)
Comments
Post a Comment