?
Solved

Array() function

Posted on 2000-04-17
10
Medium Priority
?
162 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
Industry Leaders: 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!

 
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
 

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

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

764 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