for loop

G

Guest

I have a function that returns array, i want to call this function inside a
for loop as shown below.. the problem i have i i always get the result for
the .gif as it is the last entry in ext array. how do i combine the results
and maybe use a seperator in between each results? many thanks

string[] ext = new string[3];
ext[0] = "*.mp3";
ext[1] = "*.bmn";
ext[2] = "*.gif";

string[] strCol = new string[3];
for(int x = 0; x < ext.Length; x++)
{
strCol = DirSearch("C:\\TEMP",ext[x]);
}
DataGrid1.DataSource = strCol;
DataGrid1.DataBind();
 
G

Guest

I am not sure what expected output do you need, but obviously, you could try
this way

strCol = strCol +"," + DirSearch("C:\\TEMP",ext[x]); //assume that you use,
as seperator, then your expected output might be :

result1,result2,result3

then u can use the String.split function to seperate the strign into a
string arrays, or do whatever you need to do.

regards,
Low
 
W

William F. Robertson, Jr.

You could look at merging your arrays.

string [] strcol = new string [0];
for( int x = 0 ; x < ext.Length ; x++ )
{
MergeArray( strCol, DirSearch( @"C:\temp", ext[x] ), ref strCol );
}

//this method will combine the two arrays into the third paramter passed by
ref.
private static void MergeArray( string [] array1, string [] array2, ref
string [] output )
{
string [] newArray = new string[array1.Length + array2.Length];

Array.Copy( array1, newArray, array1.Length );
Array.Copy( array2, 0, newArray, array1.Length, array2.Length );

output = newArray;
}

This solution will continue to combine the string array results from your
DirSearch method into one array. When you bind it to your datagrid, it will
show all the items in the array.

bill
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top