3
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?

4
  • Why is everyone downvoting this question? Its programming related, and he needs help with something. Commented Mar 17, 2009 at 11:44
  • thanks for u support actually my problem is since i use split i have to assign it to a string [] but i have no idea how to declare a dynamic tring array Commented Mar 17, 2009 at 11:55
  • i downvoted him because he doesnt use any punctuation or capital letters and i think that this behavior reduces the quality of this forum however if he changes this i will happily remove my downvote Commented Mar 17, 2009 at 12:11
  • I think i have done the necessary changes and i will follow them here after . Commented Mar 17, 2009 at 12:15

4 Answers 4

7

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.

Sign up to request clarification or add additional context in comments.

8 Comments

the problem when i declare it outside is since i split the string the array size increase beyond the z value for eg for one loop cycle 3 strings may be added so how do i solve this problem
Explain what you're trying to do with the two string arrays in a few clear sentences. We may be able to propose a better way to do it.
alMethSign[z] contains (int32,int32) i split with a separator "," so the size of the string [] cant be known so i am confused how to dynamically declare a string []
It sounds increasingly like you shouldn't be using a string array at all. Use a List<String> instead.
what reference we should add in order to use list data type
|
2

The scope of both asd and def are limited to the body of the for loop. If you have to access them you need to declare them outside the loop. Is there a problem in putting them out?

Take a look at the Collection Classes Tutorial on MSDN.

3 Comments

ya i cant put them out because i dont know the correct size they increase dynamically
If you have a dynamically increasing array, an array is the wrong data structure to use. Use a List instead. If you need to convert back to an array later, use the ToArray method of the List.
u cant cast a strings.collections to array list
0

Both 'asd' and 'def' are string arrays whose scope is limited to the for loop. You cannot access them outside the loop. If you want to do so, try declaring them outside the for loop.

Comments

0

First, if you want access to the data extracted/computed inside the loop, you must declare a container for the results outside the loop, and then populate its values inside the loop.

Second, don't think about casting the arrays returned from the split method, but rather think about processing their contents.

Assuming that you want the combined results from all elements of the original alParmValues array in a single pair of results, I'd use something like the following pseudo-code. Of course, you'll need to fill in the type for your alParmValues and alMethSign elements, add semicolons, etc. (Because your question didn't explain the content and relationships between the two arrays being processed in your loop, I've just treated them independently.) This isn't complete code, but just a sketch to get you started:

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)
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.