How to access the outer loop of a string array

for (int z = 0; z < alParmValues.Count; z++)
{
   //string[] def;
   string[] asd = alParmValues[z].ToString().Split(',');//this is of type string.collections and u cant cast it to a arraylist or array 
   //if (HUTT.clsParameterValues.bCustomObj == false)

   string[] def = alMethSign[z].ToString().Substring(alMethSign[z].ToString().IndexOf('(') + 1, alMethSign[z].ToString().IndexOf(')') - (alMethSign[z].ToString().IndexOf('(') + 1)).Split(',');
}

I need to access arrays of strings outside of a loop. Is there a better solution for this? I cannot use ArrayList or declare them public, so how can I access them?

+3
source share
4 answers

To access something outside the loop, simply declare it outside the loop, and then work with it after the loop has finished processing:

string[] arr = ...

for (int z = 0; z < alParmValues.Count; z++)
{
  // work with arr...
}

var item = arr[3]; // Accessed outside of loop.

However, there seems to be something wrong with your code. I would recommend thinking a bit about the body of the loop and what you are trying to do there. Consider this line, for example:

for (int z = 0; z < alParmValues.Count; z++)
{
  // ...
  string[] asd = alParmValues[z].ToString().Split(',');

  // There aren't any more references to asd after this point in the loop,
  //   so this assignment serves no purpose and only keeps its last assigned
  //   value.
}

; , , asd, .

+7

asd, def for. , . ?

> MSDN.

+2

"asd" "def" , for. . , for.

0

-, , / , , .

-, , , split, .

, alParmValues , - . , alParmValues alMethSign, .. ( , , .) , , :

ArrayList allValues = new ArrayList()
foreach (??? parameter in alParmValues) {
    foreach (String value in parameter.ToString().Split(',')) {
        allValues.add(value)
    }
}

ArrayList allMethSignValues = new ArrayList()
foreach (??? methSign in alMethSign) {
    String thisString = methSign.toString()
    int open = thisString.indexOf('(')
    int close = thisString.indexOf(')')
    String parenPart = thisString.substring(open + 1, close - open - 1)
    foreach (String value in parenPart.split(',')) {
        allMethSignValues.add(value)
    }
}
0

Source: https://habr.com/ru/post/1704885/


All Articles