Results 1 to 14 of 14

Thread: What's wrong with this code? [Resolved]

  1. #1

    Thread Starter
    Junior Member joe_h_31028's Avatar
    Join Date
    Jul 2003
    Posts
    29

    What's wrong with this code? [Resolved]

    Dim intPress As Integer, strPass As String, x As Boolean

    Private Sub Command1_Click()
    strPass = Text1.Text
    If PassCheck() = True Then
    intPress = MsgBox("Correct Password!", vbExclamation, "The result is:")
    Else
    intPress = MsgBox("Incorrect Password", vbCritical, "The result is:")
    End If
    End Sub

    Private Function PassCheck(strPass As String) As Boolean
    PassCheck = False
    If strPass = "loser" Then
    PassCheck = True
    End Function
    Last edited by joe_h_31028; Aug 14th, 2003 at 03:37 PM.

  2. #2
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    Why dont you tell us what happens?
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  3. #3
    Hyperactive Member
    Join Date
    May 2003
    Location
    USA, East Coast
    Posts
    257
    You're not passing anything (namely a string) to SUB PassCheck().

  4. #4

    Thread Starter
    Junior Member joe_h_31028's Avatar
    Join Date
    Jul 2003
    Posts
    29
    I get a compile error when the code gets to the PassCheck() function, it says "Argument not optional". I am practicing with functions, still learning of course, and I can't figure out why its not working.

  5. #5
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    CyberJar got it. But you will have another error too unless you close that If block with an End If in PassCheck
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  6. #6

    Thread Starter
    Junior Member joe_h_31028's Avatar
    Join Date
    Jul 2003
    Posts
    29
    strPass is being passed to PassCheck as an argument, the the value of PassCheck is returned in the form of a boolean to be used in the click procedure.

    At least thats what I thought I coded, what do you think?

  7. #7
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    no, its not what you coded

    Code:
    If PassCheck() = True Then
    you didnt put in the arguement
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  8. #8

    Thread Starter
    Junior Member joe_h_31028's Avatar
    Join Date
    Jul 2003
    Posts
    29
    So I have to have it like this :

    PassCheck(strPass as string)

    in both the calling and called procedures?

  9. #9
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    You are getting that error because you are not filling the required vailables. note that you have declared this..


    VB Code:
    1. Private Function PassCheck(strPass As String) As Boolean
    2. PassCheck = False
    3. If strPass = "loser" Then
    4. PassCheck = True
    5. End Function

    But only pass this..

    VB Code:
    1. if pass check() = true then

    You need to either change you function to this

    VB Code:
    1. Private Function PassCheck(optional strPass As String) As Boolean

    to allow for not using a variable when you call it or you need to supply a value to strPass then you call the function..

    VB Code:
    1. if PassCheck(X) = true then

    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  10. #10
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    No. just strPass
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  11. #11

    Thread Starter
    Junior Member joe_h_31028's Avatar
    Join Date
    Jul 2003
    Posts
    29
    Thanks guys, I believe I have it now.

    I fixed the code as per your instructions, but for learning sake, is this the best way to code this? I just want to be sure I am coding the right way.
    ---------------------------------------------------------------------------------
    Dim intPress As Integer, strPass As String, x As Boolean

    Private Sub Command1_Click()
    strPass = Text1.Text
    If PassCheck(strPass) = True Then
    intPress = MsgBox("Correct Password!", vbExclamation, "The result is:")
    Else
    intPress = MsgBox("Incorrect Password", vbCritical, "The result is:")
    End If
    End Sub

    Private Function PassCheck(strPass As String) As Boolean
    PassCheck = False
    If strPass = "loser" Then
    PassCheck = True
    End If
    End Function
    --------------------------------------------------------------------------------

  12. #12
    Frenzied Member RudyL's Avatar
    Join Date
    Mar 2001
    Location
    Chicago
    Posts
    1,519
    It looks ok from here. I would make one suggestion though..

    If you are not going to do anything with the response from the message box I would change this:


    VB Code:
    1. Private Sub Command1_Click()
    2. strPass = Text1.Text
    3. If PassCheck(strPass) = True Then
    4. intPress = MsgBox("Correct Password!", vbExclamation, "The result is:")
    5. Else
    6. intPress = MsgBox("Incorrect Password", vbCritical, "The result is:")
    7. End If
    8. End Sub

    to this:

    VB Code:
    1. Private Sub Command1_Click()
    2. strPass = Text1.Text
    3. If PassCheck(strPass) = True Then
    4. call MsgBox("Correct Password!", vbExclamation, "The result is:")
    5. Else
    6. call MsgBox("Incorrect Password", vbCritical, "The result is:")
    7. End If
    8. End Sub


    No need to handle a variable if not necesary.. Understand?

    Rudy
    10 different ways to skin a cat and amazingly enough each and every one has the same result, the cat gets skinned! The same can be applied to code, so be nice and accept each others "preferences".

  13. #13
    Hyperactive Member
    Join Date
    Aug 2002
    Posts
    416
    u were forgetting to pass the pw argument, and u forgot an 'end if' in ur passcheck func.

  14. #14

    Thread Starter
    Junior Member joe_h_31028's Avatar
    Join Date
    Jul 2003
    Posts
    29
    Got it guys, special thanks to RudyL and Cander for your help and advice. I guess now I can finally move to hour 14 lol

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