|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
|||
|
|||
|
Sorry... Yet Another Question... (See Edit)
Hey there, I'm new to this forum and VB programming, so sorry if this seems like a noobish question.
Basically for a school project I have to create a lottery program which generates six random numbers, then performs a bubble sort to sort these numbers into order and then display them in a picture box. I've been struggling with this for days, I have no idea but my code looks OK to me, and whenever I click the command button which is associated with this code I always get an error message saying "Wrong Number Of Arguments Or Invalid Property Assignment" and always highlights the first "Populate_List list()" part. Here's the code: Option Explicit Dim Counter As Integer Dim list(6) As Variant Private Sub CmdStart_Click() 'Generates six random numbers between one and forty eight and performs a bubble 'sort on them. Randomize Populate_List list() (this part is always highlighted when I try to click CmdStart) Bubble_Sort list() Output_Results End Sub Private Sub Populate_List() Dim Fill As Integer For Fill = 0 To 6 list(Fill) = Int(48 * Rnd) + 1 Next Fill End Sub Private Sub Bubble_Sort() Dim Outer As Integer Dim Inner As Integer For Outer = 6 To 0 Step -1 For Inner = 0 To Outer - 1 If list(Inner) > list(Inner + 1) Then Temp = list(Inner) list(Inner) = list(Inner + 1) list(Inner + 1) = Temp End If Next Inner Next Outer End Sub Private Sub Output_Results() Dim Count As Integer PicResult.Print " "; For Count = 0 To 15 PicNumbersOne.Print list(Count); Next Count End Sub Any help you could give me would be much appreciated. EDIT: This problem has been solved, but now I have another. OK, so after the random numbers have been generated, the user then enters six numbers between one and fifty, which are also displayed in a picture box. This is fine, and these numbers are stored in the array yournumbers. However, the problems start when the user clicks the CmdCheck Button, which is for the user to check their "winnings". The basis of this part was that the two arrays (one for the random numbers and one for the user entered numbers) are compared using a binary search and if a number the user entered matches one of the randomly generated numbers then a value of one will be added to a variable called "Winnings", and once all of the numbers have been compared then the final value of Winnings would be used in a Select...Case statement to determine how much the user would win. However, I seem to have hit a bit of a snag, as when I click the command button to start this a message box comes up saying "Next Without For". I have no idea why this is, as I have entered a For, which is bolded, and when I take the Next Count away it says "For Without Next." Again, here is the code: Private Sub CmdCheck_Click() Dim Count As Integer, Middle As Integer, Found As Boolean, FirstLocation As Integer, Passes As Integer Dim LastLocation As Integer, Pointer As Integer, Winnings As Integer, List(6) As Variant, SearchKey As Integer Winnings = 0 Passes = 0 For Count = 0 To 5 Found = False Passes = Passes + 1 FirstLocation = 0 LastLocation = 5 SearchKey = yournumbers(Count) Do Pointer = (FirstLocation + LastLocation) / 2 If List(Pointer) < SearchKey Then FirstLocation = Pointer + 1 Else LastLocation = Pointer - 1 End If If List(Pointer) = SearchKey Then Found = True Winnings = Winnings + 1 End If Next Count Loop Until Passes > 4 Select Case Winnings Case 0 PicWinnings.Print "Sorry, you have not won anything this time." Case 1 PicWinnings.Print "You have won £10!" Case 2 PicWinnings.Print "You have won £100!" Case 3 PicWinnings.Print "You have won £1000!" Case 4 PicWinnings.Print "You have won £10000!" Case 5 PicWinnings.Print "You have won £100000!" Case 6 PicWinnings.Print "You have won the jackpot of £1000000!" End Select End Sub I've bolded the For and Next statements. I also don't know if a binary search is the best way to go with this, if its not then could anyone suggest any alternatives? Sorry for what must seem like really stupid questions, I am very much a noob to this stuff. Again, thanks in advanced for any help you can give. I would ask my teacher but my school's on break and this has to be done for when we go back, plus I figure I've already gotten great help from you guys, so why not just ask again? Last edited by shotgunjoe : April 18th, 2008 at 02:01 PM. Reason: Question answered, but another question came up. |
|
#2
|
||||
|
||||
|
It's because your subs are not accepting parameters.
Populate_List list() list() seems to be on the wrong place for an argument. What is that anyway? |
|
#3
|
|||
|
|||
|
list() was meant to be an array to accept the random numbers, then I'd just display that "list" in the picture box.
Also, I am a complete noob to this, how would I make the subs accept paramaters? Thanks for your help! |
|
#4
|
||||
|
||||
|
Well, since your list variable is module-level and all the functions have access to it anyway, you could remove 'list()' from the end of the function calls. Instead of calling Populate_List list() you would call Populate_List.
I think that would make it work. However, if you want to change it so that you pass list to each of those, that's a different story. |
|
#5
|
||||
|
||||
|
^ Follow above's advice. Since list() is pulic just call it by its name.
|
|
#6
|
|||
|
|||
|
Thanks you so much guys, it now works fine!
|
|
#7
|
||||
|
||||
|
To actually pass an array
Code:
Private Sub Populate_List(ByRef varList() As Variant) This is cleaner than using a public array and for a large scale project would be advised.
__________________
medialint.com "Energy has the opportunity to change the climate if it's done right." - Sen. John Ensign, R-Nev. (quoted out of context) |
|
#8
|
|||
|
|||
|
I've updated my first post with yet another problem I was hoping you guys could help me out with, thanks!
|
|
#9
|
||||
|
||||
|
Quote:
Also, I think now is a good time to remind you to check out the 'Read this first' sticky threads and to use [code][/code] tags to preserve indentation and make your code easier to read. |
|
#10
|
||||
|
||||
|
Your new problem is because you have a for-loop and a do-until loop overlapping:
Code:
For Count..... Do '? Next Count Loop Until.... 'belongs to Do! You can't have the beginning of a control structure inside another control structure and not have the end inside the same scope (and vice versa). |
|
#11
|
||||
|
||||
|
Nice catch Lyon.
|
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > Visual Basic Programming > Wrong Number Of Arguments Or Invalid Property Assignment? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|