473,397 Members | 1,985 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,397 software developers and data experts.

a constant array

Hi Folk

Is it possible to declare a constant array

e.g.
dim i() as string
i(1) = ...
i(2) = ...
seems to work but the following does not
const i(1) = ....
const i(2) = .....
It is nice to put them into an array, because that means you can loop
through it.

Cheers

Nicolas
Nov 13 '05 #1
9 6469
rkc
xtra wrote:
Hi Folk

Is it possible to declare a constant array

e.g.
dim i() as string
i(1) = ...
i(2) = ...
seems to work but the following does not
const i(1) = ....
const i(2) = .....
It is nice to put them into an array, because that means you can loop
through it.


You can declare a contstant, set a variable to the value of
the constant and then put it in an array, but a constant can
not be assigned using a variable. If it could it wouldn't be
a constant, would it?



Nov 13 '05 #2

"rkc" <rk*@rochester.yabba.dabba.do.rr.bomb> wrote in message
news:7s*****************@twister.nyroc.rr.com...
xtra wrote:
Hi Folk

Is it possible to declare a constant array

e.g.
dim i() as string
i(1) = ...
i(2) = ...
seems to work but the following does not
const i(1) = ....
const i(2) = .....
It is nice to put them into an array, because that means you can loop
through it.


You can declare a contstant, set a variable to the value of
the constant and then put it in an array, but a constant can
not be assigned using a variable. If it could it wouldn't be
a constant, would it?

Thank you for your reply.

I only ever want to assign one value to the constant, I just wanted them to
be in an array, so that you can easily cycle through them, e.g.

for i = 0 to arraysize
x = 5 * constitem(i)
..
..
next i

or is there a different way to achieve the same?

Cheers
Nicolaas
Nov 13 '05 #3
xtra,
Nope. Declare the array then populate it. If you want it to behave like a
constant, then hard code your values in the procedure or function that
declares it and populates it. Usual caveats about scope apply.
--
Alan Webb
kn*******@SPAMhotmail.com
"It's not IT, it's IS
"xtra" <wi**********@hottermail.com> wrote in message
news:Bg*******************@news.xtra.co.nz...
Hi Folk

Is it possible to declare a constant array

e.g.
dim i() as string
i(1) = ...
i(2) = ...
seems to work but the following does not
const i(1) = ....
const i(2) = .....
It is nice to put them into an array, because that means you can loop
through it.

Cheers

Nicolas

Nov 13 '05 #4
rkc
xtra wrote:
I only ever want to assign one value to the constant, I just wanted them to
be in an array, so that you can easily cycle through them, e.g.

for i = 0 to arraysize
x = 5 * constitem(i)
..
..
next i

or is there a different way to achieve the same?


Why are you looking for a different way? What problem do
you see with building an array with the values you need?

If you're looking for a way to be sure the values in the
array can not be changed once they are set, then write
a class that
1) handles the initialization of the array internally
2) provides an interface to iterate the array
3) does not provide a way to change the values in the array
Nov 13 '05 #5
No, but you can do this

Dim x As Variant
Dim intX As Integer

x = Array(1, 2, 3, 4, 5, 6, 7, 8, 9)

For intX = LBound(x) To UBound(x)
Debug.Print x(intX)
Next

--
Terry Kreft
MVP Microsoft Access
"xtra" <wi**********@hottermail.com> wrote in message
news:Bg*******************@news.xtra.co.nz...
Hi Folk

Is it possible to declare a constant array

e.g.
dim i() as string
i(1) = ...
i(2) = ...
seems to work but the following does not
const i(1) = ....
const i(2) = .....
It is nice to put them into an array, because that means you can loop
through it.

Cheers

Nicolas

Nov 13 '05 #6
As others have pointed out, VB doesn't have constant arrays. What you can do,
though, is have a function that returns a safe copy of an array (or
collection, or any other thing) each time it's called, so even if code that
uses it changes the content, it will never affect what the next caller gets
when they call the same function.

