0

I'm trying to search an array for previous entries from a user inputted text box that match new incoming entries. Is there any way to do this in Visual Basic? I'm converting my code from C# and Visual Basic keeps giving me an error "Object reference not set to an instance of an object." With this statement, the code skips the if block to check for matching text because arrayName(i) or 0 in this case is currently NOTHING. If i take out this if block and it reaches the name check, then it causes an error because there is nothing in arrayName(i) to convert to upper string.

So here's my code..My question again was is there an easier way to search previous entries from an array to newly input entries.

Edit: details

This is the array declaration Dim arrayName() = New String(2) {} and when it gets to If arrayName(i).ToString.ToUpper = txtInput.Text.ToUpper Then it says "Object variable or With block variable not set." "NullReferenceException was unhandled by user code". The "x" in the code is the fixed length of the array, which is 2 in this case.

Dim i As Integer = 0
            While x >= i
                If arrayName(i) IsNot Nothing Then
                    If arrayName(i).ToString.ToUpper = txtInput.Text.ToUpper Then
                        match = False
                        lblName.Text = "Enter a unique name"
                    End If
                End If
                    i += 1
            End While
20
  • You probably have the same question as this thread :) Check it out. stackoverflow.com/questions/697270/… Commented Jan 27, 2014 at 3:48
  • Which line specifically is causing the exception? The exception provides you with that information. What does the debugger tell you when you step through the code? What is the declaration of arrayName? Commented Jan 27, 2014 at 3:57
  • @Tim oh wait :) Is your question about the error that pops up or 'easier way to search previous entries from an array to newly input entries'? or both? Commented Jan 27, 2014 at 3:58
  • @chris_techno25 well it's both because this method doesn't work, and I would like to know if there's a good method to use for this function Commented Jan 27, 2014 at 4:00
  • @Tim Then this thread should probably answer both of your questions :) It uses the built-in method of searching an array, so you shouldn't have any error if used correctly and plus the code is short, which answers your 'fast method' issue :) stackoverflow.com/questions/697270/… Commented Jan 27, 2014 at 4:06

2 Answers 2

1

Since you don't want to use built-in search functions...Try this...

Declare this under Global Scope...So put it just under the form class...

Dim counter As Integer = 0
Dim arrayname(10) As String

Add a button control... and add this code...

 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    If counter < 10 Then
        Dim input As String = InputBox("Please input name.")
        If input = "" Then
            MsgBox("Nothing entered!")
            Exit Sub
        End If
        For x = 0 To 10
            If UCase(input) = UCase(arrayname(x)) Then
                MsgBox("Duplicate name!")
                Exit Sub
            End If
        Next x
        arrayname(counter) = input
        counter += 1
    Else
        MsgBox("Array full!")
    End If
End Sub

That's it. Edit to suit your exact needs. Tell me if it works :)

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

6 Comments

It has an issue! I can enter the same string many times and it doesn't catch them as a match for some reason. I stepped through the code and it executes the compare statement but goes to the Else every time to add to the array
It's still doing it :/
I added some variables to see what the code is doing while it's stepping through and it appears that the array isn't storing the variable when it reaches the end of the code, any ideas why?
@chris_techno25, you should include the upper limit of 10 for the string array in your For loop. VB is different from C# in that when you Dimension an array, the array is zero based and goes inclusively to the number of the subscript. So in this case, there are 11 elements in the string array.
@AdamZuckerman Thank you Sir. Yeah I was thinking it was the same array rules as C# :)
|
0

Your code snippet hasn't defined x.

You can search your array efficiently with a little bit of LINQ:

Dim listFound As IEnumarable(String) = From item In arrayName _
                                       Where item.ToString.ToUpper _
                                       = txtInput.Text.ToUpper _
                                       Select item
If listFound.Count > 0 Then
    lblName.Text = "Enter a unique name"
End If

Or you can manually search the array, but I think this is more efficient that the method you are using:

Dim Match As Boolean = False

For i As Integer = 0 To arrayName.Count - 1
    If Not IsNothing(arrayName(i)) Then
        If arrayName(i).ToString.ToUpper = txtInput.Text.ToUpper Then
            Match = True
            Exit For
        End If
    End If
Next
If Match Then
    lblName.Text = "Enter a unique name"
End If

Two last items of note: arrayName(i).ToString.ToUpper is equivalent to UCase(arrayName(i)). You may want to perform some speed tests to see which is actually faster.

Also, Dim myArray As String(3) is not the same thing in VB as Dim myArray() = New String(3) {}. The first example results in a 1 dimensional array. The last example results in a 2 dimensional array.

5 Comments

x is just the fixed length of my array
No, it isn't defined at all. You explicitly show i's definition and instantiation. If x is defined, why not include it in the code sample?
Because there's a lot more code that isn't relevant to the question. It's just an array with a fixed length, but isn't initialized with any data
I tried the bottom code and for some reason when I step through the debugger and it reaches the if not statement, even if i enter the same string twice, it doesn't enter the if block, it just skips over it and then goes to the next
Remove the curly braces from your declaration of arrayName and your issue should resolve itself.

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.