473,569 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Declaring a constant

I have made a declaration like this:

private const Complex I = new Complex(0.0, 1.0);

When I try to build this I get the error:

The expression being assigned to 'ComplexNumberL ib.ComplexMath. I' must be
constant.

I do not understand why this constructor is not considered to be constant
nor how
to correct this error. (Just learning C#)

Regards
Chris Saunders
Jan 27 '06 #1
15 3256
Hi Chris,
a constant expression needs to be something that can be evaluated at
compile time, so you can only use consts with value types not reference types
(apart from string which is a little bit different and null).

If you want to have a non changeable reference type use the readonly keyword
instead.

Hope that helps
Mark Dawson
http://www.markdawson.org

"Chris Saunders" wrote:
I have made a declaration like this:

private const Complex I = new Complex(0.0, 1.0);

When I try to build this I get the error:

The expression being assigned to 'ComplexNumberL ib.ComplexMath. I' must be
constant.

I do not understand why this constructor is not considered to be constant
nor how
to correct this error. (Just learning C#)

Regards
Chris Saunders

Jan 27 '06 #2
Chris,
I have made a declaration like this:

private const Complex I = new Complex(0.0, 1.0);

When I try to build this I get the error:

The expression being assigned to 'ComplexNumberL ib.ComplexMath. I' must be
constant.


A constant expression is an expression that can be fully evaluated at
compile time. Therefore, the only possible values for constants of reference
types are string and null.

Regards,

Randy
Jan 27 '06 #3
Did you write the code for Complex? If so, can you provide the code for the
constructor you're calling?
--
Dale Preston
MCAD C#
MCSE, MCDBA
"Chris Saunders" wrote:
I have made a declaration like this:

private const Complex I = new Complex(0.0, 1.0);

When I try to build this I get the error:

The expression being assigned to 'ComplexNumberL ib.ComplexMath. I' must be
constant.

I do not understand why this constructor is not considered to be constant
nor how
to correct this error. (Just learning C#)

Regards
Chris Saunders

Jan 27 '06 #4
A constant expression is an expression that can be fully evaluated at
compile time. Therefore, the only possible values for constants of
reference types are string and null.

Possibly you need "readonly" field

Jan 27 '06 #5
And if you *did* write this code, then note that Complex should probably be
a value-type (struct), not a reference type (class); this would solve this
problem, plus allow for value-type symantecs, which is what people probably
expect from a complex (or quarternian for that matter) number.

Marc
Jan 27 '06 #6
Even if Compex type shall be changed from "class" to "struct" it will
not solve the problem.

Constant expression is evaluated in compile time and embedded into IL
code and it is impossible for complex data structures

C# language specification
7.15 Constant expressions
A constant-expression is an expression that can be fully evaluated at
compile-time.
constant-expression:
expression
The type of a constant expression can be one of the following: sbyte,
byte, short, ushort, int, uint, long, ulong, char, float, double,
decimal, bool, string, any enumeration type, or the null type

Jan 27 '06 #7
My thanks for the many responses.
Particularily Vladimir Matveev since you led me to
the specification - which I will read.
I have just been learning C# for a couple of days now
and was just writing the Complex class as a way to
learn.
One person suggested that I should use a struct and I
will give that option a try - don't know C# well enough
yet to understand the implications.
I am mostly accustomed to Eiffel which is quite different -
in Eiffel you can make a class a value type.

Anyways, many thanks for the help.

Regards
Chris Saunders
Jan 27 '06 #8
Fair enough;

</retract> ;-p

Marc
Jan 27 '06 #9
Phew... you saved my dignity there by vindicating at least *part* of my
previous post ;-p

IMO, yes, you should be using a struct for this; an alternative would be an
*immutable* class (e.g. one where all the fields are formally readonly and
the class is marked [Immutable]) - otherwise you could get some very
unexpected results, particularly if two objects have a Complex property, and
the same Complex instance is assigned to both; when one of them updates the
object (e.g. adds 2 to the real part), then the value would appear to change
for *both* objects that hold it, which is probably not what is intended.

Marc
Jan 27 '06 #10

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

Similar topics

2
25449
by: Sriram Chadalavada | last post by:
Hello everyone, I am a newbie to Python with experience in C programming. For my project, I am re-writing C routines as Python functions. I am currently using raw numerical values and was wondering if there is an equivalent of #define in Python for declaring constants. Is there an elegant way of defining constants in Python? Please let me...
28
1612
by: JKop | last post by:
const unsigned short int AmountHumanAdultTeeth = 32; inline unsigned short int AddFive(unsigned short int Numbr) { return Numbr + 5; } USE THESE!!
14
74810
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; myarray=0; myarray=1; myarray=8;
3
2900
by: Antony | last post by:
When I declared a constant in a class like: public Const attr As ClassB = nothing (ClassB is the user-defined Class) I get the compiler error: Constants must be an intrinsic or enumerated type, not a class, structure, or array type. So how can I declare a constant value of type ClassB in this case? Thanks!
7
9282
by: Iain Mcleod | last post by:
Hi This must be an often encountered problem. I want to declare an abstract class or an interface with nothing but several static constants so that I can use polymorphism when I call each of them from my code. My stab at the problem is shown below. Can anyone suggest what my most efficient workable solution would be (i.e. I don't want to...
4
2800
by: GnG | last post by:
Hello all, Someone posted a similar question a while ago but there was no response. Does anyone know the answer? I have a managed C++ DLL which is used by a C# project. In that DLL, I have code like this: namespace Foo { public __value const int Bar = 123;
3
2215
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;
2
3036
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 = 2 + (i*i) But I want arry to be constant. How can I declare such a constant array without actually defining all the elements?
3
4046
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: static const array<constant int>^ stuff = {1,2,3,4,5}; }; This gives me compiler error C4538 which basically says that you can't
0
7694
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...
0
7609
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...
1
7666
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
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...
1
5504
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3651
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1208
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.