Solved

Array() function

Posted on 2000-04-17
10
160 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 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

691 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