Widunder Posted August 4, 2010 Posted August 4, 2010 Hello Everyone, Im new to VB .NET, and im working on an application where the user types the lap times in and then the program has to store this data into array everytime the button is clicked. Now, I can do that, but what I can't do is to then menually sort them out, so that the fastest will be at the top and display them as a list in a label. With no built-in functions. Can anyone please post an example of how this is possible. Thanks. Quote
Administrators PlausiblyDamp Posted August 4, 2010 Administrators Posted August 4, 2010 If you can't use any of the built in functions you are probably best searching the internet for the Quick Sort algorithm, you should find plenty of examples and it is a pretty easy to implement solution to the problem. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Widunder Posted August 16, 2010 Author Posted August 16, 2010 Ok, I found the code but now I have another problem. I don't know how to call the FastestSort method, anything I do it comes up with an error: Value of type 'System.Web.UI.WebControls.ListBox' cannot be converted to '1-dimensional array of Integer'. The code: Partial Public Class _Default Inherits System.Web.UI.Page Private min As Integer Private max As Integer Public Shared Sub FastestSort(ByVal lbList() As Integer, ByVal min As Integer, ByVal max As Integer) Dim random_number As New Random Dim avg As Integer Dim i As Integer Dim low As Integer Dim high As Integer 'if minimum is >= max, then there is nothing to sort If min >= max Then Exit Sub ' randomize i to select a random number as a starter and use it as a divider i = random_number.Next(min, max + 1) avg = lbList(i) ' Swap it to the front. lbList(i) = lbList(min) low = min high = max Do ' Look down from hi for a value < med_value. Do While lbList(high) >= avg high = high - 1 If high <= low Then Exit Do Loop If high <= low Then lbList(low) = avg Exit Do End If ' Swap the lo and hi values. lbList(low) = lbList(high) ' increase the lowest number to look for the next one, while it's lower than average ' keep increasing till we got it low = low + 1 Do While lbList(low) < avg low = low + 1 If low >= high Then Exit Do Loop If low >= high Then low = high lbList(high) = avg Exit Do End If ' Swap the low and high values. lbList(high) = lbList(low) Loop FastestSort(lbList, min, low - 1) FastestSort(lbList, low + 1, max) End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click Dim lapTime As Integer Dim lapNum As Integer Integer.TryParse(txtLapNum.Text, lapNum) Integer.TryParse(txtLapTime.Text, lapTime) 'each time we click, we add time to the list lbList.Items.Add(lapTime) End Sub Protected Sub btnFastest_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFastest.Click ' pass the parameters FastestSort(lbList, min, max) End Sub End Class I also tried FastestSort(lbList(min, max)) but its coming up with a different error: Class 'System.Web.UI.WebControls.ListBox' cannot be indexed because it has no default property. Please help.:confused: Quote
Widunder Posted August 16, 2010 Author Posted August 16, 2010 Ok, I solved that, but now it's saying "Object reference not set to an instance of an object." at the avg = lbList(i) line Quote
Administrators PlausiblyDamp Posted August 16, 2010 Administrators Posted August 16, 2010 If you put a break point on that line and run it, what are the contents of the variable i, lbList and lbList(i)? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Widunder Posted August 21, 2010 Author Posted August 21, 2010 I've changed the code so that the average is calculated from the numbers in the list and thats the whole thing, but it still comes up with an error: Partial Public Class _Default Private min As Decimal Private max As Decimal Private values() As Integer Private Const max_val As Decimal = 50 Public Sub FastestSort(ByVal List() As Integer, ByVal min As Integer, ByVal max As Integer) Dim random_number As New Random Dim avg As Decimal Dim low As Decimal Dim high As Decimal Dim totalNum As Long Dim i As Long 'if minimum is >= max, then there is nothing to sort If min >= max Then Exit Sub End If ' find average which is going to be the divider For i = 0 To lbList.Items.Count - 1 totalNum += lbList.Items(i) Next i 'avg = List(i) avg = totalNum / i 'List(i) = avg ' Swap it to the front. List(i) = List(min) low = min high = max Do ' Look down from high for a value < avg. Do While List(high) >= avg high = high - 1 If high <= low Then Exit Do Loop If high <= low Then List(low) = avg Exit Do End If ' Swap the low and high values. List(low) = List(high) ' increase the lowest number to look for the next one, while it's lower than average ' keep increasing till we got it low = low + 1 Do While List(low) < avg low = low + 1 If low >= high Then Exit Do Loop If low >= high Then low = high List(high) = avg Exit Do End If ' Swap the low and high values. List(high) = List(low) Loop FastestSort(List, min, low - 1) FastestSort(List, low + 1, max) End Sub Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load End Sub Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnAdd.Click Dim lapTime As Decimal Dim lapNum As Decimal Integer.TryParse(txtLapNum.Text, lapNum) Integer.TryParse(txtLapTime.Text, lapTime) 'each time we click, we add time to the list lbList.Items.Add(lapTime) End Sub Protected Sub btnFastest_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnFastest.Click Dim i As Decimal Dim txt As String ' pass the parameters FastestSort(values, 0, max_val) ' Verify and display the results. txt = txt & Format$(values(0)) For i = 1 To max_val If values(i) < values(i - 1) Then Stop txt = txt & Format$(values(i)) Next i lbList.Text = txt End Sub End Class Right, Im adding numbers: 9,8,7,6 and when I click the button it stops and shows that: avg is 7.5D List - nothing (i) - 4 = List - nothing (min) - 0 What I don't understand is how to make my array to know about the items in the listBox - may be thats where the problem is. They're working as two separate things? Plus should I use ViewState? Although I tried using it before and it didn't work either. And what I can't understand is that how a name of array is nothing but the value in it is 4?! Quote
Administrators PlausiblyDamp Posted August 21, 2010 Administrators Posted August 21, 2010 What I don't understand is how to make my array to know about the items in the listBox - may be thats where the problem is. They're working as two separate things? You really should treat the UI as a way of interacting with the user, i.e. displaying information or gathering information, but nothing more. When the user enters a value and hits the Add button it should be adding the value to the array or collection you are using behind the scenes (I would personally use a generic collection rather than an array) and then this list used to populate the listbox. As you are working in a web environment you need to be aware that web pages are inherently stateless - information is not retained between page refreshes automatically and you will need to deal with this yourself (it may be worth looking at the Session object for a starting point). The code FastestSort(lbList(min, max)) is attempting to treat the lbList as a two dimensional array and then pass it to the FastestSort routine - hence the error. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Widunder Posted August 22, 2010 Author Posted August 22, 2010 FastestSort(lbList(min, max)) I got rid of it in the second code I posted. How can I sort the items in the list then? Should I use ViewState to store the data? How do I add values to array then? And how do I let that array to be used in another method? I only started working with .NET 6 weeks ago Quote
Administrators PlausiblyDamp Posted August 22, 2010 Administrators Posted August 22, 2010 Is there a reason you are sorting the list manually? It would be far easier to use Array.Sort and let .Net do the hard work for you. http://msdn.microsoft.com/en-us/library/ms178581.aspx explains Session state and might be a better alternative to the view state. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Widunder Posted August 23, 2010 Author Posted August 23, 2010 Yes, thats the homework to make it work manually. But it doesn't tell me how I can manually sort the data in the Session State? Quote
Widunder Posted August 24, 2010 Author Posted August 24, 2010 The problem is also in our teacher. We're learning buggar all, and many of us get frastrated and angry with it, and we complain but it doesn't change the fact that we have to pass it. Which really sucks. Google haven't been much of a help either. And when I asked my teacher how to connect the array to the ListBox - to make the values to be stored in the array, calculated and then display the result - he told me to Google it! :( I've Googled it heaps of times, but there are only code snippets of how to do the calculation, but not how to make the whole thing work. And we're not getting any examples about that as well. If someone can please write an example up I'll be grateful. All Im now trying to do is somehow tell the ListBox to add the items from the array in the certain order - the way it has been calculated. Quote
Administrators PlausiblyDamp Posted August 24, 2010 Administrators Posted August 24, 2010 Rather than thinking of this as "sorting a session" or "sorting a listbox" you really want to get a simple routine working that can sort a list of integers, this doesn't even need to be done in the context of a web application. Once you get this working you can use your web application to populate an array that you would store in the Session between post backs, the list box would just display the contents of this array. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Administrators PlausiblyDamp Posted August 24, 2010 Administrators Posted August 24, 2010 To populate the listbox you could either set it's DataSource property to the array and then call .DataBind() on it or simply loop over the array and add each item to the listbox in turn. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.