473,320 Members | 1,930 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,320 software developers and data experts.

Constant expression is required

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
Nov 6 '08 #1
8 13356
On Nov 6, 6:40*pm, "PJ6" <no...@nowhere.netwrote:
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
Nov 6 '08 #2
On Nov 6, 8:43*pm, kimiraikkonen <kimiraikkone...@gmail.comwrote:
On Nov 6, 6:40*pm, "PJ6" <no...@nowhere.netwrote:
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
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
Nov 6 '08 #3
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

"kimiraikkonen" <ki*************@gmail.comwrote in message
news:7c**********************************@i20g2000 prf.googlegroups.com...
On Nov 6, 6:40 pm, "PJ6" <no...@nowhere.netwrote:
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

Nov 6 '08 #4
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.

Nov 6 '08 #5
"PJ6" <no***@nowhere.netschrieb
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
Nov 6 '08 #6
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
"PJ6" wrote:
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
Nov 7 '08 #7
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
Nov 7 '08 #8
On Nov 7, 12:28 pm, "PJ6" <no...@nowhere.netwrote:
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
Nov 10 '08 #9

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

Similar topics

9
by: hemal | last post by:
I came across a very strange situation at work. There is an order of magnitude difference in execution time for the following two queries (10 v/s ~130 msec): select count(*) from table_name...
9
by: pvinodhkumar | last post by:
The number of elemets of the array, the array bound must be constant expression?Why is this restriction? Vinodh
10
by: Ricardo Gibert | last post by:
How can I "force" a macro to accept integer constants or expressions whose value is determined during compilation? For example, if we have: #define TEST(X) blah blah with X I want the macro to...
13
by: devdatta_clc | last post by:
Hi C experts I've a bunch of questions. Consider this simplified piece of code. const int a = 10; int main () { static int b = a;
22
by: Tomás Ó hÉilidhe | last post by:
I've been developing a C89 microcontroller application for a while now and I've been testing its compilation using gcc. I've gotten zero errors and zero warnings with gcc, but now that I've moved...
3
by: Rob Blackmore | last post by:
I am trying to create an attribute of type Icon and have the following code: <AttributeUsage(AttributeTargets.Class)_ Public Class PIMSGridViewIconAttribute Inherits System.Attribute '///...
7
by: John Koleszar | last post by:
Hi all, I'm porting some code that provides compile-time assertions from one compiler to another and ran across what I believe to be compliant code that won't compile using the new compiler. Not...
7
by: Hendrik Schober | last post by:
Hi, this #include <string> class test { typedef std::string::size_type size_type; static const size_type x = std::string::npos; }; doesn't compile using either VC9 ("expected constant...
56
by: Adem | last post by:
C/C++ language proposal: Change the 'case expression' from "integral constant-expression" to "integral expression" The C++ Standard (ISO/IEC 14882, Second edition, 2003-10-15) says under...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.