Results 1 to 22 of 22

Thread: [RESOLVED] Need help with Function [2005] [2008]

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Resolved [RESOLVED] Need help with Function [2005] [2008]

    I am a major Noob here so please forgive any stupidity that I may cause. I just started learning VB 2 months ago... Anyways...

    I am Working on a code in which I am trying to define and list box and an array in the same function I do that... I think... and when I wish to use it, it says that the array I place in isn't an array, I am sure thats the case, but i don't know how to fix it. its a Loto game and I have to create the relationship between the "Winning Numbers" And the Users Numbers in the listboxes. There are two players... here is the code to the whole thing.

    Code:
       Public Class Form1
    
        Dim Roll As Integer = 6
        Dim nNums(Roll - 1) As Integer
        Const uNum As Integer = 49
        Const lNum As Integer = 1
        Dim Count As Integer
        Dim GameCount As Integer
    
    
        Private Sub btnEnter1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnt1.Click
            If (IsNumeric(txtPlay1.Text) = False) Then
                MsgBox("Has To Be a Number", MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
                txtPlay1.Text = " "
                txtPlay1.Focus()
            ElseIf (isValid(txtPlay1.Text) = False) Then
                MsgBox("Has To Be " & lNum & "-" & uNum, MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
                txtPlay1.Text = " "
                txtPlay1.Focus()
            ElseIf (isSame(lstPlay1, txtPlay1.Text) = True) Then
                MsgBox("Has To Be a different number", MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
                txtPlay1.Text = " "
                txtPlay1.Focus()
            Else
                lstPlay1.Items.Add(txtPlay1.Text)
                txtPlay1.Text = ""
                txtPlay1.Focus()
                If lstPlay1.Items.Count = Roll Then
                    txtPlay1.Text = ""
                    btnEnt1.Enabled = False
                    txtPlay1.Enabled = False
                End If
            End If
            If (btnEnt1.Enabled = False) And (btnEnt2.Enabled = False) Then
                btnRollWin.Enabled = True
            End If
        End Sub
        Private Sub btnEnt2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnt2.Click
    
            If (IsNumeric(txtPlay2.Text) = False) Then
                MsgBox("Has To Be a Number", MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
                txtPlay2.Text = " "
                txtPlay2.Focus()
            ElseIf (isValid(txtPlay2.Text) = False) Then
                MsgBox("Has To Be " & lNum & "-" & uNum, MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
                txtPlay2.Text = " "
                txtPlay2.Focus()
    
            ElseIf (isSame(lstPlay2, txtPlay2.Text) = True) Then
                MsgBox("Has To Be a different number", MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
                txtPlay2.Text = " "
                txtPlay2.Focus()
            Else
                lstPlay2.Items.Add(txtPlay2.Text)
                txtPlay2.Text = ""
                txtPlay2.Focus()
                If lstPlay2.Items.Count = Roll Then
                    txtPlay2.Text = ""
                    btnEnt2.Enabled = False
                    txtPlay2.Enabled = False
                End If
            End If
            If (btnEnt1.Enabled = False) And (btnEnt2.Enabled = False) Then
                btnRollWin.Enabled = True
            End If
        End Sub
       
    
     Public Function isValid(ByVal iNum As Integer) As Boolean
            If (iNum >= lNum And iNum <= uNum) Then
                isValid = True
            Else
                isValid = False
            End If
    
        End Function
    
        Public Function isSame(ByRef list As ListBox, ByVal iNum As Integer) As Boolean
            Dim sNum As Boolean
            sNum = False
            Dim i As Integer
            For i = 0 To (list.Items.Count - 1)
                If (list.Items(i) = iNum) Then
                    sNum = True
                End If
            Next
            isSame = sNum
        End Function
    
        Public Function isInArray(ByVal arr() As Integer, ByVal testNum As Integer) As Boolean
            Dim look As Boolean
            look = False
            Dim i As Integer
            For i = 0 To (Roll - 1)
                If ((testNum) = nNums(i)) Then
                    testNum = i
                    look = True
                End If
            Next
            isInArray = look
        End Function
    
    
        Public Function isWin(ByRef lstName As ListBox, ByVal arr() As Integer) As Integer
            Dim Check As Integer
            Dim i As Integer  'This is the part where the Function of the problem is
            Dim j As Integer
            For i = 0 To (Roll - 1)
                For j = 0 To (Roll - 1)
                    If nNums(i) = lstName.Items(j) Then
                        Check = Check + 1
                    End If
                Next
            Next
            isWin = Check
    
        End Function
    
    
    
        Private Sub btnRollWin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRollWin.Click
            tmrWin.Enabled = True         ' This is where I want to use the function
            If isWin(lstPlay1, nNums(lblWin.Text)) > isWin(lstPlay2, nNums(lblWin.Text)) Then
                MsgBox("Player 1 Won!", MsgBoxStyle.Exclamation = MsgBoxStyle.OkOnly, "Game Over")
    
    
            End If
    
        End Sub
    
    
        Private Sub tmrWin_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrWin.Tick
            Randomize()
            Dim num As Integer
            Static i As Integer
    
            Do While (Count < Roll)
                num = Int((uNum - lNum + 1) * Rnd() + lNum)
                If (isInArray(nNums, num) = False) Then
                    nNums(Count) = num
                    Count = Count + 1
                End If
            Loop
            lblWin.Text = lblWin.Text & "  " & nNums(i)
            i = i + 1
            If i = Roll Then
                tmrWin.Enabled = False
            End If
    
    
    
        End Sub
    
        Public Sub New()
            InitializeComponent()
            btnRollWin.Enabled = False
        End Sub
    End Class

    If someone can help me you would have no idea how thankful i would be...

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Need help with Function [2005] [2008]

    Welcome to the Forums.

    An array is a type - System.Array

    Try this but also change your passed in var to an array too
    Code:
    Public Function isWin(ByRef lstName As ListBox, ByVal arr() As Array) As Integer
    Although there are alot of improvements you can do in your code, once you get it working we can optimize it
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: Need help with Function [2005] [2008]

    vb Code:
    1. Public Function isWin(ByRef lstName As ListBox, ByVal arr() As Integer) As Integer
    2.         Dim Check As Integer
    3.         Dim i As Integer  'This is the part where the Function of the problem is
    4.         Dim j As Integer
    5.         For i = 0 To (Roll - 1)
    6.             For j = 0 To (Roll - 1)
    7.                 If nNums(i) = lstName.Items(j) Then
    8.                     Check = Check + 1
    9.                 End If
    10.             Next
    11.         Next
    12.         isWin = Check
    13.  
    14.     End Function

    you never use arr in that function?
    maybe nNums(i) should be arr(i) ?
    instead of "iswin=blablabla" you can use "return blablabla"

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    I know there are a couple of problems in it... The teacher wants us to use Functions and such for things we have double code for, thats why I have it as a function... what I need to do is that the lblWin does not read as a function when I placed in nNums(lblWin.Text) The Function you sent me didn't do anything.... I need it to read the random numbers I placed in a label and compare it to the numbers in the list box

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    Quote Originally Posted by gnaver

    you never use arr in that function?
    maybe nNums(i) should be arr(i) ?
    instead of "iswin=blablabla" you can use "return blablabla"
    didn't like that... when I switched nNums to Arr it just said it wasn't part of the array

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    anyone can help me?

  7. #7
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: Need help with Function [2005] [2008]

    ok, the iswin function need to have an listbox and a array of integers, right?

    the array of integers (arr()) is never used....

    when you call the function you use:
    vb Code:
    1. isWin(lstPlay1, nNums(lblWin.Text))

    lstPlay1 i guess is a listbox, but nNums(lblWin.Text) is an integer not an array!

    dont know if im all wrong here, but imo you should change either the call og the function

  8. #8

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    Quote Originally Posted by gnaver
    ok, the iswin function need to have an listbox and a array of integers, right?

    the array of integers (arr()) is never used....

    when you call the function you use:
    vb Code:
    1. isWin(lstPlay1, nNums(lblWin.Text))

    lstPlay1 i guess is a listbox, but nNums(lblWin.Text) is an integer not an array!

    dont know if im all wrong here, but imo you should change either the call og the function

    You are right I just don't know how to change nNums(lblWin.text) back to an array cause when I created the lbl
    Code:
    Randomize()
            Dim num As Integer
            Static i As Integer
    
            Do While (Count < Roll)
                num = Int((uNum - lNum + 1) * Rnd() + lNum)
                If (isInArray(nNums, num) = False) Then
                    nNums(Count) = num
                    Count = Count + 1
                End If
            Loop
            lblWin.Text = lblWin.Text & "  " & nNums(i)
            i = i + 1
            If i = Roll Then
                tmrWin.Enabled = False
            End If
    I created it using an array... I need to compare each array random numbers to the numbers the user places into the list boxes in order to see who won, I need to see how many numbers the user guesses correctly from the random numbers the computer places as the "Winning Numbers"

  9. #9
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: Need help with Function [2005] [2008]

    just try doing this when you call the function: isWin(lstPlay1, nNums)

    or simply remove the arr() as integer from the function and nNums(lblWin.text) from the call

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    ok, so that did help me a lot here... but now that I have changed it to look like this
    Code:
     Public Function isWin(ByRef lstName As ListBox, ByVal arr() As Integer) As Integer 'Public Function isWin(ByRef lstName As ListBox, ByVal arr() As Integer) As Integer
            Dim Check As Integer
            Dim i As Integer
            Dim j As Integer
            For i = 0 To (Roll - 1)
                For j = 0 To (Roll - 1)
                    If arr(i) = lstName.Items(j) Then
                        Check = Check + 1
                    End If
                Next
            Next
            isWin = Check
    
        End Function
    
    
    
        Private Sub btnRollWin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRollWin.Click
            tmrWin.Enabled = True
            Dim play1 As Integer
            Dim play2 As Integer
            play1 = isWin(lstPlay1, nNums)
            play2 = isWin(lstPlay2, nNums)
            If play1 > play2 Then
                MsgBox("Player 1 Won!", MsgBoxStyle.Exclamation = MsgBoxStyle.OkOnly, "Game Over")
            End If
            If play1 < play2 Then
                MsgBox("Player 2 Won!", MsgBoxStyle.Exclamation = MsgBoxStyle.OkOnly, "Game Over")
            End If
            If play1 = play2 And play2 <> 0 Then
                MsgBox("Tie!", MsgBoxStyle.Exclamation = MsgBoxStyle.OkOnly, "Game Over")
            End If
            If play1 = play2 And play2 = 0 Then
                MsgBox("No Win!", MsgBoxStyle.Exclamation = MsgBoxStyle.OkOnly, "Game Over")
            End If
        End Sub
    It only tells me if there is a tie or no win, it doesn't tell me when and which player wins

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Need help with Function [2005] [2008]

    I may have missed it, but I don't see where lstPlay1 and lstPlay2 come from. From looking at your code, if you are only getting the last two options, but are getting BOTH of the last two options (not just one of them), then I would have to say that lstPlay1 = lstPlay2.

    Have you learned about breakpoints and stepping through code? If not, a breakpoint can be set on a line by clicking in the left margin of the code window. That should cause the line to be highlighted in some color like maroon. When execution hits that breakpoint, it will pause, and switch to the code. At that point, you can see the value of any variable that is in scope, and evaluate any expression, by highlighting the variable or expression and pressing Shift+F9. You can also step through the code line by line using F10 or F11, and you can resume normal processing with F5. Those are the basic tools for debugging, and you'll be needing them. In this case, you can put a breakpoint on the first If statement, and look at the values in play1 and play2. I suspect that you will find that play1 = play2 for all cases.
    My usual boring signature: Nothing

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    yea your right, its equal to zero... but I have an array there... and the array is 0 ... how do I collect the arry I used before ?

    PHP Code:
    Randomize()
            
    Dim num As Integer
            
    Static As Integer

            
    Do While (Count Roll)
                
    num Int((uNum lNum 1) * Rnd() + lNum)
                If (
    isInArray(nNumsnum) = FalseThen
                    nNums
    (Count) = num
                    Count 
    Count 1
                End 
    If
            
    Loop
            lblWin
    .Text lblWin.Text "  " nNums(i)
            
    1
            
    If Roll Then
                tmrWin
    .Enabled False
            End 
    If 
    i want to collect this array and compare it to the items in the list boxes

  13. #13
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Need help with Function [2005] [2008]

    Where's you get the Randomize bit? Is your class teaching you that? That's old VB6 style random numbers, and a major pain in the butt! Take a look at the Random object. It is a VAST improvement. No more call to randomize, no more hi - lo * rnd +1 stuff. You just call Random.GetNext, possibly with the range you want the random number to fall in.

    Also, what's isInArray? I see what it does, it's basically Array.Contains, except that I can't remember if Contains is actually a member of Array. Array.IndexOf does the same thing, but returns an integer rather than a boolean.

    As to the array, are you talking about nNums? I don't see anything wrong with that. It's declared outside of the sub, so it is visible to all subs in the form. I would declare it using Private rather than Dim, but I don't think it makes a whit of difference. What I was seeing is that since nNums is passed to isWin for both player 1 and player 2, then it will be the same for the two. The only way the score would be the same for the two is if the two listboxes being passed to isWin are also the same.

    Technically, if Roll is 0, then isWin will return 0 in all cases, but you would never see a tie in that case. If you are seeing a tie, then two listboxes have to either contain the exact same items, or there must be a match in items between nNums and both listboxes. That seems improbable, but I don't know what the listboxes hold.
    My usual boring signature: Nothing

  14. #14

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    The list boxes hold what the user types into it from the text box of thiers... Just 6 random numbers. I have 6 numbers in my label after I press winning numbers "12 23 35 31 11 3" something like this, I want to take these numbers seperated and compare them to the users numbers in the list boxes

  15. #15
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Need help with Function [2005] [2008]

    Ah, a lottery winner game.

    Are you ever getting ties, or is it always No Win?
    My usual boring signature: Nothing

  16. #16

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    Now its always no win, at one point in time I got both and before that I got a specific winner or loser... don't know how but with the shown code its always no win... at least so far, I am trying to play around with it but I seem to make it worse
    "First there was nothing... Then God Said 'LET THERE BE LIGHT!' ...Then there was still nothing, but you could see it."

  17. #17

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    I think there is a problem that when the timer starts the code doesn't bring in those 6 numbers till after it checks if the array is equal to the label... when at the time the label doesn't have anything in it
    "First there was nothing... Then God Said 'LET THERE BE LIGHT!' ...Then there was still nothing, but you could see it."

  18. #18
    Lively Member
    Join Date
    Aug 2007
    Posts
    97

    Re: Need help with Function [2005] [2008]

    Since you are getting no wins then this may suggest Iswin is returning the same value for each player.

    Try stepping through your code to see what Iswin returns back. If you don't know how to step through it just msgbox it.

  19. #19

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    Let me repeat, I am a Noob it gives me the return Value of 0 but I don't know how to take the array i want to give me the proper definitions... here is the total code...

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim Roll As Integer = 6
    4.     Dim nNums(Roll - 1) As Integer
    5.     Const uNum As Integer = 49
    6.     Const lNum As Integer = 1
    7.     Dim Count As Integer
    8.     Dim GameCount As Integer
    9.  
    10.  
    11.     Private Sub btnEnter1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnt1.Click
    12.         If (IsNumeric(txtPlay1.Text) = False) Then
    13.             MsgBox("Has To Be a Number", MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
    14.             txtPlay1.Text = " "
    15.             txtPlay1.Focus()
    16.         ElseIf (isValid(txtPlay1.Text) = False) Then
    17.             MsgBox("Has To Be " & lNum & "-" & uNum, MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
    18.             txtPlay1.Text = " "
    19.             txtPlay1.Focus()
    20.         ElseIf (isSame(lstPlay1, txtPlay1.Text) = True) Then
    21.             MsgBox("Has To Be a different number", MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
    22.             txtPlay1.Text = " "
    23.             txtPlay1.Focus()
    24.         Else
    25.             lstPlay1.Items.Add(txtPlay1.Text)
    26.             txtPlay1.Text = ""
    27.             txtPlay1.Focus()
    28.             If lstPlay1.Items.Count = Roll Then
    29.                 txtPlay1.Text = ""
    30.                 btnEnt1.Enabled = False
    31.                 txtPlay1.Enabled = False
    32.             End If
    33.         End If
    34.         If (btnEnt1.Enabled = False) And (btnEnt2.Enabled = False) Then
    35.             btnRollWin.Enabled = True
    36.         End If
    37.     End Sub
    38.     Private Sub btnEnt2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnt2.Click
    39.  
    40.         If (IsNumeric(txtPlay2.Text) = False) Then
    41.             MsgBox("Has To Be a Number", MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
    42.             txtPlay2.Text = " "
    43.             txtPlay2.Focus()
    44.         ElseIf (isValid(txtPlay2.Text) = False) Then
    45.             MsgBox("Has To Be " & lNum & "-" & uNum, MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
    46.             txtPlay2.Text = " "
    47.             txtPlay2.Focus()
    48.  
    49.         ElseIf (isSame(lstPlay2, txtPlay2.Text) = True) Then
    50.             MsgBox("Has To Be a different number", MsgBoxStyle.Information = MsgBoxStyle.OkOnly, "For your Information")
    51.             txtPlay2.Text = " "
    52.             txtPlay2.Focus()
    53.         Else
    54.             lstPlay2.Items.Add(txtPlay2.Text)
    55.             txtPlay2.Text = ""
    56.             txtPlay2.Focus()
    57.             If lstPlay2.Items.Count = Roll Then
    58.                 txtPlay2.Text = ""
    59.                 btnEnt2.Enabled = False
    60.                 txtPlay2.Enabled = False
    61.             End If
    62.         End If
    63.         If (btnEnt1.Enabled = False) And (btnEnt2.Enabled = False) Then
    64.             btnRollWin.Enabled = True
    65.         End If
    66.     End Sub
    67.     Public Function isValid(ByVal iNum As Integer) As Boolean
    68.         If (iNum >= lNum And iNum <= uNum) Then
    69.             isValid = True
    70.         Else
    71.             isValid = False
    72.         End If
    73.  
    74.     End Function
    75.  
    76.     Public Function isSame(ByRef list As ListBox, ByVal iNum As Integer) As Boolean
    77.         Dim sNum As Boolean
    78.         sNum = False
    79.         Dim i As Integer
    80.         For i = 0 To (list.Items.Count - 1)
    81.             If (list.Items(i) = iNum) Then
    82.                 sNum = True
    83.             End If
    84.         Next
    85.         isSame = sNum
    86.     End Function
    87.  
    88.     Public Function isInArray(ByVal arr() As Integer, ByVal testNum As Integer) As Boolean
    89.         Dim look As Boolean
    90.         look = False
    91.         Dim i As Integer
    92.         For i = 0 To (Roll - 1)
    93.             If ((testNum) = nNums(i)) Then
    94.                 testNum = i
    95.                 look = True
    96.             End If
    97.         Next
    98.         isInArray = look
    99.     End Function
    100.  
    101.     Public Function isWin(ByRef lstName As ListBox, ByVal arr() As Integer) As Integer 'Public Function isWin(ByRef lstName As ListBox, ByVal arr() As Integer) As Integer                      
    102.  Dim Check As Integer                      'From here is the problem
    103.  
    104.         Dim i As Integer
    105.         Dim j As Integer
    106.         For i = 0 To (Roll - 1)
    107.             For j = 0 To (Roll - 1)
    108.                 If nNums(i) = lstName.Items(j) Then
    109.                     Check = Check + 1
    110.                 End If
    111.             Next
    112.         Next
    113.         isWin = Check
    114.  
    115.     End Function
    116.  
    117.  
    118.  
    119.     Private Sub btnRollWin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRollWin.Click
    120.         tmrWin.Enabled = True
    121.         Dim play1 As Integer
    122.         Dim play2 As Integer
    123.         play1 = isWin(lstPlay1, nNums)
    124.         play2 = isWin(lstPlay2, nNums)
    125.         If play1 > play2 Then
    126.             MsgBox("Player 1 Won!", MsgBoxStyle.Exclamation = MsgBoxStyle.OkOnly, "Game Over")
    127.         End If
    128.         If play1 < play2 Then
    129.             MsgBox("Player 2 Won!", MsgBoxStyle.Exclamation = MsgBoxStyle.OkOnly, "Game Over")
    130.         End If
    131.         If play1 = play2 And play2 <> 0 Then
    132.             MsgBox("Tie!", MsgBoxStyle.Exclamation = MsgBoxStyle.OkOnly, "Game Over")
    133.         End If
    134.         If play1 = play2 And play2 = 0 Then
    135.             MsgBox("No Win!", MsgBoxStyle.Exclamation = MsgBoxStyle.OkOnly, "Game Over")
    136.         End If
    137.     End Sub
    138.  
    139.  
    140.     Private Sub tmrWin_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tmrWin.Tick
    141.         Randomize()
    142.         Dim num As Integer
    143.         Static i As Integer
    144.  
    145.         Do While (Count < Roll)
    146.             num = Int((uNum - lNum + 1) * Rnd() + lNum)
    147.             If (isInArray(nNums, num) = False) Then
    148.                 nNums(Count) = num
    149.                 Count = Count + 1
    150.             End If
    151.         Loop
    152.         lblWin.Text = lblWin.Text & "  " & nNums(i)
    153.         i = i + 1
    154.         If i = Roll Then
    155.             tmrWin.Enabled = False
    156.         End If
    157.     End Sub           'To here is the problem
    158.  
    159.     Public Sub New()
    160.         InitializeComponent()
    161.         btnRollWin.Enabled = False
    162.     End Sub
    163. End Class


    I wrote 'comments' to where i think the problems are... i got to get to sleep soon its really late here and I have to wake up early tomarrow. I hope that people will try and help me figure this out but if not i would understand. Thanks to everyone who has attempted to help me. I am really thankful
    "First there was nothing... Then God Said 'LET THERE BE LIGHT!' ...Then there was still nothing, but you could see it."

  20. #20
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Need help with Function [2005] [2008]

    The isInArray function can be replaced with this one line:

    Array.IndexOf(nNums,num) > -1

    The problem is roughly where you thought it was. By starting the timer when the button click is pressed, unless that timer was insanely fast, the rest of the code in the button click event will execute before the timer tick event is raised even one time. For that to work, you need to have pretty much everything that is currently in the button click event moved over into the timer tick event. If you were to just start the timer in the button click event, and copied the rest of the btnRollWin click event handler to immediately after you set the timer enabled to false in the timer tick event, then you would actually have it performing the way you expect it to. I am assuming that the timer should run through a few ticks (until i=row) before the result is evaluated, and moving the code to the tick event is the best way to handle that.
    My usual boring signature: Nothing

  21. #21

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    The problem with that is that I get 6 message boxes if I put it into the timer event. I need to go over the information onces and not everytime it ticks
    "First there was nothing... Then God Said 'LET THERE BE LIGHT!' ...Then there was still nothing, but you could see it."

  22. #22

    Thread Starter
    Junior Member
    Join Date
    Jun 2008
    Posts
    23

    Re: Need help with Function [2005] [2008]

    Alright, it was a lot easier then I figured.... I feel stupid, I got it well... I think I did.... until the next part at least
    "First there was nothing... Then God Said 'LET THERE BE LIGHT!' ...Then there was still nothing, but you could see it."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width