Question Compare String from txt file and textbox.text

thuyquai

New member
Joined
Aug 23, 2010
Messages
2
Programming Experience
Beginner
Can someone pls help me with this problem

first I create a class which have a string property
Property stdName() As String
then I open a text file and put into array of that class
Dim arrStudent() As Students

Dim allstdinfo As New IO.StreamReader(dlgOpenStudentsFile.FileName)
Do Until (allstdinfo.Peek() = -1)
Dim strAllstdinfo As String = allstdinfo.ReadLine()
Dim arrStudentLine() As String
arrStudentLine = strAllstdinfo.Split(New String() {"||"}, StringSplitOptions.RemoveEmptyEntries)
ReDim Preserve arrStudent(counter)
Dim singlestudent As New Students
singlestudent.stdName = arrStudentLine(0)
singlestudent.stdClass = arrStudentLine(1)
singlestudent.phoneNo = arrStudentLine(2)
singlestudent.averageScore = arrStudentLine(3)
arrStudent(counter) = singlestudent
counter += 1
Loop

Finally I run a for loop and compare the stdName property with textbox.text

Dim counter As Integer = arrStudent.Length

(debug here)
For i As Integer = 0 To counter - 1
If (String.Compare(arrStudent(i).stdName, txtStdName.Text) = 0) Then
lblPhoneNo.Text = arrStudent(i).phoneNo
End If
Next

and I add watch:

4920292612_c4e46a5f60_b.jpg


Why the compare return 1 :((
 
Last edited:
Look at your Watch.

arrStudent(i).stdName has an extra space at the end. Therefore, the strings are not equal.
 
Dear user:

I dont know waht version of VB are you using, but the compare return (1/-1) or 0.

The case sensitive affects to the result.

See this example: Dim str1 As String
Dim str2 As String
str1 = "vb.net"
str2 = "VB.NET"
Dim result As Integer
result = String.Compare(str1, str2)
MsgBox(result)
result = String.Compare(str1, str2, True)
MsgBox(result)

Hope was usefull
 
Back
Top