Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: Learn on May 02, 2004, 01:12 PM

Title: [VB]Problem with "Select Case e(0)"
Post by: Learn on May 02, 2004, 01:12 PM
Well, I've just started programming with VB and I'm working on a simple and easy chat bot. But I keep getting an error when I Play then Connect it. I was hoping someone can help.

Heres there error I get:

Run-time error '9':

Subscript out of range.

Then I click debug and it highlights " Select Case e(0) "

Please help.  
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: CodeMaster on May 02, 2004, 01:17 PM
Perhaps a little bit of code would make it easier for us people who I might remind you are not psychics.

Edit: Usually when you receive Subscript out of range when using an array, you are referring to an array variable that hasn't been declared as anything. This usually happens when using Split().
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Learn on May 02, 2004, 01:18 PM
Sorry. Heres some of the code:

Private Sub sckBNET_DataArrival(ByVal bytesTotal As Long)
Dim i&
  Dim a
  Dim e
  Dim p
      sckBNET.GetData s, vbString
      a = Split(s, Chr(13))
      For i = LBound(a) To UBound(a)
          If Left(a(i), 1) = Chr(10) Then a(i) = Right(a(i), Len(a(i)) - 1)
          e = Split(a(i), Chr(32), 1)
          Select Case e(0)
          Case "1001"
                  p = Split(a(i), Chr(32), 5)
                  p(4) = Mid(p(4), 2, Len(p(4)) - 2)
                  AddChat vbYellow, p(2) & " is in the channel."
              Case "1002"
                  p = Split(a(i), Chr(32), 5)
                  p(4) = Mid(p(4), 2, Len(p(4)) - 2)
                  AddChat vbGreen, p(2) & " joined the channel."
              Case "1003"
                  p = Split(a(i), Chr(32), 3)
                  AddChat vbRed, p(2) & " left the channel."
              Case "1004"
                  p = Split(a(i), Chr(32), 4)
                  AddChat vbYellow, "<From: " & p(2) & "> ", vbgray, Mid(p(4), 2, Len(p(4)) - 2)
              Case "1005"
                  p = Split(a(i), Chr(32), 4)
                  AddChat vbYellow, "<" & p(2) & "> ", vbWhite, Mid(p(4), 2, Len(p(4)) - 2)
              Case "1007"
                  p = Split(a(i), Chr(32), 2)
                  'Mid(p(2), 2, Len(p(2)) - 2) Channel
              Case "1009"
                  p = Split(a(i), Chr(32), 3)
                  'p(2) Flags
              Case "1010"
                  p = Split(a(i), Chr(32), 4)
                  AddChat vbCyan, "<To: " & p(2) & "> ", vbgray, Mid(p(4), 2, Len(p(4)) - 2)
              Case "1018"
                  p = Split(a(i), Chr(32), 2)
                  AddChat vbYellow, Mid(p(2), 2, Len(p(2)) - 2)
              Case "1019"
                  p = Split(a(i), Chr(32), 2)
                  AddChat vbRed, "Error: " & Mid(p(2), 2, Len(p(2)) - 2)
              Case "1023"
                  p = Split(a(i), Chr(32), 4)
                  AddChat vbYellow, "<" & p(2) & " " & Mid(p(4), 2, Len(p(4)) - 2) & ">"
              Case "2000"
                  '
              Case "2010"
                  p = Split(a(i), Chr(32), 2)
                  'p(2) Logon
              Case Else
                  '
          End Select
      Next i
End Sub
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: CodeMaster on May 02, 2004, 01:21 PM
On first look I can see that e is not declared as an array
Dim e() As String or w/e would work, same would apply to a and p

Edit: Is that the problem???
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Learn on May 02, 2004, 01:26 PM
Ok, well i fixed the Dim a(and e and p as well) As String, but now im getting:

Compile Error:

Expected Array.

bah ><

Edit: Yea the Dim a+e+p As String worked but now im getting the above error.
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: CodeMaster on May 02, 2004, 01:27 PM
Does it point to any specific point of your code?
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Learn on May 02, 2004, 01:28 PM
Yea sorry, forgot that:

Private Sub sckBNET_DataArrival(ByVal bytesTotal As Long)


Edit: Private Sub sckBNET_DataArrival(ByVal bytesTotal As Long) is highlighted in yellow

