473,396 Members | 1,860 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 const array

If I write:

int[] x = { 1 };

that is fine with the compiler, but if I try to declare the array as const,
meaning that it will not be changed:

const int[] x = { 1 };

I receive the error message:
error CS0623: Array initializers can only be used in a variable or field
initializer. Try using a new expression instead.

So now I try:
const int[] x = new int[1] { 1 };

and I get:
error CS0133: The expression being assigned to
'Sis.Messaging.BasicSendersReceivers.TestTcpHandle r.itest' must be constant
Nov 16 '05 #1
2 8440
Arrays are probably one of those things that can only be evaluated at
runtime. Constants must be evaluated at compile time. Try using "readonly"
instead of "const".
"Edward Diener" <di***************@ORsoftware.com> wrote in message
news:eT**************@TK2MSFTNGP12.phx.gbl...
If I write:

int[] x = { 1 };

that is fine with the compiler, but if I try to declare the array as const, meaning that it will not be changed:

const int[] x = { 1 };

I receive the error message:
error CS0623: Array initializers can only be used in a variable or field
initializer. Try using a new expression instead.

So now I try:
const int[] x = new int[1] { 1 };

and I get:
error CS0133: The expression being assigned to
'Sis.Messaging.BasicSendersReceivers.TestTcpHandle r.itest' must be constant

Nov 16 '05 #2
You can't declare complex constants in C#.

You can get most of the desired effect by declaring it readonly, but
even that has its limitations.

Nov 16 '05 #3

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

Similar topics

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...
14
by: Eric Bantock | last post by:
Very basic question I'm afraid. Once an array has been declared, is there a less tedious way of assigning values to its members than the following: myarray=8; myarray=3; myarray=4; myarray=0;...
1
by: Bern McCarty | last post by:
I am using MEC++ in VC 7.1. I had a method on a __gc object that looked like this: __property System::UInt32 get_MyProperty(void) { System::Byte __pin * pinBytes = &m_byteArray; // entire...
10
by: athanasios.silis | last post by:
Hello everyone, i am attempting to make a structure #include "globalVars.h" struct myStruct{ int offset; unsigned char uChars; } saveVars, getVars;
1
by: Ilya N. Golubev | last post by:
There is already one function processing pointer values this way. And there may easily appear more, including wrappers for this function. For purposes of further discussion, consider simplified...
15
by: edson | last post by:
Greetings. My microcontroller program uses arrays of strings. I want to store the strings AND their pointers in ROM. I am able to initialize the arrays so that the strings are put in ROM, but the...
2
by: Pavan | last post by:
Hi, I need to create a consant array of larze size but however its elements can be calculated using an equation, say for example I need an int arry of 20 elements where each element will be arr...
3
by: longbrmb | last post by:
I'm new to C++/CLI and my main background is Java. I'm trying to create an array of constants as a static member of a class. An example is shown below: public ref class TestClass { public:...
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
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
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...
0
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,...
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
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.