[英]visual basic array searching
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?
在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."
我正在从C#转换代码,Visual Basic不断给我一个错误“对象引用未设置为对象实例”。 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块以检查是否有匹配的文本,因为在这种情况下arrayName(i)或0当前为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.
如果我取出此if块并到达名称检查,则将导致错误,因为arrayName(i)中没有任何内容可转换为大写字符串。
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." 这是数组声明
Dim arrayName() = New String(2) {}
,当它到达If arrayName(i).ToString.ToUpper = txtInput.Text.ToUpper时,它说“对象变量或未设置块变量。 “ "NullReferenceException was unhandled by user code". “用户代码未处理NullReferenceException”。 The "x" in the code is the fixed length of the array, which is 2 in this case.
代码中的“ x”是数组的固定长度,在这种情况下为2。
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
. 您的代码段尚未定义
x
。
You can search your array efficiently with a little bit of LINQ: 您可以使用一些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))
. 需要注意的最后两项:
arrayName(i).ToString.ToUpper
等效于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) {}
. 另外,在VB中,将
Dim myArray As String(3)
与Dim myArray() = New String(3) {}
。 The first example results in a 1 dimensional array. 第一个示例生成一维数组。 The last example results in a 2 dimensional array.
最后一个示例生成一个二维数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.