• 0

[vb] Split function and Arrays


Question

hey, I know most of you are Web Coders but Im wondering if anyone out there has some knowledge of vb...

anyway, Im trying to split up a string and plonk it in an array, of course, VB does this with the Split function, the only problem is that it returns an error "Cant assign to array" when I pre-define the array (Dim arry(1) As String). If the Array is assigned as a dynamic array (Dim arry() As String) then it all works fine, but I get a "Subscript out of range" error message when I check to see if arry(1) has anything in it or not.

So far the only way Ive figured to get around it is to use an "On error goto" statement, though I think thats a little shoddy and theres gotta be some way to get the Split function liking fixed arrays...

just for reference, my code looks like so...

Option Explicit
Dim p00p As String
Dim arry(1) As String


Private Sub cmdZoomIn_Click()
'Erase arry

p00p = ActiveWindow.RangeSelection.Address
arry = Split(p00p, ":", 2)
'On Error GoTo zoominerror:
If arry(1) <> "" Then
    MsgBox arry(0)
    MsgBox arry(1)
End If

'zoomcont:

'Exit Sub
'zoominerror:
'    MsgBox "You need to select another number!"
'Resume zoomcont:
End Sub

as you can see, Ive commented out my error goto code and the "erase arry" statement (cause theyre both unnecesary).. any ideas?

Link to comment
https://www.neowin.net/forum/topic/86958-vb-split-function-and-arrays/
Share on other sites

10 answers to this question

Recommended Posts

  • 0

im not entirely sure what u are trying to do here. Are you trying to get a string and split it into to parts. If so well surely the array would need 2 elements instead of 1. As for the cannot assign to array error you would have to put one section of your sting into one variable and then the other part of ur string into another variable and then put them into the array.

  • 0

yes, Im trying to split a string into parts, its actually for a VBA project (macros) in Excel where I get the two Cell addresses that are selected, if only one is selected it will only return one string so only one array value will be filled.

The reason its declared as 1 is because all Arrays start at 0, so I have 0 and 1 in the Array which is two elements, though even if I declare it with 100 I still get the "Cannot assign to Array" message.

Segosa, what would it be looking for? Im not sure whether I should use the On Error Resume Next's because we havent really been taught them yet in the class so I dont know if they will like us using them or not. :wacko:

  • 0

Try not dimensioning the array size in the declaration. Just do:

dim arry() as string

arry = Split(p00p, ":", 2)

VB will handle the sizing. You can check the size with ubound(arry) and and empty array (when used with a split like this) will return a ubound of -1 (If I remember correctly).

  • 0
  Novex said:
Im not sure whether I should use the On Error Resume Next's because we havent really been taught them yet in the class so I dont know if they will like us using them or not. :wacko:

Well it are the most useful thing in VB, almost any error you have, can be got rid of with On Error Resume Next. All it does is go to the next line when an error occurs... but this doesnt really suit your program, i dont know wtf is wrong with the array, i mean, just try what that other person said.

  • 0
  ixsis said:
Try not dimensioning the array size in the declaration. Just do:

dim arry() as string

arry = Split(p00p, ":", 2)

VB will handle the sizing. You can check the size with ubound(arry) and and empty array (when used with a split like this) will return a ubound of -1 (If I remember correctly).

yeah, I did that, the split function worked, but then it keeled over in the if statement when it was checking if something was in key number one, if key 1 hasnt been created then it spits out an error too..

I tried pre-defining the array by setting 0 and 1 to "", but then the split statement buggered up again so Im just going to ask my Tutorial guy next Workshop about it :D

On Error Goto and all that seem to be a very good resource to use, so I figure why not use them

Thanks for the help :D

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.