954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

ArrayList to multi-dimensional string array

Consider the following code snippet:

counter = 0;
workOperations = new String [OPERATIONS.Count,2];
foreach ( System.Array o in OPERATIONS)
{
  workOperations[counter,0] = (string) o.GetValue(0,0);
  workOperations[counter,1] = (string) o.GetValue(0,1);
  counter++;
}
ps.Operations = workOperations;

There has got to be a better way to do this! The issue is, theps.Operations property requries a 2-dimensional string array. (It's a list of operations to peform on a PostScript file, along with the parameters needed for each operation.)

I build that array dynamically, which means creating an ArrayList, which is "OPERATIONS" in the code. The challenge is to get the members of the ArrayList into a suitable string array. The above code is my solution.

There should be a way to do this without resorting to a loop. Just a couple of lines of code with some judicious casting, but I couldn't make anything work.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

you should beable to use Array.Copy();
or since your using an arraylist do OPERATIONS.CopyTo();

plazmo
Posting Whiz in Training
207 posts since Aug 2005
Reputation Points: 23
Solved Threads: 16
 

I think I tried that, but can't remember. I'll re-investigate. I think one of the issues is that each element of the ArrayList contains a 2-element array...

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You