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
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 :)
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.