views:

776

answers:

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

+2  A: 

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.

dirkgently
ya i cant put them out because i dont know the correct size they increase dynamically
Arunachalam
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.
John Feminella
u cant cast a strings.collections to array list
Arunachalam
+6  A: 

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.

John Feminella
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
Arunachalam
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.
John Feminella
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 []
Arunachalam
It sounds increasingly like you shouldn't be using a string array at all. Use a List<String> instead.
John Feminella
+1 for use a List<string>
Ian Nelson
what reference we should add in order to use list data type
Arunachalam
k i got it System.Collections.Generic; rite?
Arunachalam
Yep, that's right. If you get stuck trying to transform it, post a new StackOverflow question and we'll continue the discussion there, since I think this one is probably solved.
John Feminella
i am getting this error cannot convert from 'string[]' t0 'string'
Arunachalam
A: 

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.

pragadheesh
A: 

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)
    }
}
joel.neely