PC Review


Reply
Thread Tools Rate Thread

Declaring a const array

 
 
Edward Diener
Guest
Posts: n/a
 
      10th Feb 2005
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.TestTcpHandler.itest' must be constant


 
Reply With Quote
 
 
 
 
Peter Rilling
Guest
Posts: n/a
 
      10th Feb 2005
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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.TestTcpHandler.itest' must be

constant
>
>



 
Reply With Quote
 
Bruce Wood
Guest
Posts: n/a
 
      10th Feb 2005
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Declaring Public Const Robert Microsoft Access VBA Modules 11 27th Nov 2009 08:53 PM
Want a const array... Sinex Microsoft C# .NET 3 10th Aug 2005 07:51 PM
Declaring a passed by reference function argument as const =?Utf-8?B?UmFscGggRmxhdWdoZXI=?= Microsoft C# .NET 7 6th Jul 2005 09:48 AM
declaring objects const? Karl Hungus Microsoft C# .NET 1 10th May 2004 08:22 AM
declaring a DateTime const Microsoft C# .NET 2 23rd Mar 2004 07:21 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:34 PM.