Declaring a constant

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Saunders

    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


  • Mark R. Dawson

    #2
    RE: Declaring a constant

    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




    "Chris Saunders" wrote:
    [color=blue]
    > 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
    >
    >
    >[/color]

    Comment

    • Randy A. Ynchausti

      #3
      Re: Declaring a constant

      Chris,
      [color=blue]
      >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.[/color]

      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


      Comment

      • Dale

        #4
        RE: Declaring a constant

        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:
        [color=blue]
        > 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
        >
        >
        >[/color]

        Comment

        • Vladimir Matveev

          #5
          Re: Declaring a 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.

          Possibly you need "readonly" field

          Comment

          • Marc Gravell

            #6
            Re: Declaring a constant

            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


            Comment

            • Vladimir Matveev

              #7
              Re: Declaring a constant

              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

              Comment

              • Chris Saunders

                #8
                Re: Declaring a constant

                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


                Comment

                • Marc Gravell

                  #9
                  Re: Declaring a constant

                  Fair enough;

                  </retract> ;-p

                  Marc


                  Comment

                  • Marc Gravell

                    #10
                    Re: Declaring a constant

                    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


                    Comment

                    • Jon Skeet [C# MVP]

                      #11
                      Re: Declaring a constant

                      Marc Gravell wrote:[color=blue]
                      > Fair enough;
                      >
                      > </retract> ;-p[/color]

                      Well, before you retract it - if you have a readonly value type, that's
                      largely "good enough" (not the same as const, but quite close). The
                      same can be achieved with a reference type, but you need to make sure
                      that the reference type is immutable, as otherwise clients can change
                      the "constant". (This is a major issue when people make readonly arrays
                      - the array reference itself is readonly, but the contents can be
                      changed.)

                      Jon

                      Comment

                      • Marc Gravell

                        #12
                        Re: Declaring a constant

                        revision: [System.Componen tModel.Immutabl eObject(true)]

                        (not [Immutable])


                        Comment

                        • Nick Hounsome

                          #13
                          Re: Declaring a constant


                          "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                          news:1138359438 .326283.133890@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                          > Marc Gravell wrote:[color=green]
                          >> Fair enough;
                          >>
                          >> </retract> ;-p[/color]
                          >
                          > Well, before you retract it - if you have a readonly value type, that's
                          > largely "good enough" (not the same as const, but quite close). The
                          > same can be achieved with a reference type, but you need to make sure
                          > that the reference type is immutable, as otherwise clients can change
                          > the "constant". (This is a major issue when people make readonly arrays
                          > - the array reference itself is readonly, but the contents can be
                          > changed.)
                          >
                          > Jon[/color]

                          It is my hope that one day the whole world will realize that leaving out C++
                          const is too high a price to pay for extra compatibility with other
                          languages.




                          Comment

                          • Jon Skeet [C# MVP]

                            #14
                            Re: Declaring a constant

                            Nick Hounsome wrote:[color=blue]
                            > It is my hope that one day the whole world will realize that leaving out C++
                            > const is too high a price to pay for extra compatibility with other
                            > languages.[/color]

                            I go back and forth on this. One of the troubles with C++ const is that
                            you can cast it away, which reduces its value significantly - but means
                            you don't need to get everything right to start with. Another is in
                            terms of expressing concepts succinctly. Presumably I should be able to
                            have const string[] and (const string)[] and them be different things -
                            bring generics in and it gets even worse.

                            Now, this sounds very negative, but I feel the pain too - I would
                            really like some way of indicating that something *won't* change an
                            object significantly, or that an object *can't* be changed, etc.

                            I suspect people will work out a way of doing this cleanly some time. I
                            also suspect that it's too late to ever put it into .NET - to be
                            effective, it would have to pervade the existing libraries, and that
                            would be horrific in terms of backward compatibility, I suspect.

                            Jon

                            Comment

                            • Nick Hounsome

                              #15
                              Re: Declaring a constant


                              "Jon Skeet [C# MVP]" <skeet@pobox.co m> wrote in message
                              news:1138360694 .069949.208630@ z14g2000cwz.goo glegroups.com.. .[color=blue]
                              > Nick Hounsome wrote:[color=green]
                              >> It is my hope that one day the whole world will realize that leaving out
                              >> C++
                              >> const is too high a price to pay for extra compatibility with other
                              >> languages.[/color]
                              >
                              > I go back and forth on this. One of the troubles with C++ const is that
                              > you can cast it away,[/color]

                              But the whole beauty of having it in .NET is that you couldn't cast it away.
                              [color=blue]
                              > which reduces its value significantly - but means
                              > you don't need to get everything right to start with.[/color]

                              I've never found it difficult.
                              [color=blue]
                              > Another is in
                              > terms of expressing concepts succinctly. Presumably I should be able to
                              > have const string[] and (const string)[] and them be different things -
                              > bring generics in and it gets even worse.
                              >[/color]

                              simplify using typedefs - another omission and one that it would cost
                              nothing to add. (of course your example is not relevant to C# cos is const
                              anyway)
                              [color=blue]
                              > Now, this sounds very negative, but I feel the pain too - I would
                              > really like some way of indicating that something *won't* change an
                              > object significantly, or that an object *can't* be changed, etc.[/color]

                              I hate having to write wrappers to ensure constness and I hate it that I
                              have to do runtime checks to see whether the IList i'm passed is readonly or
                              not.

                              You can get around the problem by going down the IConstList path but nobody
                              does because you need too many interfaces.
                              [color=blue]
                              > I suspect people will work out a way of doing this cleanly some time. I
                              > also suspect that it's too late to ever put it into .NET - to be
                              > effective, it would have to pervade the existing libraries, and that
                              > would be horrific in terms of backward compatibility, I suspect.
                              >
                              > Jon[/color]

                              I think you'll find that its those VB programmers who are the source of the
                              problem (Although that wouldn't explain java)


                              Comment

                              Working...