Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of ArrayList.Contains 1

Status
Not open for further replies.

mainit123

Technical User
Jan 24, 2005
25
0
0
I am trying to use the built-in VB tools rather than manually code my task of trying to tell if a string character I have trapped is or is not a member of an array.
This would seem to be a classic use of ArrayList.contains but the documentation is sparse on examples that I can go forward with. I feel I am close to a solution but need that final push.
Here is my code:

Dim badArray As Array = Array.CreateInstance(GetType(String), 4)

Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
'Load the Bad char array,
badArray.SetValue(";", 1)
badArray.SetValue("+", 2)
badArray.SetValue("?", 3)
'Now validate
txtbx1 = TextBox1.Text
txt1cnt = Len(txtbx1)
txt1cntm1 = txt1cnt - 1
For i = 0 To txt1cntm1
For j = 1 To 3
mysub = TextBox1.Text.Substring(i, 1)
If mysub = badArray.GetValue(j) Then
Badchar = "Yes"
End If
Next j
Next i
As you can see, I am creating an array of "special" characters, like ";" , "+", and "?" so that I can alert the user when they have inadvertently typed those characters into the TextBox1. I am simply looping through each character of the captured TextBox string and comparing them against each item in my badArray.
So, I would love to see how ArrayList.Contains could automate this or not make it so manual.
Thanks in advance.
 
Dim notAllowed() As Char = {"+", ";", "?"}

If Me.TextBox1.Text.IndexOfAny(notAllowed) <> -1 Then
MsgBox("error")
End If



:)
 
Also here is the use of .contains() with a function:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If Not ValidateTextSucceeded(Me.TextBox1.Text) Then
MsgBox("Error")
End If
End Sub



Private Function ValidateTextSucceeded(ByVal myString As String) As Boolean
Dim a As New ArrayList
a.Add("?")
a.Add("+")
a.Add(";")
a.TrimToSize()

For i As Integer = 0 To a.Count - 1
If Me.TextBox1.Text.Contains(a(i).ToString) Then Return False
Next

Return True
End Function



:)
 
Thanks so much for the great and timely response.
I will put this to use immediately and I appreciate your efforts in helping me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top