Author Topic: How to create a list of integers in VBScript?  (Read 4434 times)

Boron

  • EA User
  • **
  • Posts: 111
  • Karma: +6/-0
    • View Profile
How to create a list of integers in VBScript?
« on: October 16, 2019, 07:47:14 pm »
Hello all,
in an EA script (using EA 14.1) I would like to create and access :o a list of integers.
I want store the element IDs of a bunch of classes and objects in that list and do some checks on this.

What I did:
Code: [Select]
set listOfDs = CreateObject("System.Collections.ArrayList")
...
listOfIDs.add(connector.SupplierID)
Now I would like to access the elements in the list.
But unfortunately there is no listOfIDs.GetAt(<index>) method.
When trying to access an element with the [] operator I get an error message.
Code: [Select]
Session.Output listOfIDs[0]leads to the error message: "Expected end of statement".

Any idea how to access the element in the ArrayList?
Are there alternatives to the ArrayList (number of elements to be in the list is unknown, so dynamic resizing would be great)?

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to create a list of integers in VBScript?
« Reply #1 on: October 16, 2019, 07:53:29 pm »
use

Code: [Select]
listOfIDs(0)
which is (if I remember correctly) syntactic sugar for

Code: [Select]
listOfIDs.item(0)
Geert

Boron

  • EA User
  • **
  • Posts: 111
  • Karma: +6/-0
    • View Profile
Re: How to create a list of integers in VBScript?
« Reply #2 on: October 16, 2019, 08:11:18 pm »
Seems to work :). Thanks a lot.

But who in this world, designs a programming language that is using () instead of [] for index based accesses. This is insane!
I truly spent ~ 4 hour on this issue. Now I will go into the basement and smash something against the floor to soothe my rage >:(.

Geert Bellekens

  • EA Guru
  • *****
  • Posts: 13404
  • Karma: +567/-33
  • Make EA work for YOU!
    • View Profile
    • Enterprise Architect Consultant and Value Added Reseller
Re: How to create a list of integers in VBScript?
« Reply #3 on: October 16, 2019, 08:27:32 pm »
Seems to work :). Thanks a lot.

But who in this world, designs a programming language that is using () instead of [] for index based accesses. This is insane!
I truly spent ~ 4 hour on this issue. Now I will go into the basement and smash something against the floor to soothe my rage >:(.
I'm not sure about the chronology, but think thinks stems from the Basic syntax, which was around pretty early on (1964 according to Wikipedia).
It might have been the other, later, languages, who decided to start using [] instead.

VBscript has some more of these quirks, but yo can get used to it. After all it's just another syntax.

The weirdest one is the fact that you cannot use parentheses when calling a sub, or calling a function without using it's returntype

suppose I define a function like this that concatenates two strings

Code: [Select]
function myFunction(param1, param2)
    myFunction = param1 & param2
end function

When using the returntype I have to use this syntax:

Code: [Select]
dim result
result = myFunction("string1", "string2")

But if I'm not planning to use the result I have to call it like this:

Code: [Select]
myFunction "string1", "string2"
No parentheses, indeed. :o

Geert