Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 968
  • Last Modified:

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
0
alwaysdead
Asked:
alwaysdead
  • 3
  • 2
  • 2
1 Solution
 
PePiCommented:
check your spelling ...

ReDim vaildMacAddresses(numbOfMacAddresses)

should be ReDim validMacAddressess
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
0
 
PePiCommented:
actually, all most of the array name in the Sub is vaildMacAddressess... change them all to validMacAddresses
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
PePiCommented:
beat Idle_mind to it by a minute ;-p
0
 
alwaysdeadAuthor Commented:
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?  
0
 
alwaysdeadAuthor Commented:
upon checking the rules... it is first comment that gets the points.  

(http://www.experts-exchange.com/help.jsp#hs5)
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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!...."
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now