• 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

    • Bethesda's Deathloop is free to claim on the Epic Games Store this week by Pulasthi Ariyasinghe The Epic Games Store's Mega Sale promotion is in its final week, and that means mystery giveaways are soon ending too. For the finale, Epic Games has called in something big from Bethesda, with the Arkane-developed time loop adventure Deathloop coming in as the latest freebie for PC gamers. Alongside it, the indie adventure Ogu and the Secret Forest is also free. Developed by Arkane Lyon and published by Bethesda, Deathloop comes in touting an action-packed campaign involving plenty of time travel shenanigans. The game is set on the mysterious island of Blackreef, where two rival assassins, Colt and Julianna, are trapped in a time loop. The player, as Colt, has to figure out how to eliminate eight targets within a single day to escape the loop. Each of the assassinations can be taken care of in many ways, including stealth, traps, accidents, or simply going in guns blazing. Aside from gunplay, the title also makes use of supernatural systems very similar to the studio's Dishonored franchise, letting players teleport, go invisible, use telekinesis, and more. There is a multiplayer twist here too, where players, as Julianna, can invade the campaigns of others to take the role of the rival assassin, flipping the tables on the main character and his plans. As for Ogu and the Secret Forest, this is a 2024-released indie adventure featuring hand-drawn characters and various types of puzzles. The 2D game involves befriending characters across a fantasy land as baby Ogu, with plenty of exploration, puzzle solving, and boss battles available. The Deathloop and Ogu and the Secret Forest giveaways are now live on the Epic Games Store for all PC gamers. The promotion is slated to last until June 12, giving you seven days to claim a copy for your library permanently. While the summer mystery giveaways are ending, regular freebies will continue to arrive from the Epic Games Store. When this one comes to an end on Thursday, the SEGA-published humorous hospital simulation entry Two Point Hospital is incoming as the next giveaway.
    • If graphics is all that matters to you go watch a Pixar movie or something, you sound like an absolute baby right now. The game looks great.
    • For sure!! My take is... If the dev choses to use Apple's payment services, sure, Apple can take a cut. Apple disallowing 3rd party payment processing directly within an app, kind of a gray area, its anti-competitive sure, but I can at least see arguments on both sides. Apple blocking or attempting to take a cut from weblinks is pure evil, there is no possible justification at all. Heck, if we take this to the logical next step, Apple should start demanding a cut of retail transactions if the user used GPS directions on an iPhone to get to the store.
  • 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
      407
    2. 2
      +FloatingFatMan
      181
    3. 3
      snowy owl
      176
    4. 4
      ATLien_0
      170
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!