• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 167
  • Last Modified:

Array() function

I need to initialise several arrays, each of about 100 doubles.  I have tried to use the Array() function to do this.  E.g:

Dim v As Variant
Dim i As Integer

v = Array(0.00000154414016, 0.00000313028629, 0.00000620856271, 0.00001118206384, 0.00001883176701, ... etc.
i = 0
Do While i <= UBound(v)
    mdblCoefs_10_1(i) = CDbl(v(i))
    i = i + 1
Loop

However, entering the v = Array(...) line (which can get very long so I use the continuation character, _) causes VB6 (SP3)  to perform an invalid page fault.

Is there a limit to the number of characters on any one line?  VB has warned me that there is a limit of the number of continuation characters (_) that can be used on one line - although I can't remember what it is.

Is there a limit on the number of elements that the Array() function can handle?  -  I never get a waring message.

Is there a limitation caused by using a Variant type?
0
Gatesy
Asked:
Gatesy
  • 3
  • 3
  • 2
  • +2
1 Solution
 
AzraSoundCommented:
why dont you declare v like this:

Dim v() As Double
0
 
detiegeCommented:
I think that your problem is the Variant Type:

Variant data type
A special data type that can contain numeric, string, or date data as well as user-defined types and the special values Empty and Null. The Variant data type has a numeric storage size of 16 bytes and can contain data up to the range of a Decimal, or a character storage size of 22 bytes (plus string length), and can store any character text. The VarType function defines how the data in a Variant is treated. All variables become Variant data types if not explicitly declared as some other data type.
0
 
caraf_gCommented:
Since you're doing...
Do While i <= UBound(v)
    mdblCoefs_10_1(i) = CDbl(v(i))
    i = i + 1
Loop

....anyway, you may as well do (similar to what AzraSound says)
Dim mdblCoefs_10_1(0 to somevalue) As Double

mdblCoefs(0) = 0.00000154414016
mdblCoefs(1) = 0.00000313028629
etc...

The amount of code you have to write really shouldn't matter, it avoids the use of any continuation characters, and probably is much faster too.

3-way win situation.

A perfect workaround for your problem. Probably worth another "B" or "C" in your eyes, I'd guess. I'd prefer it if you didn't accept this comment at all in that case. Thanks.

Further to your question; some of the stuff below is conjecture:

1 - no there is no practical limit to the number of elements. Theoretically you should be able to assign at least 2GB (the range of a Long) values. Practically, of course, you'll run into trouble with memory and disk space long before then. That is for an ordinarry array declaration. My guess is that the same would be true for the Array keyword.

2 - If you're getting an invalid page fault, perhaps you did indeed hit some undocumented max number of characters on a line. I'm not aware of an "official" value for this.

3 - The number of line continuations should not cause an invalid page fault. The worse that should happen is a user-friendly IDE message saying "Too many line continuations"

4 - There should be no problem at all with the variant data type, apart from it being slow and cumbersome.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
AzraSoundCommented:
i think it might be best to store all those values in a text file. each value on a separate line.  then you can read the values into the array as needed:


Dim v(0 To 99) As Double  '100 values
Dim filenum As String 'file number
Dim strline As String
Dim i As Integer


Private Sub Form_Load()
    filenum = FreeFile
    Open "C:\somepath\values.txt" For Input As filenum
    Do While Not EOF(filenum)
        Line Input #filenum, strline
        v(i) = strline
        i = i + 1
    Loop
    Close filenum
End Sub

0
 
GatesyAuthor Commented:
I have just tried declaring v() as a double, but the problem remains.  Presumably the Array() function creates a variant before storing it in the double array.
0
 
GatesyAuthor Commented:
I have just tried declaring v() as a double, but the problem remains.  Presumably the Array() function creates a variant before storing it in the double array.
0
 
JGanzCommented:
I agree with AzraSound to declare an array of doubles first, but then you should first determine the number of elements to fill into the array and redim the array according to the number of elements, eg:

number of elements = n,
redim v(1 to n) or redim v(n - 1), depending of your array-base

now you could fill the single elements of that array in a loop.
0
 
caraf_gCommented:
So don't use the Array() function then.
0
 
GatesyAuthor Commented:
It did actually occur to me to do:
mdblCoefs(0) = 0.00000154414016
mdblCoefs(1) = 0.00000313028629
etc.

I thought there may have been a neater way - It appears that there isn't.

By the way I don't want to read it in from a file - I would much rather have it encapsulated in the code.

Anyway, here's your precious A grade.  Well done.
0
 
caraf_gCommented:
Thanks!
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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