473,396 Members | 1,942 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Declaring a variable size array

Hi

Please excuse my ignorance. I am trying to declare an array with a variable amount but I'm having no luck.
this is what I've tried

Dim Number as string
X = Val(Number)
dim Array(X) as string

I've also tried

dim Array(val(Number)) as string

but with both I get a compile error "constant expression required"

Can someone help me. I have tried looking up all my reference books and on the Internet.

Frank
Dec 15 '06 #1
5 7355
Killer42
8,435 Expert 8TB
Please excuse my ignorance.
If it weren't for ignorance, we'd have nothing to do here, would we? :)

I am trying to declare an array with a variable amount but I'm having no luck. this is what I've tried
Expand|Select|Wrap|Line Numbers
  1. Dim Number as string
  2. X = Val(Number)
  3. dim Array(X) as string
I've also tried
Expand|Select|Wrap|Line Numbers
  1. dim Array(val(Number)) as string
but with both I get a compile error "constant expression required"

Can someone help me. I have tried looking up all my reference books and on the Internet.
For starters, I think it depends on where you define the array. If you are defining it inside a Sub or Function, then I think (not completely certain) that it has to be a specific size. For a variable-sized array, try defining it at the module level (as Public if desired).

Also, I think (once again, not 100% certain) that you have to define it without any size, then use the ReDim statement in your code to set the size. For example:
Expand|Select|Wrap|Line Numbers
  1. ' At the module level...
  2. Private MyArray() As String
  3.  
  4. ' In a Sub or Function...
  5. ReDim MyArray(1 To X)
  6.  
One important thing to remember - you can change the size of the array later by using ReDim again, but by default it will wipe out all values in the array at the time. Use Redim Preserve to keep existing contents while changing the size.
Dec 15 '06 #2
Thanks for your reply I will try it in a module. I have tried to set up variables in a module before but every time I've tried when the variable is called the error comes back variable unknown this maybe because I'm not tying the module to the form in some way or I'm declaring the variable in the module properly, I'm not sure but I will keep trying until I run out of ideas.

Thanks again.
Frank
Dec 15 '06 #3
Killer42
8,435 Expert 8TB
Thanks for your reply I will try it in a module. I have tried to set up variables in a module before but every time I've tried when the variable is called the error comes back variable unknown this maybe because I'm not tying the module to the form in some way or I'm declaring the variable in the module properly, I'm not sure but I will keep trying until I run out of ideas.
That's the spirit.

Note though, I'm pretty sure you can do it in a form. You would declare it at the form level. In other words, at the top of the code before any Sub or Function declarations. Then in your code (perhaps the Form_Load event?) you would set it to the size you want using ReDim.

Anyway, as you said, have a play with it and see where it goes.
Dec 16 '06 #4
Thanks alot

I tried as you said to declare the array at the top of the form under Option Explicit and then reDim inside the sub I needed it for and it worked.

Frank
Dec 16 '06 #5
Killer42
8,435 Expert 8TB
I tried as you said to declare the array at the top of the form under Option Explicit and then reDim inside the sub I needed it for and it worked.
That's great news! Have fun...
Dec 16 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

29
by: Friday | last post by:
Sorry if this is the wrong group. I tried to find the one I thought would be most relevant. I'm an old PHP guy, who knows little about asp and NOTHING about asp.net, but need to learn at least...
2
by: Jim Hudon | last post by:
i need to create an array of a size determined by a non-const variable: int char sampleArray; why does the following not work, and what can i do: const int constArraySize = arraySize; int...
6
by: JNY | last post by:
Hello, Is it possible to declare an array with variable indeces? i.e. int x = 4; int myArray; for (j = 0;j < 5;j++) {
3
by: mark | last post by:
When I declare an array as double(,) then try to use it I get an error: "Object reference not set to an instance of an object." I have found that I can redim the array and all is well. Is my...
3
by: farseer | last post by:
i am getting "error C2057: expected constant expression" with the following code: ifstream f( argv ); f.seekg( 0, ios::end ); const long fSize = f.tellg(); f.close(); char content;
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
12
by: gcary | last post by:
I am having trouble figuring out how to declare a pointer to an array of structures and initializing the pointer with a value. I've looked at older posts in this group, and tried a solution that...
3
by: dn6326 | last post by:
hi, essentially, my program needs to store an array of lists so that in main() it can have 2 run modes from the command line. i.e. i can run the program with ./matrix load filename to load a...
5
by: miladhatam | last post by:
how can i declare a varible dynamically like this : for (i=1 ; i<10;i++){ int ("j" + i) = i ; //declaring j1 - j9 } ofcourse this code is wrong and it is an algorithm
8
by: bintom | last post by:
What are the differences between the following methods of declaring strings? char string1 = "C++ forum"; char* string2 = "C++ forum"; I know that the first uses the array notation, whereas...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.