and

For i = LBound(a) To UBound(a)
the LBound is highlighted in blue
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Eric on May 02, 2004, 03:26 PM
It most likely means that e(0) does not contain any and/or enough information required to finish the process.
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Grok on May 02, 2004, 05:09 PM
Quote from: Learn on May 02, 2004, 01:28 PM
Yea sorry, forgot that:

Private Sub sckBNET_DataArrival(ByVal bytesTotal As Long)


Edit: Private Sub sckBNET_DataArrival(ByVal bytesTotal As Long) is highlighted in yellow

and

For i = LBound(a) To UBound(a)
the LBound is highlighted in blue

Your variable a is not an array.  Declare it as an array.
Dim a() As (appropriate datatype here)
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Learn on May 02, 2004, 06:30 PM
Ok, this issue has been resolved.

I used the InStr command to solve it.

But now once i connect my bot using VB the Chattering in the channel doesn't show up on my screen, I can't join channels either. But i CAN whisper people. Also, when i try to type any of the commands you'd normally use on a Client, i hear a beep and nothing happens.

Would you need to see the code to see what is wrong? If so i'll edit this and put it in.

(this is just a Chat Bot, I know that it cant join private channels so that isnt my problem with the joining channels.)

Edit: I can also enter text to B.net through the bot, but again the text doesn't show up on the screen.

Code:

Private Sub sckBNET_DataArrival(ByVal bytesTotal As Long)
   Dim i&
   Dim a() As String
   Dim e() As String
   Dim p() As String
       sckBNET.GetData s, vbString
       a = Split(s, Chr(13))
       For i = LBound(a) To UBound(a)
           If Left(a(i), 1) = Chr(10) Then a(i) = Right(a(i), Len(a(i)) - 1)
           If InStr(1, a(i), Chr(32)) > 0 Then
           e = Split(a(i), Chr(32), 1)
           Else
           ReDim e(0)
           e(0) = a(i)
           End If
           Select Case e(0)
           Case "1001"
                   p = Split(a(i), Chr(32), 5)
                   p(4) = Mid(p(4), 2, Len(p(4)) - 2)
                   AddChat vbYellow, p(2) & " is in the channel."
               Case "1002"
                   p = Split(a(i), Chr(32), 5)
                   p(4) = Mid(p(4), 2, Len(p(4)) - 2)
                   AddChat vbGreen, p(2) & " joined the channel."
               Case "1003"
                   p = Split(a(i), Chr(32), 3)
                   AddChat vbRed, p(2) & " left the channel."
               Case "1004"
                   p = Split(a(i), Chr(32), 4)
                   AddChat vbYellow, "<From:" & p(2) & ">", vbgray, Mid(p(4), 2, Len(p(4)) - 2)
               Case "1005"
                   p = Split(a(i), Chr(32), 4)
                   AddChat vbYellow, "<" & p(2) & ">", vbWhite, Mid(p(4), 2, Len(p(4)) - 2)
               Case "1007"
                   p = Split(a(i), Chr(32), 2)
                   'Mid(p(2), 2, Len(p(2)) - 2) Channel
               Case "1009"
                   p = Split(a(i), Chr(32), 3)
                   'p(2) Flags
               Case "1010"
                   p = Split(a(i), Chr(32), 4)
                   AddChat vbCyan, "<To:" & p(2) & ">", vbgray, Mid(p(4), 2, Len(p(4)) - 2)
               Case "1018"
                   p = Split(a(i), Chr(32), 2)
                   AddChat vbYellow, Mid(p(2), 2, Len(p(2)) - 2)
               Case "1019"
                   p = Split(a(i), Chr(32), 2)
                   AddChat vbRed, "Error: " & Mid(p(2), 2, Len(p(2)) - 2)
               Case "1023"
                   p = Split(a(i), Chr(32), 4)
                   AddChat vbYellow, "<" & p(2) & " " & Mid(p(4), 2, Len(p(4)) - 2) & ">"
               Case "2000"
                   '
               Case "2010"
                   p = Split(a(i), Chr(32), 2)
                   'p(2) Logon
               Case Else
                   '
               End Select
       Next i
