Skip to main content

Creating a number of variables

Is there a way to let the user define a number and than the script creates that number of variables (dim as string)? I thought i would be something likes this:
 
Option Explicit
Dim LimitQ As String
Dim i As Integer
LimitQ = 5
For i =0 To LimitQ
Dim Limit(i) As String
Next i
Sub main
End Sub
 
 
 

Brian Element Tue, 08/28/2018 - 07:05

Hi Robert, you would use the for next loop to set the variables.  If you just want to define them the replace the for next loop with:

Dim Limit(LimitQ - 1) as string 'arrays start at 0 so if you define one as 5 you have 6 elements in the array.

Robert van den… Tue, 08/28/2018 - 09:04

In reply to by Brian Element

if the code is set as follows: Dim Limit(LimitQ - 1) As String  i get an error and the word "LimitQ" gets selected in the script. But do i need so set it as an array? Because i only want to set the variables.
 
What i really want is that the line task.AddUpperLimit from the stratification function gets set with the input of the user. I tried the following code:
For Count = 1 To LimitQtask.AddUpperLimit Val(LimitQ) * Val(LimitSize)Next 
 
But then is get an error the Upper limit must be greater than the lower limit, but the lower limit is set to 0. It's really giving me a headache for not understanding what is going wrong.
 

Brian Element Tue, 08/28/2018 - 10:17

In reply to by Robert van den…

The second part of your problem is you are using a for next loop but your arrays are using a static variable.  You need to change the LimitQ and LimitSize for the Count so that the first time you go through the loop it would be task.AddUpperLimit Val(1) * Val(1), the second time it would be task.AddUpperLimit Val(2) * Val(2), and so on, right now it never changes and that is why you are getting the error.  Also if your first item in your is item 0 then you need to change the for loop to For Count = 0 to LimitQ

Robert van den… Thu, 08/30/2018 - 10:34

In reply to by Brian Element

Is there a book, guide or tutorial to get familiar with arrays and defining variables with a loop function?
 
I thought this might do the trick, but no:
 
For i = 1 To LimitQ
Dim Limit() As Integer
Next
For i = 1 To LimitQ
Limit = Val(LimitSize) * Limit() 
Next
MsgBox "" & Val(LimitQ) * Val(LimitSize)
For i = 0 To UBound(Limit)
LimitTemp = LimitTemp & Limit(i) & " , "
Next i
MsgBox "" & LimitTemp

Robert van den… Fri, 08/31/2018 - 05:15

In reply to by Brian Element

I have been googling the entire morning, and i understand the visual basic a bit. But it seems that code doesn't work so well in IDEA and the python script is like greek language for me. 
 
What i'am trying to figure out is how to make this line of code depending on defined number (different variable) that later can be set by the user in a dialog.
Dim students(5) As Integer
 
This line creates the array, but how can i make that "5" is get from an other variable? 

Brian Element Fri, 08/31/2018 - 07:19

What you can do is first define the variable:

Dim students(0) As Integer

So you have created it but haven'd done anything with it.

You can then get the number of students through a dialog or inputbox:

value = InputBox("Enter Number of Students")

and then redim the array:

ReDim students(value - 1)

Now your array has been resized to hold the number of students.

Robert van den… Tue, 09/04/2018 - 05:36

In reply to by Brian Element

Hi, i got it working. But now when the limit size gets to big for some reason i don't understand (like 40.000) i get an error Overflow line 202 " The script works fine with 10.000/20.000 and 30.000.
Line 202 is "LimitSizeInput = dlg1.LimitSize" where the number is extracted from the dialog box. I don't understand why above certain numbers the script crashes.

Brian Element Tue, 09/04/2018 - 10:13

In reply to by Robert van den…

It is the LimitSizeInput is defined as an integer which has a size of around 32,000, so anything larger than that will give you an error.  To get around this change the variable from interger to long so that it can handle numbers up to around 2 billion.

The website encountered an unexpected error. Try again later.