Results 1 to 10 of 10

Thread: [RESOLVED] If statement not catching - Different

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2007
    Location
    Riverton, WY
    Posts
    36

    Resolved [RESOLVED] If statement not catching - Different

    VB Code:
    1. If InStr(comFinder, "!roll") = 1 Then
    2.                     If Len(comFinder) = 7 Or 8 Then
    3.                         numFinder = InStr(comFinder, " ")
    4.                        
    5.                         'Error here.
    6.                         fstNum = Right(comFinder, Len(comFinder) - numFinder)
    7.                         sendToIRC (sendMsgText & " PRIVMSG #shadowfall :" & charName & " rolled " & CStr(fstNum) & " dice: " & diceRoller(fstNum, 0))
    8.                    
    9.                     ElseIf Len(comFinder) = 9 Or 10 Or 11 Then
    10.                         numFinder = InStr(comFinder, " ")
    11.                         numFinder2 = InStr(numFinder + 1, comFinder, " ")
    12.                         fstNum = Mid(comFinder, numFinder + 1, numFinder2 - 1)
    13.                         sndNum = Mid(comFinder, numFinder2 + 1, Len(comFinder) - (numFinder2 + 1))
    14.                    
    15.                         sendToIRC (sendMsgText & " PRIVMSG #shadowfall :" & charName & " rolled " & CStr(fstNum) & " dice: " & diceRoller(fstNum, sndNum))
    16.                     End If
    17.                 End If

    If it is sent "!roll 3" or "!roll 12" it works perfectly.

    If it is sent "!roll 3 2", "!roll 10 2", or "!roll 10 10" it fails. It catches on the marked line of code. It shouldn't even be touching that code at this point. It shouldn't be meeting the requirements and should therefore be skipping over that lil' section to the "ElseIf".

    What's going on?

    Thank you all in advance for your time.
    Last edited by CyberInfantry; Jan 23rd, 2007 at 04:57 PM. Reason: No longer resolved

  2. #2
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: If statement not catching

    Quote Originally Posted by CyberInfantry
    VB Code:
    1. If Len(comFinder) = 7 Or 8 Then
    Change this to:
    VB Code:
    1. If Len(comFinder) = 7 Or Len(comFinder) = 8 Then

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2007
    Location
    Riverton, WY
    Posts
    36

    Re: If statement not catching

    Ah, perfect! I feel so dumb some times. :P

  4. #4

    Thread Starter
    Member
    Join Date
    Jan 2007
    Location
    Riverton, WY
    Posts
    36

    Re: If statement not catching - Different

    I changed it to a Select Case.

    VB Code:
    1. Select Case Len(comFinder)
    2.                         Case 7, 8
    3.                             numFinder = InStr(comFinder, " ")
    4.                             fstNum = Right(comFinder, Len(comFinder) - numFinder)
    5.                             sendToIRC (sendMsgText & " PRIVMSG #shadowfall :" & charName & " rolled " & CStr(fstNum) & " dice:" & diceRoller(fstNum, 0))
    6.                    
    7.                         Case 9, 10, 11
    8.                             numFinder = InStr(comFinder, " ")
    9.                             numFinder2 = InStr(numFinder + 1, comFinder, " ")
    10.  
    11.                             'Error here
    12.                             fstNum = Mid(comFinder, numFinder + 1, (numFinder2 - numFinder))
    13.                             sndNum = Mid(comFinder, numFinder2 + 1, Len(comFinder) - (numFinder2 + 1))
    14.                        
    15.                             sendToIRC (sendMsgText & " PRIVMSG #shadowfall :" & charName & " rolled " & CStr(fstNum) & " dice:" & diceRoller(fstNum, sndNum))
    16.                     End Select

    Now it is catch at the new marked spot. -sigh-

  5. #5
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: If statement not catching - Different

    OK, I took a closer look, and I think I know what you are trying to do. Here is a different approach, using the Split function:
    VB Code:
    1. Dim Substrings() As String
    2.  
    3. ' ...
    4. Substrings = Split(comFinder) ' Default splits on space character and returns all strings
    5. If Substrings(0) = "!roll" Then
    6.   If UBound(Substrings) > 0 Then ' If there is at least 1 substring after "!roll"
    7.     fstNum = Substrings(1) ' Get the first substring
    8.     If UBound(Substrings) > 1 Then ' If there are at least 2 substrings after "!roll"
    9.       sndNum = Substrings(2) ' Get the second substring
    10.     Else
    11.       sndNum = "0"
    12.     End If
    13.   Else
    14.     fstNum = "0"
    15.     sndNum = "0"
    16.   End If
    17.   ' ...
    18. End If

  6. #6

    Thread Starter
    Member
    Join Date
    Jan 2007
    Location
    Riverton, WY
    Posts
    36

    Lightbulb Re: If statement not catching - Different


    That looks AMAZINGLY simple! I didn't realize that it acted like that. I had encountred the Split command earlier in the code (I'm not working on my own code, rather; I'm modifying code created by forum member Penderj) but had passed it off as something I didn't know about and probably did not need.

    Do I need to declare any additional variables with that?

  7. #7
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: If statement not catching - Different

    Assuming that all other variables have been declared, you only need to add a declaration for the dynamic Substrings array.

    It has occurred to me that fstNum and sndNum are Integers, not Strings, so my code above would need to modified to assign appropriate values to those variables.

  8. #8

    Thread Starter
    Member
    Join Date
    Jan 2007
    Location
    Riverton, WY
    Posts
    36

    Question Re: If statement not catching - Different

    Quote Originally Posted by Logophobic
    Assuming that all other variables have been declared, you only need to add a declaration for the dynamic Substrings array.

    It has occurred to me that fstNum and sndNum are Integers, not Strings, so my code above would need to modified to assign appropriate values to those variables.

    Modified as in...:

    fstNum = Int(Substrings(1))

    ?

  9. #9
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: If statement not catching - Different

    fstNum = Val(Substrings(1))

    This should do well enough. Int() and CInt() will cause an error if the string contains non-numeric characters, or if the string is zero-length.

  10. #10

    Thread Starter
    Member
    Join Date
    Jan 2007
    Location
    Riverton, WY
    Posts
    36

    Thumbs up Re: If statement not catching - Different

    You're the man! It works like a charm.

    I had a little bit of trouble (due to a logic error on my part), but now it's working perfectly!

    I've been able to expand beyond this section of the code now; and the rest of it is simply a different variation of this.

    You have been a HUGE help.

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