Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to access a string array outside loop

Tags:

arrays

c#

loops

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 have to access both the string arrays outside the loop. Is there a better solution to this? I can't use an ArrayList or declare them as public so how can I access them?

like image 465
Arunachalam Avatar asked Dec 06 '25 05:12

Arunachalam


1 Answers

To access something outside of a loop, just declare it outside of the loop, then work with it after your loop processing is done:

string[] arr = ...

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

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

However, there seem to be a few things wrong with your code. I'd recommend thinking a little bit more about the loop body and what you're 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.
}

This assignment is pointless; every time you go through the loop, you just overwrite the previous value of asd, and you never use it later in the loop.

like image 108
John Feminella Avatar answered Dec 07 '25 19:12

John Feminella



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!