• Welcome to Valhalla Legends Archive.
 

[VB]Problem with "Select Case e(0)"

Started by Learn, May 02, 2004, 01:12 PM

Previous topic - Next topic

Learn

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.  

CodeMaster

#1
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().

Learn

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

CodeMaster

#3
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???

Learn

#4
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.

CodeMaster

Does it point to any specific point of your code?

Learn

#6
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

Eric

#7
It most likely means that e(0) does not contain any and/or enough information required to finish the process.

Grok

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)

Learn

#9
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

hismajesty

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


Learn

#11
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

Eli_1

#12
'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.

Learn

#13
 ::)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?

:-[ :'( ::)

hismajesty

#14
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.