calling a part of a structure into a button click - vb.net

I'm relatively new to vb. I have made a structure and now I want to do a bubble sort on the values. I'm unsure on how to call all of the data in the single part of the structure which is also a list.
(module)
module module 1
structure studenttype
dim id as string
dim name as string
end structure
public studentdetails as new list(of studenttype)
(main code)
Private Function bubbleSortbyID(ByVal namelist() As String) As String()
Dim n As Integer = namelist.Length()
Dim swapped As Boolean
Do
swapped = False
For i As Integer = 1 To n - 2
If namelist(i) > namelist(i + 1) Then
Dim temp As String = namelist(i + 1)
namelist(i + 1) = namelist(i)
namelist(i) = temp
swapped = True
End If
Next
Loop Until swapped = False 'no swap made so order Is correct
Return namelist
End Function
Private Sub BtnSort_Click(sender As Object, e As EventArgs) Handles BtnSort.Click
Dim id As String ' it is here I do not how how to call the whole variable
bubbleSortbyID(id)' id remains empty
ClearAndAdd()
End Sub'''

Related

VB.net Placing a .txt file into a Class List and assigning a property to certain indexes of the .txt file

I have a problem, I've created a class list for my sort program, so I can sort the items based on names. I was able to do it with assigned data but I don't know how to add data to the class from a txt file, nor assigning properties to those items once the file is loaded.
My current code:
Public Class PatientSorter
Public Class Patients
Property Name As String
Property Age As Integer
Property Weight As Double
Property Height As Integer
Public Overrides Function ToString() As String
Return String.Format("{0}: {1} years old, {2} mm, {3} kg", Name, Age, Height, Weight)
End Function
End Class
Public Sub PatientSorter_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim i As Integer
Do While i <= DataEntry.lstPatientArray.Items.Count - 1
lstCurrentData.Items.Add(DataEntry.lstPatientArray.Items(i))
i = i + 1
Loop
End Sub
Private Sub btnSearchForm_Click(sender As Object, e As EventArgs) Handles btnSearchForm.Click
Me.Hide()
Search.Show()
End Sub
Public Sub BubbleSort(ByRef patients As List(Of Patients))
For i = 0 To patients.Count - 2
Dim doneSwap = False
For j = i + 1 To patients.Count - 1
If patients(i).Name > patients(j).Name Then
Dim tmp = patients(j)
patients(j) = patients(i)
patients(i) = tmp
doneSwap = True
End If
Next
If Not doneSwap Then Return
Next
End Sub
Public Sub btnNameSort_Click(sender As Object, e As EventArgs) Handles btnNameSort.Click
Dim fileReader As String
fileReader = My.Computer.FileSystem.ReadAllText("C:\Data.txt")
Dim patients As New List(Of Patients)
BubbleSort(patients)
End Sub
End Class
Some of the Data in the txt file, first line is name, second is age, third is height and fourth is weight:
Monty Reyes
28
1700
70.7
Kier Burke
45
1800
93.5
My goal is to sort the data from the txt file using my bubblesort based on names. Not really able to do that without the data. Hopefully someone out there can help me or give me a clue on something I'm missing.
Using ReadAllLines instead of ReadAllText will give you an array of all line items in the file. You can then loop through that array an extract the data line by line to create and populate your Patient objects, which you'd then insert into your list.
Dim patients As New List(Of Patient)
Dim data = System.IO.File.ReadAllLines("C:\Data.txt") 'read lines into an array
'loop through the array, incrementing the index by 4 each iteration
For index = 0 To data.Length - 1 Step 4
Dim patient = New Patient() 'create a Patient
'Populate the patient data by accessing the current 4 array indexes
patient.Name = data(index)
patient.Age = data(index + 1)
patient.Weight = data(index + 2)
patient.Height = data(index + 3)
patients.Add(patient) 'add the Patient to the list of Patients
Next

Sorted List in Array Visual Basic [duplicate]

This question already has answers here:
What is a NullReferenceException, and how do I fix it?
(27 answers)
Closed 7 years ago.
I have a program that creates a list of 25 random numbers from 0 to 1,000. I have to buttons the first button will load a list box with the random numbers and the second button will sort the list of numbers from the smallest to largest which is where I implemented bubble sort code. Now the other list box that is supposed to hold the sorted numbers doesn't work properly it only shows one number instead of all of them.
Here is my code:
Option Strict On
Public Class Form1
Dim rn As Random = New Random
Dim Clicked As Long = 0
Dim numbers, sort As Long
Private Sub GenerateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBtn.Click
Clicked += 1
For x = 0 To 25
numbers = rn.Next(0, 1000)
RandomBox.Items.Add(numbers)
If Clicked >= 2 Then
RandomBox.Items.Clear()
Clicked = 1
End If
Next
End Sub
Private Sub SortBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortBtn.Click
Dim Sorted() As Long = {numbers}
Dim Swapped As Boolean
Dim endOfArray As Integer = Sorted.Length - 1
Dim Tmp As Byte
While (Swapped)
Swapped = False
For I = 0 To endOfArray - 1
If Sorted(I) > Sorted(I + 1) Then
Tmp = CByte(Sorted(I))
Sorted(I) = Sorted(I + 1)
Sorted(I + 1) = Tmp
Swapped = True
End If
endOfArray = endOfArray - 1
Next
End While
SortBox.Items.Clear()
For I = 0 To Sorted.Count - 1
SortBox.Items.Add(Sorted(I))
Next
End Sub
End Class
Change your:
Dim Sorted() As Long = {numbers}
to
Sorted(x) = numbers
edit: Since you changed your code. You need to put back in the line that loads the Sorted Array.
For x = 0 To 25
numbers = rn.Next(0, 1000)
RandomBox.Items.Add(numbers)
Sorted(x) = numbers
If Clicked >= 2 Then
RandomBox.Items.Clear()
Clicked = 1
End If
Next
and remove the:
Dim Sorted() As Long = {numbers}
from the second part and put this declaration back in the beginning like you had:
Dim Sorted(26) as Long
The way you have will only show the latest random number. It is not any array but a single entity. Therefore only the latest will be add into the array. You need to load each number into the array as you create each one. Thus the (x) which loads it into position x.
You didn't use any arrays at all in your project...you're using the ListBox as your storage medium and that's a really bad practice.
I recommend you set it up like this instead:
Public Class Form1
Private rn As New Random
Private numbers(24) As Integer ' 0 to 24 = 25 length
Private Sub GenerateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GenerateBtn.Click
For x As Integer = 0 To numbers.Length - 1
numbers(x) = rn.Next(0, 1000)
Next
' reset the listbox datasource to view the random numbers
RandomBox.DataSource = Nothing
RandomBox.DataSource = numbers
' empty out the sorted listbox
SortBox.DataSource = Nothing
End Sub
Private Sub SortBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SortBtn.Click
' make a COPY of the original array that we will sort:
Dim sorted(numbers.Length - 1) As Integer
Array.Copy(numbers, sorted, numbers.Length)
Dim Swapped As Boolean = True
Dim endOfArray As Integer = Sorted.Length - 1
Dim Tmp As Integer
While (Swapped)
Swapped = False
For I As Integer = 0 To endOfArray - 1
If sorted(I) > sorted(I + 1) Then
Tmp = sorted(I)
sorted(I) = sorted(I + 1)
sorted(I + 1) = Tmp
Swapped = True
End If
Next
endOfArray = endOfArray - 1
End While
' reset the listbox datasource to view the sorted numbers
SortBox.DataSource = Nothing
SortBox.DataSource = sorted
End Sub
End Class
Also, note that you were decrementing endOfArray inside your for loop. You should only decrement it after each pass; so outside the for loop, but inside the while loop.
Additionally, you were using a Tmp variable of type Byte, but generating numbers between 0 and 999 (inclusive). The Byte type can only hold values between 0 and 255 (inclusive).
Your Bubble Sort implementation was very close to correct!

Storing Objects into a Class & Retrieving upon Request

I seriously need help with my project.
I am trying to store specific jobs into a Class, which then displays in a List Box.
When selecting the List Box, I want the rest of the information to be displayed into a Text Box.
I can add Jobs into the List Box, and the Report button sorts the Job by Earliest to Latest.
I just CANNOT seem to code the Display Button to retrieve the rest of the information.
http://i.stack.imgur.com/0eV5j.png
What am I doing wrong?
My Code:
Public Class Form1
Dim jobList As List(Of UserInformation) = New List(Of UserInformation)
Dim j As UserInformation = New UserInformation()
Private Sub btnReport_Click(sender As Object, e As EventArgs) Handles btnReport.Click
Dim p As UserInformation = New UserInformation()
Dim qty As Integer = jobList.Count - 1
Dim name(qty) As String
Dim deadline(qty) As Date
Dim i As Integer = 0
'fill the array
For i = 0 To qty
p = jobList(i)
name(i) = p.Name
deadline(i) = p.Deadline
Next
'sort the array
Dim done As Boolean = False
While done = False
done = True
For i = 0 To qty - 1
Dim tempName As String
Dim tempDate As Date
If deadline(i) > deadline(i + 1) Then
tempName = name(i)
tempDate = deadline(i)
name(i) = name(i + 1)
deadline(i) = deadline(i + 1)
name(i + 1) = tempName
deadline(i + 1) = tempDate
done = False
End If
Next
End While
lsbReport.Items.Clear()
lblListbox.Text = "List in date order"
For i = 0 To name.Length - 1
Dim str As String
str = name(i) + ", "
str += deadline(i).ToString + "."
lsbReport.Items.Add(str)
Next
End Sub
Private Sub updateListBox()
lsbReport.Items.Clear()
lblListbox.Text = "All people in the List"
For Each person As UserInformation In jobList
Dim str As String
str = person.Name + ", "
str += person.Deadline.ToString + "."
lsbReport.Items.Add(str)
Next
End Sub
Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
Dim p As UserInformation = New UserInformation()
p.Name = firstNameText.Text
p.Deadline = lastNameText.Value
jobList.Add(p)
updateListBox()
End Sub
Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim job_info As UserInformation = CType(lsbReport.SelectedItem(), UserInformation)
txtReport.Text = "Job Title: " & job_info.Name() & Environment.NewLine
txtReport.Text &="Job DeadLine: " & job_info.Deadline & Environment.NewLine
txtReport.Text &="Job Description" & job_info.Description
End Sub
End Class
Public Class UserInformation
Public job_deadline As Date
Public job_name As String
Public job_description As String
Public Property Name() As String
Get
Return job_name
End Get
Set(ByVal value As String)
job_name = value
End Set
End Property
Public Property Deadline() As String
Get
Return job_deadline
End Get
Set(ByVal value As String)
job_deadline = value
End Set
End Property
Public Property Description() As String
Get
Return job_description
End Get
Set(ByVal value As String)
job_description = value
End Set
End Property
End Class
The bottom line is that you stored string values to the LBs:
str = person.Name & ", " & person.Deadline.ToString
lsbReport.Items.Add(str)
So, that is what you get back making it difficult to connect the string created with the object it represents.
Listboxes and comboboxes can store objects as well as strings. Simple Demo:
Public Class Employee
Public Property ID As Integer ' a db ID maybe
Public Property Name As String
Public Property Department As String
Public Property HireDate As Date
Public Overrides Function ToString As String
Return Name ' text to show in controls
' in a more realistic class it might be:
' Return String.Format("{0}, {1} ({2})", LastName,
' FirstName, BadgeNumber)
' e.g. "Whitman, Ziggy (123450)"
End Function
End Class
Friend EmpList As New List(of Employee) ' if needed
storing an object to a listbox is simple:
Dim newEmp As Employee
newEmp.Name = "Ziggy"
' set props as needed
myLB.Items.Add(newEmp) ' add to a ListBox directly.
Once you have a class for these things, you have many options. You can store them to a List(Of T) which can be used with a List or ComboBox:
Private EmpList As New List(Of Employee)
...
EmpList.Add(newEmp) ' adding to a list is same as a ListBox
' add from List to a control:
myLB.Items.AddRange(Emps.ToArray)
myLB.SelectedItem will now be an Employee object, which should make displaying details elsewhere simple.
To make it even more efficent, you can use the list as a DataSource so you dont have to add references to the listbox:
myLB.DataSource = EmpList
myLB.DisplayMember = "Name" ' the property to display
myLB.ValueMember = "Id" ' what to use for SelectedValue

Basic Name Sorting Program using VB.net

My teacher has instructed our class to create a basic word sorting program the 'old fashioned way' in visual basic. So comparing two array values, a and b, then if one is considered higher in the order than the other, swap them if not do nothing, continue until there are no more swaps. Here is the code I have so far:
Imports System.IO
Imports System
Public Class Form1
Public arrText As New ArrayList()
Private Sub btnImprt_Click(sender As Object, e As EventArgs) Handles btnImprt.Click
'Dim OpenAnswerFile As New OpenFileDialog
Dim objReader As New StreamReader("c:\Users\Adam\Desktop\unSortList.txt")
Dim sLine As String = ""
Dim arrText As New ArrayList()
Do
sLine = objReader.ReadLine()
If Not sLine Is Nothing Then
arrText.Add(sLine)
End If
Loop Until sLine Is Nothing
objReader.Close()
Dim i As Integer = 0
txtImport.Text = arrText(i)
End Sub
Private Sub btnSort_Click(sender As Object, e As EventArgs) Handles btnSort.Click
Dim i As Integer = 0
Dim a As Integer = i + 1
txtImport.Text = i
txtImport.Text = a
Dim Temp As String
Dim Change As Boolean = True
While Change = True
Change = False
For Each i In arrText(i) - 1
If String.Compare(arrText(i), arrText(i + 1)) = 1 Then
Change = True
Temp = arrText(i)
arrText(i) = arrText(i + 1)
arrText(i + 1) = Temp
End If
Next
i = 0
End While
txtSort.Text = arrText(39)
End Sub
My problem is that I am getting an Index error and I'm not sure where the error is located as the logic seems fine.
And yes I am aware of the sorting function built into Visual Basic. but as the teacher said. No cheating.
Your code has several flaws, which I'm ignoring and just concentrating on the sorting part, as your query is related to that. Replace your sort loop with the following and check again. The basic problem was that your loop should only iterate up to List.Count - 2 and not List.Count - 1 because you're comparing List(i) and List(i + 1) inside the loop:
Dim Temp As String
Dim Change As Boolean = True
While Change
Change = False
For i = 0 To arrText.Count() - 2
If String.Compare(arrText(i), arrText(i + 1)) = 1 Then
Change = True
Temp = arrText(i)
arrText(i) = arrText(i + 1)
arrText(i + 1) = Temp
End If
Next
End While

Bubble Sort logical error VB.NET

This program suppose to sort records(in arySort) in ascending order by last name(index 1 in aryTemp and aryTemp2) and place the result in the list box over the old, preloaded, unsorted records.
It sorts them strangely, I have to click multiple times the Ascending button to get the actual sort result that I suppose to get from clicking the button once.
Why doesn't it sort items with a single mouse click?
The source:
Public Class Form1
Dim FILE_NAME As String = "Students.txt"
Dim numberOfRecords As Integer 'total number of records
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If System.IO.File.Exists(FILE_NAME) = True Then
Dim objReader As New System.IO.StreamReader(FILE_NAME)
Do While objReader.Peek() <> -1
lstBox.Items.Add(objReader.ReadLine)
numberOfRecords += 1
Loop
objReader.Close()
End If
End Sub
Private Sub btnAscending_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAscending.Click
'load all students into array
Dim arySort(numberOfRecords - 1) As String
Dim aryTemp() As String 'holds first record's last name
Dim aryTemp2() As String 'holds second record's last name
For i = 0 To numberOfRecords - 1
arySort(i) = lstBox.Items(i)
Next
Dim temp As String 'holds temporary record
Dim k As Integer
For i = 0 To arySort.Length - 2
aryTemp = Split(arySort(i), " ")
For k = i + 1 To arySort.Length - 1
aryTemp2 = Split(arySort(k), " ")
If aryTemp(1) < aryTemp2(1) Then
temp = arySort(k)
arySort(k) = arySort(i)
arySort(i) = temp
End If
Next
Next
lstBox.Items.Clear()
numberOfRecords = 0
For i = 0 To arySort.Length - 1
lstBox.Items.Add(arySort(i))
numberOfRecords += 1
Next
End Sub
End Class
If you just need to sort your list (as you say in the comment), don't implement your own sort mechanism but use the one of .NET:
' Define how we want to compare items '
Function compareByLastName(ByVal item1 As String, ByVal item2 As String) As Integer
Return String.Compare(item1.Split(" ")(1), item2.Split(" ")(1))
End Function
Private Sub btnAscending_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAscending.Click
' load all students into array '
Dim arySort(numberOfRecords - 1) As String
For i = 0 To numberOfRecords - 1
arySort(i) = lstBox.Items(i)
Next
' Use built-in .NET magic '
Array.Sort(arySort, AddressOf compareByLastName)
' Write the values back into your list box '
lstBox.Items.Clear()
numberOfRecords = 0
For i = 0 To arySort.Length - 1
lstBox.Items.Add(arySort(i))
numberOfRecords += 1
Next
End Sub
This uses the built-in quicksort algorithm of the .NET class library. Here's the documentation of the method we are using: Array.Sort(T(), Comparison(Of T)).
compare with my working bubble sort:
Public Sub BubbleSort(ByVal arr() As Integer)
Dim flag As Boolean = False
For i As Integer = 0 To arr.Length - 1
For j As Integer = 0 To arr.Length - 2 - i
If arr(j + 1) < arr(j) Then
flag = True
Dim temp As Integer = arr(j)
arr(j) = arr(j + 1)
arr(j + 1) = temp
End If
Next
If flag = False Then Return ' no swaps =>already sorted
Next
End Sub
I see a two major issues with your algorithm:
It's not bubble sort. Bubble sort swaps adjacent elements, i.e., it swaps i with i+1. You, on the other hand, swap some element i with the first j > i where name(i) < name(j). Maybe you should show us, in pseudo code, which algorithm you are actually trying to implement?
aryTemp contains element i and aryTemp2 contains some element j > i. Why do you swap the elements if aryTemp(1) < aryTemp2(1)? Isn't that already the correct order if you want your elements to be sorted ascending?