Solved

Array() function

Posted on 2000-04-17
10
153 Views
Last Modified: 2010-05-18
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
Comment
Question by:Gatesy
  • 3
  • 3
  • 2
  • +2
10 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2721929
why dont you declare v like this:

Dim v() As Double
0
 
LVL 1

Expert Comment

by:detiege
ID: 2721958
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
 
LVL 10

Accepted Solution

by:
caraf_g earned 100 total points
ID: 2722007
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
 
LVL 28

Expert Comment

by:AzraSound
ID: 2722015
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
 

Author Comment

by:Gatesy
ID: 2722016
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:Gatesy
ID: 2722033
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
 

Expert Comment

by:JGanz
ID: 2722043
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2722077
So don't use the Array() function then.
0
 

Author Comment

by:Gatesy
ID: 2722078
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
 
LVL 10

Expert Comment

by:caraf_g
ID: 2722090
Thanks!
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…

707 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now