[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Array() function

Posted on 2000-04-17
10
Medium Priority
?
164 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

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

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
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 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…
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…
Suggested Courses

650 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