[RESOLVED] If statement not catching - Different
VB Code:
If InStr(comFinder, "!roll") = 1 Then
If Len(comFinder) = 7 Or 8 Then
numFinder = InStr(comFinder, " ")
'Error here.
fstNum = Right(comFinder, Len(comFinder) - numFinder)
sendToIRC (sendMsgText & " PRIVMSG #shadowfall :" & charName & " rolled " & CStr(fstNum) & " dice: " & diceRoller(fstNum, 0))
ElseIf Len(comFinder) = 9 Or 10 Or 11 Then
numFinder = InStr(comFinder, " ")
numFinder2 = InStr(numFinder + 1, comFinder, " ")
fstNum = Mid(comFinder, numFinder + 1, numFinder2 - 1)
sndNum = Mid(comFinder, numFinder2 + 1, Len(comFinder) - (numFinder2 + 1))
sendToIRC (sendMsgText & " PRIVMSG #shadowfall :" & charName & " rolled " & CStr(fstNum) & " dice: " & diceRoller(fstNum, sndNum))
End If
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. :)
Re: If statement not catching
Quote:
Originally Posted by CyberInfantry
VB Code:
If Len(comFinder) = 7 Or 8 Then
Change this to:
VB Code:
If Len(comFinder) = 7 Or Len(comFinder) = 8 Then
Re: If statement not catching
Ah, perfect! I feel so dumb some times. :P
Re: If statement not catching - Different
I changed it to a Select Case.
VB Code:
Select Case Len(comFinder)
Case 7, 8
numFinder = InStr(comFinder, " ")
fstNum = Right(comFinder, Len(comFinder) - numFinder)
sendToIRC (sendMsgText & " PRIVMSG #shadowfall :" & charName & " rolled " & CStr(fstNum) & " dice:" & diceRoller(fstNum, 0))
Case 9, 10, 11
numFinder = InStr(comFinder, " ")
numFinder2 = InStr(numFinder + 1, comFinder, " ")
'Error here
fstNum = Mid(comFinder, numFinder + 1, (numFinder2 - numFinder))
sndNum = Mid(comFinder, numFinder2 + 1, Len(comFinder) - (numFinder2 + 1))
sendToIRC (sendMsgText & " PRIVMSG #shadowfall :" & charName & " rolled " & CStr(fstNum) & " dice:" & diceRoller(fstNum, sndNum))
End Select
Now it is catch at the new marked spot. -sigh-
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:
Dim Substrings() As String
' ...
Substrings = Split(comFinder) ' Default splits on space character and returns all strings
If Substrings(0) = "!roll" Then
If UBound(Substrings) > 0 Then ' If there is at least 1 substring after "!roll"
fstNum = Substrings(1) ' Get the first substring
If UBound(Substrings) > 1 Then ' If there are at least 2 substrings after "!roll"
sndNum = Substrings(2) ' Get the second substring
Else
sndNum = "0"
End If
Else
fstNum = "0"
sndNum = "0"
End If
' ...
End If
Re: If statement not catching - Different
:eek: :eek: :eek:
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?
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.
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))
?
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.
Re: If statement not catching - Different
You're the man! :D 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. :)