473,804 Members | 3,153 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring a variable size array

22 New Member
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(Numbe r)) 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 7372
Killer42
8,435 Recognized Expert Expert
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
frankleggett
22 New Member
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 Recognized Expert Expert
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
frankleggett
22 New Member
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 Recognized Expert Expert
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
4058
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 enough to convert a favorite PHP script to work on an ASP.NET site. I'm experimenting with simple example scripts that will help me learn how to implement each "piece" of the puzzle.
2
2488
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 char sampleArray[ constArraySize; for both of the above, i get the same error message when i compile:
6
2073
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
2188
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 approach proper here or is the a better way for setting the instance of this specific format for an array. -- mark
3
2232
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
10172
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". */ unsigned short int array_size = 25; /*Declare an array named "numbers" using the variable initialized above. */ unsigned short int numbers;
12
3891
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 looked sensible, but it didn't work right. Here is a simple example of what I'm trying to accomplish: // I have a hardware peripheral that I'm trying to access // that has two ports. Each port has 10 sequential // registers. Create a...
3
2762
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 matrix into my array of lists then use ./matrix find 1 3 to do something to the matrix I have just made (which is an array of lists) i was going to create a global variable which was an array of lists but i need to know the size of it to...
5
1196
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
3467
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 the second uses pointer notation. But apart from that what are the implications / dangers, etc. if any.
0
9708
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9587
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10588
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4302
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3827
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.