Constant expression is required

P

PJ6

Const factor As Double = Math.Sqrt(3) / 6

Error 1 Constant expression is required.

This looks like laziness to me. In SQL Server, functions are given a
distinction between ones that always return the same result for the same
input, and those that can change. All (?) of the functions in Math are of
the former type and should be allowed to evaluate to a constant expression,
just the same as the results of expressions with +, -, /, etc.

Paul
 
K

kimiraikkonen

Const factor As Double = Math.Sqrt(3) / 6

Error 1 Constant expression is required.

This looks like laziness to me. In SQL Server, functions are given a
distinction between ones that always return the same result for the same
input, and those that can change. All (?) of the functions in Math are of
the former type and should be allowed to evaluate to a constant expression,
just the same as the results of expressions with +, -, /, etc.

Paul

It is not supported to initialize an object using a Const. Instead,
declare a variable then assign it to a Const if Const is really needed
as follows:

Untested:

Dim myfactor As Double = Math.Sqrt(3) / 6
Const factor As Double = myfactor

Hope this helps,

Onur Guzel
 
K

kimiraikkonen

It is not supported to initialize an object using a Const. Instead,
declare a variable then assign it to a Const if Const is really needed
as follows:

Untested:

Dim myfactor As Double = Math.Sqrt(3) / 6
Const factor As Double = myfactor

Hope this helps,

Onur Guzel

As i stated, my post was untested, and yes it gives "constant
expression is required" error though,

So, as tested, you need to use a variable instead a Const:

' works
Dim myfactor As Double = Math.Sqrt(3) / 6

Sorry for previous post,

Onur Güzel
 
S

Stanimir Stoyanov

This is not valid either because myfactor is not a constant expression
(depends on an object too). The compiler cannot evaluate the result of
Math.Sqrt (it is not constant) and that's the reason your expression is not
correct. If the calculations are affecting performance you can hard-code the
result of Math.Sqrt(3) which is 1.7320508075688772 and use it. Otherwise, if
you are fond of using the more readable code of calling Math.Sqrt() you
should consider not making the expression constant.

Sample code:

Const squareRootOf3 As Double = 1.7320508075688772
Const factor As Double = squareRootOf3 / 6

or (if you do not have to use squareRootOf3 anywhere else)

Const factor As Double = 1.7320508075688772 / 6
--
Stanimir Stoyanov
http://stoyanoff.info

Const factor As Double = Math.Sqrt(3) / 6

Error 1 Constant expression is required.

This looks like laziness to me. In SQL Server, functions are given a
distinction between ones that always return the same result for the same
input, and those that can change. All (?) of the functions in Math are of
the former type and should be allowed to evaluate to a constant
expression,
just the same as the results of expressions with +, -, /, etc.

Paul

It is not supported to initialize an object using a Const. Instead,
declare a variable then assign it to a Const if Const is really needed
as follows:

Untested:

Dim myfactor As Double = Math.Sqrt(3) / 6
Const factor As Double = myfactor

Hope this helps,

Onur Guzel
 
A

Armin Zingler

PJ6 said:
Const factor As Double = Math.Sqrt(3) / 6

Error 1 Constant expression is required.

This looks like laziness to me. In SQL Server, functions are given a
distinction between ones that always return the same result for the
same input, and those that can change. All (?) of the functions in
Math are of the former type and should be allowed to evaluate to a
constant expression, just the same as the results of expressions
with +, -, /, etc.


If it's a field and not a local variable, you can write:


Public Readonly factor As Double = Math.Sqrt(3) / 6


Armin
 
A

AMercer

Const factor As Double = Math.Sqrt(3) / 6
Error 1 Constant expression is required.

This looks like laziness to me. In SQL Server, functions are given a
distinction between ones that always return the same result for the same
input, and those that can change. All (?) of the functions in Math are of
the former type and should be allowed to evaluate to a constant expression,
just the same as the results of expressions with +, -, /, etc.

Assuming 'factor' is not a local variable in a sub or function, you can get
what you want with a declaration like this:

Public ReadOnly factor As Double = Math.Sqrt(3) / 6

Instead of a constant, factor is declared as a readonly variable. I use
this syntax to declare a name with an initializer that is too complicated for
VB to handle. The initializing expression can even be a function that your
write that returns a Double. As you point out, it would be more convenient
if VB allowed more expressions for a Const initializer.
 
O

OmegaSquared

Hello, Paul,

I think that the problem here is that dotNet really doesn't know what
Math.Sqrt is and so cannot determine that the result will be constant. I
also have found this sort of thing to be a nuisance. Perhaps (like you
suggest) there could be some way when a function/property is defined for
dotNet that it could be declared to be "constant result from constant
arguments". We could always hope such a thing might appear in some future
version. ;-)

In the meanwhile, I just do what others are suggesting, and use a variable.
(It doesn't cost much.)

Cheers,
Randy
 
P

PJ6

I will go so far to say that it would be easy not only for MS to classify
all .Net Framework primitive functions as either deterministic or
non-deterministic, but also automatically classify any static function in
such a way, within certain reasonable constraints, from its use or lack of
use of non-deterministic primitives.

I'd be surprised if this hasn't already been discussed considering we have
LINQ. "That's not correct" only means, not yet.

And yes, I'm familiar with Shared Readonly, I'm just nitpicking and
generally causing trouble ;)

Paul
 
C

Chris Dunaway

I will go so far to say that it would be easy not only for MS to classify
all .Net Framework primitive functions as either deterministic or
non-deterministic, but also automatically classify any static function in
such a way, within certain reasonable constraints, from its use or lack of
use of non-deterministic primitives.

I'd be surprised if this hasn't already been discussed considering we have
LINQ. "That's not correct" only means, not yet.

And yes, I'm familiar with Shared Readonly, I'm just nitpicking and
generally causing trouble ;)

Paul

Since constants are determined at compile time, you would have to have
the *compiler* execute the Math.Sqrt method. What other methods
should the compiler execute? And how will the compiler know when to
execute a method and when not to? What should it do if there is an
exception? What if someone passed in a negative number?

It seems as if it's asking too much for the compiler to do, which
might explain why they added the ReadOnly keyword.

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top