End Sub
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: hismajesty on May 02, 2004, 06:33 PM
For not seeing messages: Post your parse code.
For the beep: (assuming) you're hearing it when you press enter you have to do this:

If Keyascii = 13 Then
Keyascii = 0 'Stop beep
'The rest of the stuff

Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Learn on May 02, 2004, 06:40 PM
Uhh your code is slightly different then mine:

heres mine:

Private Sub txtSend_KeyPress(KeyAscii As Integer)
   If KeyAscii = 13 Then
       sckBNET.SendData txtSend.Text & vbCrLf
       txtSend.Text = ""
       End If


and i'll edit the above post with the code
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Eli_1 on May 02, 2004, 06:48 PM
'Stop beep is a comment, it is simply explaining what that line of code does.
To stop the beep you put
KeyAscii = 0
after your if statement.

[Edit] On a side note, try to get into the habit of putting [ code ] [ /code ] tags around code you post.

[Edit again!] Your variable names are horrible, try to use less cryptic variable names. For instance, you used s as one of your strings. Using something like MessageBuffer would be much better and easier to understand.
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Learn on May 02, 2004, 06:51 PM
 ::)Thanks Eli_1

And why do i put around the code i post?

Can't i just click the little box that says "Check this if you'll be adding code (or don't like smilies).

Edit: OOOH I see. I'll do that now.

Edit to Eli_1's edit: I don't understand. Whats MessageBuffer?

:-[ :'( ::)
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: hismajesty on May 02, 2004, 08:42 PM
I was just posting the relevant code.


Private Sub txtSend_KeyPress(KeyAscii As Integer)
   If KeyAscii = 13 Then
       KeyAscii = 0
          sckBNET.SendData txtSend.Text & vbCrLf
       txtSend.Text = vbNullString
   End If
End Sub


using the [ code] and [ /code] tags just makes the code easier to read.

He was just mentioning that you need to use more meaningful variable names.

Use vbNullString instead of "" so it doesn't take up space in the memory.
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Learn on May 02, 2004, 10:30 PM
Thanks, but can anyone help me with my problem?
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: hismajesty on May 03, 2004, 04:06 PM
Quote from: Learn on May 02, 2004, 10:30 PM
Thanks, but can anyone help me with my problem?

You posted that it was resolved.
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Learn on May 03, 2004, 07:58 PM
QuoteOk, this issue has been resolved.

I used the InStr command to solve it.

But now once i connect my bot using VB the Chattering in the channel doesn't show up on my screen, I can't join channels either. But i CAN whisper people. Also, when i try to type any of the commands you'd normally use on a Client, i hear a beep and nothing happens.

That problem
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Eli_1 on May 03, 2004, 08:42 PM
Are you appending vbCrLf to the end of the text you send?
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Learn on May 04, 2004, 07:13 AM
Please use an example of what you mean, because i really dont KNow what u mean.


p.s. : nice avatar  ATHF rocks =]
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Eli_1 on May 04, 2004, 01:47 PM
Quote from: Learn on May 04, 2004, 07:13 AM
Please use an example of what you mean, because i really dont KNow what u mean.


p.s. : nice avatar  ATHF rocks =]


Const VK_RETURN = 13
If KeyAscii = VK_RETURN Then
   WinSock.SendData txtInput.Text & vbCrLf
   '/* Notice I'm putting vbCrLf on the end of the text :O */

   DoEvents
   '/* DoEvents isn't needed. I use it
   '** because on one of my really old
   '** computers it won't send if I leave DoEvents
   '** out. Btw, these are all comments, learn. */

   KeyAscii = 0 '// Don't beep -.-
End If


P.S. LOL, finally someone recognizes the character. ATHF def. rocks. :)


Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Learn on May 04, 2004, 03:57 PM
Ok I know what a comment is I'm not THAT much of a newb with VB. but thanks for the vbCrLf tip i didn't know that.
Title: Re:[VB]Problem with "Select Case e(0)"
Post by: Eli_1 on May 04, 2004, 04:23 PM
Quote from: Learn on May 04, 2004, 03:57 PM
Ok I know what a comment is I'm not THAT much of a newb with VB. but thanks for the vbCrLf tip i didn't know that.
Last post I read, someone thought the comment was code. I thought it might have been you, so I put that in there to make sure I didn't confuse you.