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 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
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
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
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.
"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
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
by: pvinodhkumar |
last post by:
The number of elemets of the array, the array bound must be constant
expression?Why is this restriction?
Vinodh
|
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);
|
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;
|
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...
| |
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
|
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:
|
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
|
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".
|
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...
|
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...
| |
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |