When is it better to declare a dynamic array as a Variant verses declaring it as a Dynamic Array?

Here are two ways to declare my dynamic array.

Dim CandidateSentencesArray () As String

Dim CandidateSentencesArray () As Variant

I’m using the Variant method in my agent.

Dim CandidateSentencesArray () As Variant

This is how the dynamic array is used in the code.

delim = “.”

CandidateSentencesArray = Split(outputCandidateStrip, delim, -1)

It’s working the way I expect it. Would like to know if a declaring the dynamic array one way over the other is more efficient for proccessing?

Subject: When is it better to declare a dynamic array as a Variant verses declaring it as a Dynamic Array?

As written you’re declaring an uninitialized array of strings or an uninitialized array of variants…

the array of variants can hold data of multiple types…

but you could do:

dim sentences as variant

sentences = split(string, delim, -1)

and still get what you want.

maybe you knew that.

Subject: When is it better to declare a dynamic array as a Variant verses declaring it as a Dynamic Array?

Don,

Declaring it as a string is always going to be more efficient because you are going to use less memory to declare the array. Declaring a variant is the most high cost way to declare a variable like this.

My rule of thumb is to always declare my dynamic arrays to the datatype I am expecting. If you are using the Variant method because you won’t know the size of the value you are passing to the array, I would strongly suggest looking in the Designer documentation about Lists, which are really powerful and easy to resize.

I tend to reserve using variants in functions for when I am expecting to pass an object, usually something from OLE/COM (like an excel object) or when I know that a function can work on similar types of NotesObjects (like say a function that does something to a NotesViewEntryCollection OR a NotesDocumentCollection. I just test for the object type before I run the function.

just my 2 cents.

brandt

Subject: RE: When is it better to declare a dynamic array as a Variant verses declaring it as a Dynamic Array?

When it comes to declaration of variables, I would have to side with you, always declare what is closest to your needs.

…but, in this case the OP has a list of strings that is dynamic in nature.

The “normal” way of doing this would be to:

dim arrList() as string

redim arrList(0 to 3)

arrList(0) = “some value”

However, it is much more clear to do the other way, by declaring

dim arrList as variant

arrList = split(“somevalue, morevalues”)

In this case I believe the added benefit, memorywise, is so insignificant that I would always choose the second option just for the clarity of my code. Make your code easy to read and maintain first, and then optimize for speed and memory if (and only if) needed.

regards,

Bjorn