Link to home
Start Free TrialLog in
Avatar of alwaysdead
alwaysdead

asked on

vb 6.0 arrays, redim and ubounds throws error

I have some vb6.0 code.  If I use redim, ubounds seems to fail (where using just dim with a specified number works).  Also, I tried dim and redim, with dim(0), and then redim(x); ubounds still returned 0.  

The error I get is subscript out of range (9).  This happens on the first Ubound in the for loop.  Please don't suggest avoiding this with error handling, as I want Ubound to work.  

Option Explicit

Private validMacAddresses() As String
Private numbOfMacAddresses As Integer

Private Sub Class_Initialize()
    'user defined!!
    Const numbOfMacAddresses = 1  'real numb, not counting zero!
    ReDim vaildMacAddresses(numbOfMacAddresses)
    vaildMacAddresses(0) = "11-11-11-11-11-11"
    'validMacAddresses(0) = "00-06-1B-C6-77-F4"
    'validMacAddresses(0) = "00-05-4E-42-70-26"
   
    If (MsgBox(IsArray(vaildMacAddresses), vbYesNo, "index") = vbNo) Then
    End If
    Dim xx As Integer    'ensure all upper case
    For xx = 0 To (UBound(validMacAddresses) - 1)

        If (MsgBox(UBound(validMacAddresses), vbYesNo, "index") = vbNo) Then
        End If
        vaildMacAddresses(xx) = UCase(vaildMacAddresses(xx))
        vaildMacAddresses(xx) = Replace(vaildMacAddresses(xx), "-", "")
        vaildMacAddresses(xx) = Replace(vaildMacAddresses(xx), " ", "")
        If Len(validMacAddresses(0)) <> 12 Then
            validMacAddresses(xx) = "Bad Mac Address"
        End If
    Next
End Sub
ASKER CERTIFIED SOLUTION
Avatar of PePi
PePi

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mike Tomlinson
The problem is right here:

    ReDim vaildMacAddresses(numbOfMacAddresses)

You have the variable mispelled!  Here is a big Caution statement from the bottom of the help file on "ReDim":

    "Caution   The ReDim statement acts as a declarative statement if the variable it declares doesn't exist atmodule level orprocedure level. If another variable with the same name is created later, even in a widerscope, ReDim will refer to the later variable and won't necessarily cause a compilation error, even if Option Explicit is in effect. To avoid such conflicts, ReDim should not be used as a declarative statement, but simply for redimensioning arrays."

So what is happening is the ReDim statement is creating a new local variable called "vaildMacAddresses", NOT "validMacAddresses".  Consequently, further in the code when you attempt to get the Ubound() of "validMacAddresses" it fails because it was never dimensioned because of the spelling error.

Fix all of the variable names and it should be fine.

Also, I believe you have a logic error here:

        If Len(validMacAddresses(0)) <> 12 Then
            validMacAddresses(xx) = "Bad Mac Address"
        End If

I'm guessing the "validMacAddresses(0)" should instead be "validMacAddresses(xx)"

~IM
Avatar of PePi
PePi

actually, all most of the array name in the Sub is vaildMacAddressess... change them all to validMacAddresses
beat Idle_mind to it by a minute ;-p
Avatar of alwaysdead

ASKER

I feel like an idiot... some much for option explicit helping on this one... (leaving amount of time I stared at that undisclosed...)

That is a good point on the time pepi... 1 minute apart... is it a split points since they were so close or a first come, first servered kind of thing?  
upon checking the rules... it is first comment that gets the points.  

(https://www.experts-exchange.com/help.jsp#hs5)
Lol@Pepi...

>> beat Idle_mind to it by a minute ;-p

Have you been waiting a long time to "beat" me at a question?   =)

I feel like I should be laughing maniacally as I disappear off into the darkness.

*muhahahaha*

"You haven't seen the last of me!...."