473,651 Members | 2,659 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 13405
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.comwrot e:
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.7320508075688 772 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.7320508075688 772
Const factor As Double = squareRootOf3 / 6

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

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

"kimiraikko nen" <ki************ *@gmail.comwrot e in message
news:7c******** *************** ***********@i20 g2000prf.google groups.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
4740
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 where column_name = 'value' or 1 = 0 select count(*) from table_name where column_name = 'value' I do not want to go into the reason why the redundent condition exists, the example is representative of the real query where it
9
3947
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
3655
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 be used as x += TEST(5); y += TEST(3*7);
13
2563
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
3600
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 over to the micrcontroller compiler I'm getting all sorts of errors. One thing I'd like to clarify is the need (in C89) for a compile- time constant in the initialiser of a variable. The compiler rejects the following source file: /* Start...
3
2604
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 '/// Private variables Private oPiGridViewIcon As System.Drawing.Icon = Nothing
7
2805
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 naming names here to remove bias - I'm trying to tell if I'm relying on implementation defined behavior or if this is a bug in the new compiler. Consider this stripped down example:
7
4258
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 expression") or Comeau Online ("constant value is not known"). If I replace
56
6713
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 6.4.2(2) : case constant-expression : I propose that the case expression of the switch statement be changed from "integral constant-expression" to "integral expression".
0
8347
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8792
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8457
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 most users, this new feature is actually very convenient. If you want to control the update process,...
1
6157
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5605
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4280
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2696
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1905
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1585
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.