General discussion

Locked

Visual Basic Question

By nulubez ·
Okay. This is so very basic but I am having an issue getting help on this matter. I am making a program that shows member's names and phone numbers and allows the user to update the information. When the program loads the names are listed. When the user highlights the names the name and phone number are shown in separate text boxes below the listbox. I can't seem to write the code that searches the name highlighted and places the correct phone number with it.

Here is what I have so far:

Structure List
Dim name As String
Dim phone As String
End Structure
Dim memberList(7) As List

Private Sub Unit06_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim temp As List
Dim sr As IO.StreamReader = IO.File.OpenText("MEMBERPHONES.TXT")

'place member information into array memberList

For i As Integer = 0 To 7
memberList(i).name = sr.ReadLine
memberList(i).phone = sr.ReadLine
Next

'bubble sort on member's name

For i As Integer = 1 To 7
For j As Integer = 1 To 8 - i
If memberList(j - 1).name > memberList(j).name Then
temp = memberList(j - 1)
memberList(j - 1) = memberList(j)
memberList(j) = temp
End If
Next
Next

'display member list in list box

lstMembers.Items.Clear()
For j As Integer = 0 To 7
lstMembers.Items.Add(memberList(j).name)
Next
sr.Close()
End Sub

Private Sub lstMembers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMembers.SelectedIndexChanged

Dim list() As String
Dim name As String
name = txtName.Text.Trim().ToUpper
Dim sr As IO.StreamReader = IO.File.OpenText("MEMBERPHONES.TXT")
Do While sr.Peek() <> -1
If name = list(0).ToUpper Then
'this is where I want mtxtphone to add the number???

End If
Loop
sr.Close()

txtName.Text = lstMembers.Text


End Sub

I know this is basic. Thank you for the help!

This conversation is currently closed to new comments.

7 total posts (Page 1 of 1)  
| Thread display: Collapse - | Expand +

All Comments

Collapse -

just do this

by jck In reply to Visual Basic Question

1) Read your data
2) put it in a grid with 1 visible column (name)

on the grid click, get the row and read the other column from that row with the hidden info (phone).

QED

Collapse -

Tsk Tsk

by Shellbot In reply to just do this

Unneccessary bloat using a data grid :)

I'm getting the idea he's looking to code this for the learning experience maybe?

Always better to learn it the correct way, then take shortcuts :)

Collapse -

Er

by jck In reply to Tsk Tsk

Bloat? Correct way?

If you are doing small sets (under 50,000 records), it's not bloat. That will easily run in memory and will give a performance gain vs going to the network/service for an individual record each time on the click event.

If I am providing a data lookup to my customer, I'd prefer to give them nanosecond response times than millisecond response times in their application.

As for it being not being "correct", what is the correct way? Hm? And who defined correct?

You have me curious now. I can program it in VB.NET or VB6, yet it's not correct to do so.

I think Steve Ballmer has gotten to you, Shell. Think outside the MS box.

P.S.- He never said what the exercise was in, i.e.- file access, etc etc. I guess I needed to see the assignment rather than just his code to know what he needs.

Collapse -

Ok..a few things

by Shellbot In reply to Visual Basic Question

To use what you've got there you need to loop through your MemberList and compare txtName with the Name property of each member list element.

Can I ask why you are opening your file a second time?
(Dim sr As IO.StreamReader = IO.File.OpenText("MEMBERPHONES.TXT"))
You've already got the value in the array.

For an alternative way to do this, replace the Sturcture with a System.Generics.Collections.SortedDictionary <T,T> Object

Look that up, you might find it a lot easier to work with, you can reference the names directly, without writing a search, and it will sort them for you.

Let us know how you get on ok.

Collapse -

Put memberlist(i) in the list box

by Tony Hopkinson In reply to Visual Basic Question

instead of it's member name.
There's an overload for add that takes a string and a name, or you could override memberlist's ToString() method.

As Shell said you could use a Dictionary instead of an array, or a sortedlist of MemberList.

PS MemberList would be better named Member, list being an aggregate of them.

Collapse -

OK everyone.... went somewhere different!

by nulubez In reply to Visual Basic Question

First thank you all for your input. This seems like such a basic code to write yet I am having multiple problems with it. I tried something a little different then my original posted work. I used a function to find the name and now I have no syntax errors but when you highlight the name in the listbox it always gives the last number in the txt file. There must be something wrong with my binary search but once again, I cannot figure out what. I am not looking for any of you to do my work for me, I just want to learn how to troubleshoot these issues so that I can become a better programmer. I can't seem to get personal responses back from my professor so I greatly appreciate any help you have to offer!

Structure List
Dim name As String
Dim phone As String
End Structure
Dim memberList(7) As List

Private Sub Unit06_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim temp As List
Dim sr As IO.StreamReader = IO.File.OpenText("MEMBERPHONES.TXT")

'place member information into array memberList
For i As Integer = 0 To 7
memberList(i).name = sr.ReadLine
memberList(i).phone = sr.ReadLine
Next

'bubble sort on member's name
For i As Integer = 1 To 7
For j As Integer = 1 To 8 - i
If memberList(j - 1).name > memberList(j).name Then
temp = memberList(j - 1)
memberList(j - 1) = memberList(j)
memberList(j) = temp
End If
Next
Next

'display member list in list box
lstMembers.Items.Clear()
For j As Integer = 0 To 7
lstMembers.Items.Add(memberList(j).name)
Next
sr.Close()
End Sub

Private Sub lstMembers_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMembers.SelectedIndexChanged
'display member name and phone number in associated text boxes.
Dim member As String
Dim result As String
member = lstMembers.Text
txtName.Text = lstMembers.Text
result = FindNum(member)
mtxtPhone.Text = result

End Sub

Function FindNum(ByRef member As String) As String
'Binary search for the member name
Dim first, middle, last As Integer
first = 0
last = memberList.GetUpperBound(0)
Do While (first <= last)
middle = CInt(Int((first + last) / 2))
Select Case memberList(middle).name.ToUpper
Case Name
Return middle
Case Is > Name
last = middle - 1
Case Is < Name
first = middle + 1
End Select
Loop
Return memberList(middle).phone
End Function


End Class

Collapse -

Binary Search

by victorvmpm In reply to OK everyone.... went some ...

Function FindNum(ByRef member As String, ByRef first as Integer, ByRef last as Integer) As String
'Okay I have no idea what your code does so I am writting you a Binary Search
'I get the half
Dim middle as Integer = CInt(Int((first + last)/2))
If memberList(middle).name = member then
return memberList(middle).phone
'LessThan is a function that tells if a String is less than the other
ElseIf (memberList(first).name = member) then
return memberList(first).phone
ElseIf (memberList(last).name = member) then
return memberList(last).phone
ElseIf (LessThan(memberList(middle).name, member) Then
return FindNum(member, first, middle)
Else
return FindNum(member, middle, last)
End If
End Function

' This how it would work with numbers, In the Case above is applied to Strings
' 0 1 2 3 4 5. Looking for 1
' first = 0
' last = 5
' middle = 2
' 1 is LessThan 2
' first = 0
' last = 2
' middle = 1
' returns 1

By the way be sure the list is properly ordered, to see that debug it after bubble sort and see the memberlist. After that, walk through the memberlist to be sure its ordered.

Back to Windows Forum
7 total posts (Page 1 of 1)  

Operating Systems Forums