• 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.
  • Posts

    • It's just exactly what it looks like. The facial polygon count is low and rigid, and the background appears heavily blurred. You can clearly see jagged edges around the car window as it exits the tunnel. The character animations feel like they're from 2010. The Xbox Series S has performance similar to an RX 570 or GTX 1660, which is disappointing and contributes to holding back game development. The Switch GPU is roughly on par with a 2050 or 3050, which is even worse. Developers have often discussed the challenges and limitations of developing for outdated/low end hardware, noting how it can hold back progress. This isn't a new issue. I am sure we will hear about the "Challenges" they had to overcome here and sacrifices that had to be made for compatibility with the outdated/low end hardware.
    • Whether on Edge or Firefox, I don't get this prompt to accept cookies, because they're already whitelisted by the Cookie AutoDelete extension, both on Edge and Firefox. And, as I've already said, using Edge (yesterday), Neowin isn't whitelisted (I never use Edge), and I didn't get that pop-up. And this morning, after checking, still no pop-up, and everything is activated, the proof (screenshot), I have no ads on the right side. I must add that I block the following trackers Doubleclick, and Scorecardresearch, as well as a few other undesirable ones, via the Host file. Back to square one! After all, it's a simple click to continue, so I'll do with it. 🤷🏽‍♂️ Thank You
    • F'ing FIIINALLY! Much easier to ask people what their hardware is 🙏 and could also very well just say DDR4 or DDR5 without asking to bloat it too much. I'm also curious to see what it will say in More device info...
    • Sure, that must be why it was revealed at the Sony showcase... the game was announced 5 years ago and the visuals are far from poor, what the hell are you smoking, plus the Switch 2 is pretty much on par with an Xbox Series S. It would be absolutely insane for a dev to build a game "with the Switch 2 in mind" when it's coming to all platforms where the majority of sales will happen.
  • Recent Achievements

    • Week One Done
      jbatch earned a badge
      Week One Done
    • First Post
      Yianis earned a badge
      First Post
    • Rookie
      GTRoberts went up a rank
      Rookie
    • First Post
      James courage Tabla earned a badge
      First Post
    • Reacting Well
      James courage Tabla earned a badge
      Reacting Well
  • Popular Contributors

    1. 1
      +primortal
      400
    2. 2
      +FloatingFatMan
      178
    3. 3
      snowy owl
      170
    4. 4
      ATLien_0
      167
    5. 5
      Xenon
      134
  • Tell a friend

    Love Neowin? Tell a friend!