6

I have an array like so

Dim array() As String = {}

and the following code

For i = 0 To membertable.Rows.Count - 1
    If InStr(membertable.Rows(i)("name"), txtSearch.Text, CompareMethod.Text) - 1 _
       <> -1 And txtSearch.Text.Length >= 3 Then

        found = True

        'add the item that matches the criteria to the array here.

    End If
Next i

So the code loops through the rows of an access table and every time it finds a value under the "name" column that matches the criteria I want to add that item to the array. the database item will always be a string.

3
  • could you please mention the input string and required output Commented Sep 11, 2014 at 16:47
  • The input string is a name for example inputting "Ben" into the textbox would result in the code finding all the names in the database that contain the string "Ben". I want all these strings to be put into an array. Commented Sep 11, 2014 at 16:51
  • Why are you looping through the rows, why not use the text as search criteria to just get the rows that contain the text? Commented Sep 11, 2014 at 16:54

4 Answers 4

16

Arrays have a fixed length. Use a List(Of String) instead:

Dim list As New List(Of String)()

...

list.Add(someString)

Note: Lists use arrays internally and resize them automatically (basically doing the same as Redim Preserve). Instead of increasing the list size by one element at each addition, they start with an array size of 4 and double its size each time the array gets too small. This reduces the number of copy operations needed, since increasing the size of an array means to create a new array and to copy the contents of the old one to the new one.

So, there is really no point in using Redim yourself, as lists make it automatically and efficiently for you.


By the way InStr(...) - 1 <> -1 is a strange condition. What is its purpose? InStr(...) <> 0is equivalent. Shouldn't the condition be InStr(...) <> -1? Or membertable.Rows(i)("name").Contains(txtSearch.Text)?

Sign up to request clarification or add additional context in comments.

2 Comments

What about myArray.SetValue('ben' ,i)
Why not simply myArray(i) = "ben"? But this updates an exiting array position. It does not add a new one, i.e., the size of the array remains the same. If you have an array of length 10, you cannot add an eleventh element. You can only do this with lists.
3

To answer your question, you need to re-dimension you array each time you want to add another item:

Redim Preserve array(array.length)

Then add your item to the last one:

array(array.length - 1) = ???

The important thing is using the PRESERVE keyword. Without that, your array will be cleared.

The better way would be to not use an array at all but to use a collection or list.

Comments

1

Use a List(Of String) instead of an array. Also you could LINQ the results. It is also better not to name a variable the same name as a Data Type.

Dim myList = (From dr As DataRow In membertable.Rows Where dr("name").ToString = txtSearch.Text).ToList

Comments

0

It depends on how often you add elements to your array. When it happens many times, you should not use any form of arrays including Lists. Maybe LinkedLists are what you're searching for. They provide efficient adding (especially anywhere, further deleting is efficient too). Their only disadvantage is "slow" O(n) indexing. Sequential For Each lookup is always O(n) and there is hardly any difference between them and arrays.

And if you just create the elements and then process them, you can use Iterator Functions (also possible as lambda inside your procedure).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.