• 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

    • The long-awaited Nothing Phone (3) is finally coming next month, launch date confirmed by Aditya Tiwari London-based consumer electronics brand Nothing is due to launch its latest flagship in 2025. The company dropped a new teaser for the Phone (3), revealing when the flagship device will be out on the market. Nothing Phone (3) will be unveiled during a live event on July 1 at 1:00 PM ET / 10:00 AM PT / 6:00 PM BST / 11:30 PM IST. It has already created a live event titled "Come to Play" on its official YouTube channel, for which you can add a reminder by clicking on the "Notify Me" button. Nothing CEO Carl Pei has previously dropped several details about the unreleased smartphone. He took part in a social media AMA earlier this year and said Nothing Phone (3) will arrive in the third quarter of 2025. Pei confirmed that Nothing Phone (3) will make its way to the US this time after a dry spell since 2023. However, he didn't specify whether the device will be sold directly or through the beta channel, which currently includes Phone (3a), Phone (3a) Pro, and CMF Phone 2 Pro. The US has been a rocky terrain for Nothing. Its first smartphone was made available through the beta channel, and the Nothing Phone (2) is the only smartphone from the company that has been widely available in the US. The Glyph interface featured on the back of Nothing smartphones has remained a differentiating factor from the start. However, the smartphone maker recently posted a 9-second video in which the Glyph lights on the back of a Nothing smartphone abruptly turn off. "We killed the Glyph Interface," the company said. It makes sense when you check out the Phone (3) teaser and see dot matrix-style lights being flashed in a pattern. Nothing released another teaser about a week ago, featuring the number 3 lit up as dot matrix LEDs. If you're looking for some trivia, Nothing product manager Raymond Zhu estimated in a Q&A video that the company would need to sell about 250,000 Phone (3) units to turn a profit. Answering another question, he added that their biggest weakness is "no one knows us", and the company is struggling to reach the masses without high marketing budgets. Let's wait to see what Nothing has in store for Phone (3) next month other than the new physical button. Speaking of the future of smartphones, the Nothing CEO believes that our entire software experience will eventually be condensed down to just one app.
    • As far as I can remember, no one has done a 4v4 before. 2v2? yes. 3v3? yes.
    • I just checked with Brave portable, the shield is disabled, as well as uBlock Origin Lite, no change to the Host file, and no pop-up when I connect to the site 😵! I think this might be the best option? I don't have a Paypal account, because I got rid of it, and I was fed up with all the phishing attempts, so I use my credit card. Do you take credit card payments? Because so far it's been safer, no phishing attempts.    
    • *facepalm* Good sir... holy smokes. ...anyways I assume Qanba and the rest will also produce options in the near future.
  • 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
      403
    2. 2
      +FloatingFatMan
      179
    3. 3
      snowy owl
      174
    4. 4
      ATLien_0
      170
    5. 5
      Xenon
      135
  • Tell a friend

    Love Neowin? Tell a friend!