473,467 Members | 1,565 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Want a const array...

Hi,
I want to declare an array of constants (of type byte) and initialize the
array ...all at one shot. How do I do that?

Sinex
Nov 17 '05 #1
3 1123
Sinex,

There is no such thing as an "array of constants". A normal array
contains values that can be changed.

To define a normal array and assign it values, use the syntax:

byte[] myArray = {1, 2, 3, 100, 205, etc.}

Best Regards
Johann Blake

Nov 17 '05 #2
Afaik there is no direct way how to do this
you can declare for expample

public readonly byte[] arr = new byte[3] { 0x01, 0x02, 0x03 };

However, array elements aren't readonly.

Another way is to use ArrauyList

private static byte[] arr = new byte[3] { 0x01, 0x02, 0x03 };
public readonly ArrayList al = ArrayList.ReadOnly(ArrayList.Adapter(arr));

al is readonly collection. The minus of such a collection is that if you
will try to set the value of it you will get runtime exception and not the
error in the compile-time

--
Vadym Stetsyak aka Vadmyst

"Sinex" <ma************@honeywell.com> wrote in message
news:#V**************@TK2MSFTNGP10.phx.gbl...
Hi,
I want to declare an array of constants (of type byte) and initialize the array ...all at one shot. How do I do that?

Sinex

Nov 17 '05 #3
Sinex wrote:
I want to declare an array of constants (of type byte) and
initialize the array ...all at one shot. How do I do that?


As mentioned, there's no convenient built-in way to do this, but what you
can do is declare a simple class that wraps an array and only exposes the
ability to read it, like this (warning, code is untested):

class ConstArray {
byte[] data;

public ConstArray(byte[] data) {
this.data = data;
}

public byte this [int index] {
get
{
return data[index];
}
}
}

Now you can declare in your class:

public readonly ConstArray arr = new ConstArray(new byte[3] {0x01, 0x02,
0x03});

Now the compiler will enforce that neither the array nor its elements can be
changed. If possible you may want to make ConstArray a value type to avoid
unneccessary indirection.
--
Derrick Coetzee, MCP, MSFT (Speech Server)
This posting is provided "AS IS" with no warranties, and confers no
rights. Use of included code samples are subject to the terms
specified at http://www.microsoft.com/info/cpyright.htm
Nov 17 '05 #4

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

Similar topics

19
by: Thomas Matthews | last post by:
Hi, Given a structure of pointers: struct Example_Struct { unsigned char * ptr_buffer; unsigned int * ptr_numbers; }; And a function that will accept the structure:
4
by: cppsks | last post by:
I have been working on making a constant array available for the clients. However, it is placing it in the text segment, and external references result in unresolved references to the array. Taking...
6
by: Dylan Nicholson | last post by:
Is there any way of declaring the parameter "array" below to be const, so that the function as written will not compile: void foo(int array /*const*/) { array = array; // should not be allowed...
7
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
18
by: DaveLessnau | last post by:
I'm trying to learn C on my own and, apparently, my brain went on vacation somewhere. I just can't figure out how to parse the following function call: "void fillDeck( Card * const wDeck, const...
8
by: Roger Leigh | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 A lot of functions use const pointer arguments. If I have a non-const pointer, it is transparently made const when I pass it to the function, e.g....
5
by: Milan Cvetkovic | last post by:
Hi, Recently I came accross a weird bug in my code which turned to be my missunderstanding of arrays in C++. It all boils down to the small example: template<class ITER> const char* gggg...
3
by: Jess | last post by:
Hello, If I have a constant array, i.e. it's elements aren't changed, should I declare it as: const int a = {1,2,3}; or int const a = {1,2,3}
12
by: hweekuan | last post by:
hi, it seems i can't assign the const variable u in class A, one way to solve the problem may be to build a copy constructor. however, why does C++ or vector class not like this code? my g++ is:...
10
by: Stephen Howe | last post by:
Hi Just going over some grey areas in my knowledge in C++: 1) If I have const int SomeConst = 1; in a header file, it is global, and it is included in multiple translations units, but it...
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
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
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...
1
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...
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.