array structure sort?

SI03

Member
Joined
Mar 30, 2006
Messages
12
Programming Experience
Beginner
I am fairly new to vb.net and was wanting to make a bubble sort from this information. From searching the net I found some but still do not understand how to do it with what I have.
Here is all of the code that I have and guidance on where to start to do a bubble sort ? I have a menu with each of the structure names in it and would like to be able to sort the array when it is displayed by each of those elements.Am I missing something with the structure to be able to do this?
VB.NET:
    Structure MovieInfo
        <VBFixedString(20)> Public Title As String
        <VBFixedString(4)> Public Year As Integer
        <VBFixedString(4)> Public Price As Integer
        Public sngPrice As Single
    End Structure

    Public udtMovie As MovieInfo
    Public objMovieArray(0) As Object

    Private Sub btnAddMovie_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddMovie.Click
        Try
            udtMovie.Title = txtTitle.Text
            udtMovie.Year = txtYear.Text
            udtMovie.Price = txtPrice.Text
            udtMovie.sngPrice = Val(txtPrice.Text)
            Dim intSize As Integer
            intSize = UBound(objMovieArray)
            ReDim Preserve objMovieArray(intSize + 1)
            objMovieArray(intSize) = udtMovie.Title & vbTab & udtMovie.Year & vbTab & udtMovie.sngPrice
            MsgBox("The Following Movie was added to array:" & vbCrLf & objMovieArray(intSize), MsgBoxStyle.Information, "Record added")
            'btnViewArray.Enabled = True
            txtTitle.Clear()
            txtYear.Clear()
            txtPrice.Clear()
        Catch ex As Exception
            MsgBox("Error occured in application", MsgBoxStyle.Critical)
        End Try
    End Sub

    Private Sub btnViewArray_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnViewArray.Click
        Try
            Dim intCounter As Integer
            Dim intSize As Integer
            intSize = UBound(objMovieArray) - 1
            lstArray.Items.Clear()

            For intCounter = 0 To intSize
                lstArray.Items.Add(objMovieArray(intCounter))
            Next intCounter
        Catch ex As Exception
            MsgBox("Error occuried while trying to view the array", MsgBoxStyle.Critical)
        End Try
    End Sub
 
Last edited by a moderator:
Is there a particular reason you're using the VBFixedString attribute, and on Integer variables no less?

When you put objects in a ListBox like that it is the return value of its ToString method that gets displayed. Since you haven't overridden the ToString method it will simply return its type. The alternative is to assign the array to the DataSource of the ListBox and the name of the property you want displayed to the DisplayMember.

As for sorting, make sure you have set the Sorted property of the ListBox to False. You can sort the array containing the objects by calling Array.Sort and passing an IComparer. It is your IComparer that will determine how the objects are sorted, so you can define it in such a way that you can tell it which property to sort on.
 
I was just always taught to used the fixedstring when making a structure. I added a function to return a string instead of type for it. Is that what I should have done to begin with?
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] ToString() [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Return[/COLOR][/SIZE][SIZE=2] Title & vbTab & Year & vbTab & sngPrice
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][/COLOR][/SIZE] 
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

As for the sorting, I was told that is how you can do it, but they still wanted me to learn how to do a bubble sort. So now I am stuck on trying to figure out how to do that.
 
I suggest that you read what the VBFixedString attribute is for and only use it when it's needed.

That ToString method will do the job, assuming that that's what you want returned.

Implementations of the bubble sort algorithm, if memory serves, simply require two nested loops. The outer loop works from the second element to the last. The inner loop works from the position of the outer loop back to the second element. Inside the inner loop you compare the current element with the one before it. If they are in the wrong order then you switch them and continue the inner loop. If they are in the correct order then you drop out of the inner loop and continue with the outer loop. That way each element, from the second to the last, gets bubbled up the the list until it finds its correct position.
 
Thanks for helping me to understand a bit more on what I am after. Trying the sort I am getting stuck on actually how to do it exactly. here is what I have so far by using your explanation. I get an exception error:
Specified cast is not valid.

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] SortYear([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] objMovieArray [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] L1 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] L2 [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Integer
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] L1 = UBound(objMovieArray) [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] LBound(objMovieArray) [/SIZE][SIZE=2][COLOR=#0000ff]Step[/COLOR][/SIZE][SIZE=2] -1
[/SIZE][SIZE=2][COLOR=#0000ff]For[/COLOR][/SIZE][SIZE=2] L2 = LBound(objMovieArray) + 1 [/SIZE][SIZE=2][COLOR=#0000ff]To[/COLOR][/SIZE][SIZE=2] L1
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] objMovieArray(L2 - 1) > objMovieArray(L2) [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]Swap(objMovieArray(L2 - 1), objMovieArray(L2))
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] L2
[/SIZE][SIZE=2][COLOR=#0000ff]Next[/COLOR][/SIZE][SIZE=2] L1
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]
[/COLOR][/SIZE]

 
When trying to use that bit of code you provided in my Sub to call it in the form. I get an error message: "An unhandled exception of type 'System.MissingMemberException' occurred in microsoft.visualbasic.dll
Additional information: Public member 'GetUpperBound' on type 'Integer' not found."

I am attempting to call the sub like this Call SortYear(udtMovie.Year). When I step through the process it sees the last integer and then throws that error.
 
SI03, is Year an array?
 
In the code I provided 'arr' is an array. Arrays have a GetUpperBound method. Integers do not. The whole point of this is to sort an array, right? In my code 'arr' is the array being sorted and inside the inner loop you compare the elements at indices j and j - 1 and switch their positions if required.
 
I guess I am just a bit lost. Yes I want to sort the array. But I have to sort the array by each element. It is to be done by clicking on a menu item for instance Year is one of the menu items. When you click on Year it will sort the array by Year.
 
Whatever your array is you substitute it for 'arr' in my code. Then inside the inner loop you compare the elements of that array at indices j and j - 1 and swap them if required. I'm not going to show you that part because it's your homework.
 
Am I going in the right direction with this? This seems to switch the first one with the second one but not the second one with the first one.
VB.NET:
[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] objMovieArray.IndexOf(objMovieArray, udtMovie.sYear) > objMovieArray.IndexOf(objMovieArray, udtMovie.sYear) - 1 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2]temp = objMovieArray(j).ToString
objMovieArray(j) = objMovieArray(j - 1)
objMovieArray(j - 1) = objMovieArray(j)
[/SIZE][SIZE=2][COLOR=#008000]
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]
 
Several issues there.

1. IndexOf is a Shared member. You call Shared members on the type, not an instance. That means that it should be "Array.IndexOf", not "objMovieArray.IndexOf".

2. You're supposed to be comparing the elements at index j and index j-1. Look at your If statement. There is no mention of 'j' there whatsoever.

3. Take another look at your If statement. If "Array.IndexOf(objMovieArray, udtMovie.sYear)" is X then you're comparing X to X-1. X is always going to be greater than X-1 so that If statement will always evaluate True.

What you need to do is compare the array element at index j with the array element at index j-1. I don't think I can word it any more clearly than that.
 
Back
Top