1

I have this structure:

Module Global_Variables
    Public myrecords(10) As myDatabase
    Public counter As Integer = 0

    Public Structure myDatabase
        'creates database Structure For input data
        <VBFixedString(30)> _
        Dim Driver As String           '30 bytes
        Dim Car As Integer             '4 bytes
        <VBFixedString(15)> _
        Dim Team As String             '15 bytes
        Dim Grid As Integer            '4 bytes
        Dim FastestLap As Double       '8 bytes
        Dim RaceTime As Double         '4 bytes
        Dim Points As Double           '4 bytes

    End Structure
End Module

The program receives data from the user and then displays the data in a text box called txtOutput in another form:

myrecords(counter).Driver = driver_box.Text
myrecords(counter).Car = car_box.Text
myrecords(counter).Team = team_box.Text
myrecords(counter).Grid = grid_box.Text
myrecords(counter).FastestLap = fl_box.Text
myrecords(counter).RaceTime = rt_box.Text
myrecords(counter).Points = points_box.Text

Form_Display.txtDisplay.AppendText(myrecords(counter).Driver & "          " & 
    myrecords(counter).Car & "          " & myrecords(counter).Team & "          " & 
    myrecords(counter).Grid & "             " & myrecords(counter).FastestLap & "             " & 
    myrecords(counter).RaceTime & "          " & myrecords(counter).Points & vbCrLf)

counter = counter + 1
MsgBox("Submit success!")
Call input_box_clear()

The user can then click a button to sort the records in ascending order by fastest lap. How do I do this?

I have tried algorithms such as bubble sort and selection sort but neither worked.

Thank you

6
  • RULE #1: Do not declare Structure types larger than 16 bytes. Anything >16 - declare a class. Commented Nov 1, 2016 at 21:43
  • Use LINQ and you will never need to write your own algorithm Commented Nov 1, 2016 at 21:45
  • Also take a look at String.Format() as a better option for building your display string. Commented Nov 1, 2016 at 22:26
  • @T.S. Normally I agree, but the use of VBFixedString indicates he may be relying on an API that requires a structure. Commented Nov 1, 2016 at 22:34
  • @JoelCoehoorn I doubt it. He seem trying to learn to align records. May be better use padding Commented Nov 1, 2016 at 22:41

1 Answer 1

1

Declaring type (class, not structure)

Public class RaceData
    Public Property Driver As String
    Public Property Car As Integer
    Public Property Team As String
    Public Property Grid As Integer
    Public Property FastestLap As Double
    Public Property RaceTime As Double
    Public Property Points As Double
End Class

In-memory database (look what is in System.Collections)

Private _raceDb As New List(Of RaceData)()

Add user input

Dim newItem As New RaceData()
newItem.Driver = driver_box.Text
newItem.Car = Integer.Parse(car_box.Text)
newItem.Team = team_box.Text
newItem.Grid = Integer.Parse(grid_box.Text)
newItem.FastestLap = Double.Parse(fl_box.Text)
newItem.RaceTime = Double.Parse(rt_box.Text)
newItem.Points = Double.Parse(points_box.Text)
_raceDb.Add(newItem)

Sorting for the grid (Read about LINQ)

// sort by fastest race time
Dim sortedDb As List(Of RaceData) = _raceDb.OrderBy(Function(x) x.RaceTime).ToList()

Pick one fastest race

Dim fastest As RaceData = _raceDb.OrderBy(Function(x) x.RaceTime).FirstOrDefault()
If fastest IsNot Nothing Then ...

Build a string for each item to add to multi-line textbox

Dim lines() As String = _raceDb.Select(Function(x) x.Driver & " --- " & x.Team).ToArray()
' Using some tips from the comments
Dim lines() As String = _raceDb.
    Select(Function(x) string.Format("{0,-30} --- {1,15}", x.Driver, x.Team)).ToArray()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.