VB.net build an array step by step

Murray Brown
Murray Brown used Ask the Experts™
on
Hi

What is the best way to build up items in a string array so that the array is easy to loop through. If for example I have 5 columns of data contain country names, I want to loop through all five and get list of unique items. So for each column item I first check if it is in the array, and if not then I add it to my array (which then has one more item)
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
What are the columns of data? DataColumns (of DataTable) or DataGridView columns, and which particular columns specifically contain the distinct country names you want?
Glanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015
Commented:
Arrays are immutable which means they cannot be changed* without creating an entirely new array. You might find it easier to work with something like a List or a Dictionary. While in terms of processing power you won't be saved from the copying of elements from an existing array to a new array, all of this is handled by the classes themselves. You just worry about adding (or removing) elements from the collection; the class will resize the internal storage as necessary.

* By this I don't mean that you cannot change any individual element. Rather I mean that if you want shrink or grow the array, then you must create a new array and copy the existing elements to the new array.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
The columns contain Names
Expert Spotlight: Joe Anderson (DatabaseMX)

We’ve posted a new Expert Spotlight!  Joe Anderson (DatabaseMX) has been on Experts Exchange since 2006. Learn more about this database architect, guitar aficionado, and Microsoft MVP.

Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks Kaufmed. How would I create a simple list
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Would adding a class called MyNames be a good idea
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
thanks very much
kaufmedGlanced up at my screen and thought I had coded the Matrix...  Turns out, I just fell asleep on the keyboard.
Most Valuable Expert 2011
Top Expert 2015

Commented:
Sorry for the delay. I had my laptop stripped apart.

How would I create a simple list
A simple list could be:

Dim aList As New System.Collections.Generic.List(Of String)()

aList.Add("one")
aList.Add("two")
aList.Add("three")

Open in new window


Checking the list could be:

If Not aList.Contains("four") Then
    aList.Contains("four")
End If

Open in new window


Would adding a class called MyNames be a good idea
The great thing about generics is that you can add whatever type you like to a new List. If you were to create a class named "MyNames", you could declare you List as such:

Dim aList As New System.Collections.Generic.List(Of MyNames)()

Open in new window


...and then just add new instances as necessary:

Dim m1 As New MyNames()
Dim m2 As New MyNames()
Dim m3 As New MyNames()

aList.Add(m1)
aList.Add(m2)
aList.Add(m3)

Open in new window


The checking for existing objects would be a bit more involved, but not very difficult.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks very much. I appreciate the time you took

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial