Array question

  • Thread starter Thread starter Nikhil Patel
  • Start date Start date
N

Nikhil Patel

I have a Jagged array that stores RoutingIDs and theirs corresponding Bank
Names.
I can access it as myArray[row][column]. The column index indicates whether
the value is RoutingID or a BankName.

myArray[0][0] - first bank's RoutingID
myArray[0][1] - first bank's Name
myArray[1][0] - second bank's RoutingID
myArray[1][1] - second bank's Name
....

Now I want to perform few operations on this array. But don't know what is
the best way.

1) I would like to display all Routing IDs in a combobox.
2) I would like to display all distinct Bank Names in another combobox.
NOTE: I need to display distinct Names. The array may contain same Bank Name
more than once with different Routing IDs.
3) When they select an item in either of the two comboboxes, I would select
their corresponding value in the second combobox.

Thanks.
 
I would have to suggest switching from that jagged array to a DataTable.
First off, the DataTable can act as a small database, allowing sorting and
simple queries against it and will make it a lot easier to do things like
this. The trade off *might* be a bit more memory used for the DataTable and
also a bit more code to load it. But it should be easy to use the same code
that you are using to load your array.

Using the jagged array: (Note, I use ArrayLists for temp storage. I find
that they perform better than calling Array.Copy after every insert.)

To get all the Routing IDs:

<pseudo-code>
ArrayList tempArray = new ArrayList();
for(int r = 0; r < myArray.Length; r++)
{
tempArray.Add(myArray[r][0]);
}
return (string[])tempArray.ToArray(typeof(string));
</pseudo-code>

For getting all distinct bank names just do the same thing

<pseudo-code>
ArrayList tempArray = new ArrayList();
for(int r = 0; r < myArray.Length; r++)
{
if(!tempArray.Contains(myArray[r][1]))
{
tempArray.Add(myArray[r][0]);
}
}
return (string[])tempArray.ToArray(typeof(string));
</pseudo-code>

For the third one, I think the FindString or FindStringExact method of the
DropDownList will get you what you need. You could also use the
SelectedIndex property if you are bound your array.

--
HTH

Kyril Magnos

Question of the day:
What is Mono?
A) Disease where the lymph nodes become swollen.
B) A single sound
C) A synonym for one
D) A port of .NET meant to royally irritate MSFT
E) All of the above.

|I have a Jagged array that stores RoutingIDs and theirs corresponding Bank
| Names.
| I can access it as myArray[row][column]. The column index indicates
whether
| the value is RoutingID or a BankName.
|
| myArray[0][0] - first bank's RoutingID
| myArray[0][1] - first bank's Name
| myArray[1][0] - second bank's RoutingID
| myArray[1][1] - second bank's Name
| ...
|
| Now I want to perform few operations on this array. But don't know what is
| the best way.
|
| 1) I would like to display all Routing IDs in a combobox.
| 2) I would like to display all distinct Bank Names in another combobox.
| NOTE: I need to display distinct Names. The array may contain same Bank
Name
| more than once with different Routing IDs.
| 3) When they select an item in either of the two comboboxes, I would
select
| their corresponding value in the second combobox.
|
| Thanks.
|
|
|
 
Hi
1) I would like to display all Routing IDs in a combobox.
For that you should loop on the first index(lets say you will use a
variable int i for the loop)( and on that loop you add MyArray[0] to the
items collection of your combobox .
One more simple way to bind the combobox to the array and select the index
routingID as the display member

2) I would like to display all distinct Bank Names in another combobox.
This best done in a loop this time add MyArray[1] to the combobox
items collection , and you need to check for duplicate before adding the
item.
3) When they select an item in either of the two comboboxes, I
would select
their corresponding value in the second combobox.
You write a handler for selection changed event in both
combobox, for the first , you search for a match looping on MyArray[0]
and display the correspondence value at [1] , and the opposite would be
done in the other combo handler
hope that helps
Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
Back
Top