Of course, if you copy an object (such as a collection), you have to make sure
you really cloned it, and did not just pass another reference to the same
object.

Public Function MyArrayValues() As String()
Static sblnInitialized As Boolean
Static sastrResult(1 to 5) As String

If Not sblnInitialized Then
sastrResult(1) = "A"
sastrResult(2) = "B"
sastrResult(3) = "C"
sastrResult(4) = "D"
sastrResult(5) = "E"
sblnInitialized = True
End If

MyArrayValues = sastrResult
End Function

Public Sub TestArray()
Dim varItem As Variant

For Each varItem In MyArrayValues
Debug.Print varItem
Next

End Sub

On Sun, 8 May 2005 13:50:46 +1200, "xtra" <wi**********@hottermail.com> wrote:
Hi Folk

Is it possible to declare a constant array

e.g.
dim i() as string
i(1) = ...
i(2) = ...
seems to work but the following does not
const i(1) = ....
const i(2) = .....
It is nice to put them into an array, because that means you can loop
through it.

Cheers

Nicolas


Nov 13 '05 #7

"xtra" <wi**********@hottermail.com> wrote in

Thank you guys.... As I understand it, there are lots of ways to solve it,
but Constant Arrays do not actually exist as such.

Cheers

- Nicolaas
Nov 13 '05 #8
To be fair you can create constant arrays but it's a bit black belt and I've
certainly never had a need to do it.

--
Terry Kreft
MVP Microsoft Access
"xtra" <wi**********@hottermail.com> wrote in message
news:_r****************@news.xtra.co.nz...

"xtra" <wi**********@hottermail.com> wrote in

Thank you guys.... As I understand it, there are lots of ways to solve it, but Constant Arrays do not actually exist as such.

Cheers

- Nicolaas

Nov 13 '05 #9

"Terry Kreft" <te*********@mps.co.uk> wrote in message
news:UN********************@karoo.co.uk...
To be fair you can create constant arrays but it's a bit black belt and I've certainly never had a need to do it.

--
Terry Kreft
MVP Microsoft Access
"xtra" <wi**********@hottermail.com> wrote in message
news:_r****************@news.xtra.co.nz...

"xtra" <wi**********@hottermail.com> wrote in

Thank you guys.... As I understand it, there are lots of ways to solve

it,
but Constant Arrays do not actually exist as such.

Cheers

- Nicolaas



Yes, that is what I got, difficult to do, so why bother.
Nov 13 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: richbl | last post by:
Hello all, I have a question about unserializing a single array element from a serialized array. Can this be done, or must I first unserialize the array, and then access the element? For...
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
2
by: JJ Feminella | last post by:
This statement is legal C#: public const string day = "Sunday"; but this statement is not: public const string days = new string { "Sun", "Mon", "Tue" }; The C# Programmer's Reference...
7
by: icosahedron | last post by:
Is there a way to determine if a parameter to a function is a constant (e.g. 2.0f) versus a variable? Is there some way to determine if this is the case? (Say some metaprogramming tip or type...
1
by: Andrzej 'Foxy' D. | last post by:
Hi! I cannot figure out, why is the following code incorrect: const int array = { 1 }; const int var = 1; template<int I> struct Blah {};
25
by: galapogos | last post by:
Hi, I'm trying to compare an array of unsigned chars(basically just data without any context) with a constant, and I'm not sure how to do that. Say my array is array and I want to compare it with...
13
by: hn.ft.pris | last post by:
Hi: I have the following simple program: #include<iostream> using namespace std; int main(int argc, char* argv){ const double L = 1.234; const int T = static_cast<const int>(L); int arr;
15
by: skibud2 | last post by:
Consider this example: u8 my_array; for (i = 0; i < (sizeof(my_array)/sizeof(u8)); i++) { ... } In the standard C specification, is the evaluation of '(sizeof(my_array)/sizeof(u8))' to 10...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.