Solved

VBSCRIPT - dim and redim?

Posted on 2006-06-29
12
1,629 Views
Last Modified: 2012-05-05
Hi

I try to do this to keep an array with 'erroneous website names'

Dim erroneousWebsites(0)

While ...

ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))
    erroneousWebsites(UBound(erroneousWebsites)) = Trim(CStr(website_rows.fields("website").value))


Wend

but I get

Syntax error - type mismatch on the line

ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))
0
Comment
Question by:paulwhelan
  • 3
  • 2
  • 2
  • +1
12 Comments
 
LVL 43

Expert Comment

by:TimCottee
ID: 17008046
Hi paulwhelan,

You have to initially Dim the array with no elements:

Dim erroneousWebsites()

If you specify bounds in the initial declaration then it is fixed length and cannot be redimensioned.

Tim Cottee
0
 

Author Comment

by:paulwhelan
ID: 17008373
Hey!

I still get the same error even with


Dim erroneousWebsites()

Thanks
0
 
LVL 43

Expert Comment

by:TimCottee
ID: 17008402
paulwhelan,

Ok, that is probably because when it is declared initially it has no length and therefore UBound() is meaningless.
Try

If Len(Join(erroneousWebsites)) = 0 Then
    ReDim Preserve erroneousWebsites(1)
Else
    ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))
End If

To avoid this.


Tim
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Comment

by:paulwhelan
ID: 17008470
Thanks Tim

That gives me an error at

ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))

---------------

If Len(Join(erroneousWebsites)) = 0 Then
      ReDim Preserve erroneousWebsites(1)
    Else
      ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))
    End If
0
 
LVL 5

Expert Comment

by:lunchbyte
ID: 17011165
Here is what I do


Dim erroneousWebsites()

Then before I use it add more it it, I set it to zero and just ignore index zero.

redim
Dim erroneousWebsites(0)

YOUR LOOP or whatever
   ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))

   erroneousWebsites(UBound(erroneousWebsites)) = "YOUR VALUE"

END LOOP
0
 
LVL 13

Accepted Solution

by:
jmundsack earned 20 total points
ID: 17011602
The +1 is in the wrong spot.  Move it outside the paren:

ReDim Preserve erroneousWebsites(UBound(erroneousWebsites) + 1)
0
 
LVL 5

Expert Comment

by:lunchbyte
ID: 17011642
LOL. I made the same mistake. That damn copy and paste.
0
 
LVL 5

Expert Comment

by:lunchbyte
ID: 17011651
CORRECT ONE

Dim erroneousWebsites()

Then before I use it add more it it, I set it to zero and just ignore index zero.

redim
Dim erroneousWebsites(0)

YOUR LOOP or whatever
   ReDim Preserve erroneousWebsites(UBound(erroneousWebsites) + 1)

   erroneousWebsites(UBound(erroneousWebsites)) = "YOUR VALUE"

END LOOP
0
 
LVL 13

Expert Comment

by:jmundsack
ID: 17294823
The reason for the error in the ORIGINAL QUESTION...

"Syntax error - type mismatch on the line
ReDim Preserve erroneousWebsites(UBound(erroneousWebsites + 1))"

...is because the + 1 is inside the parens, and I was the first to point this out.

TimCottee is also correct that if you want to ReDim you need to Dim without bounds initially.

But for 20 points it's hardly worth fighting about LOL.  Flip a coin?
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question