c# - Can you call a place in an array with a char that is a valid int but defined as a string? -
for example, have array (called array1): {"1","2","3"}
and string (called string1): "0"
can following: array1[string1]
and have equal: "1"
i have checked how can evaluate c# expression dynamically? , call c# function defined in string did not understand answers. apologize.
when try string1 = int.parse(string1);
error:
or string1 = convert.toint32(string1); :
cannot implicitly convert type 'int' 'string'
to elaborate, (sorry if description wasn't clear), code this:
btnname = btn.name.tostring(); //btnname example == btn01 btnco1 = btnname[3].tostring(); // btnco1 equal "0" btnco2 = btnname[4].tostring(); //btnco2 equal "1" btnco1 = convert.toint32(btnco1); //i want btnco1 int use : btnco2 = convert.toint32(btnco2); grid[btnco1][btnco2] //grid jagged array
you have parse string int, preferably using tryparse
:
int idx; if (int32.tryparse(string1, out idx)) console.writeline(array1[idx]);
other way using convert.toin32
. might throw exception if string1
cannot converted int, example if contains "hello world"
.
edit concerning yours: can´t change variables data-type once you´ve declared it. if btnco1
is string
can´t change int
, have define new variable it:
int idx1 = convert.toint32(btnco1); int idx2 = convert.toint32(btnco2); dosomething(grid[idx1][idx2]);
Comments
Post a Comment