473,772 Members | 2,522 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Want to define a constant along the lines of Math.PI

I'm not very familiar with C#. I have a static class and would like to
define some constants similarily to Math.PI. Could someone show me how to
go about doing this. So far no luck searching.

Regards
Chris Saunders

Jun 27 '08 #1
7 2777
n!
umm, you mean something along the lines of:

public static class MyClass
{
public const MyPI = 3.14157f;
}

? That should have been incredibly easy to find.

ave

"Chris Saunders" <ev**@mountainc able.netwrote in message
news:3D******** *************** ***********@mic rosoft.com...
I'm not very familiar with C#. I have a static class and would like to
define some constants similarily to Math.PI. Could someone show me how to
go about doing this. So far no luck searching.

Regards
Chris Saunders

Jun 27 '08 #2
On Apr 21, 9:32 am, "n!" <nfactor...@nom ailplease.comwr ote:
umm, you mean something along the lines of:

public static class MyClass
{
public const MyPI = 3.14157f;

}

? That should have been incredibly easy to find.

ave

"Chris Saunders" <e...@mountainc able.netwrote in message

news:3D******** *************** ***********@mic rosoft.com...
I'm not very familiar with C#. I have a static class and would like to
define some constants similarily to Math.PI. Could someone show me how to
go about doing this. So far no luck searching.
Regards
Chris Saunders
Don't you need to make that const static?

Chris
Jun 27 '08 #3
First thanks for the response. For learning I'm trying to define a class
that represents complex numbers so I don't think it is quite so simple
(although it may be). For my class I have only one constructor that just
takes two doubles for the real and imaginary parts. What you described I
did find out about easily. I am attempting to define a static class for
doing math functions on complex numbers and I wish to define some simple
constants. My apologies if my question seems silly.

Regards
Chris Saunders

"n!" <nf********@nom ailplease.comwr ote in message
news:us******** ******@TK2MSFTN GP03.phx.gbl...
umm, you mean something along the lines of:

public static class MyClass
{
public const MyPI = 3.14157f;
}

? That should have been incredibly easy to find.

ave

"Chris Saunders" <ev**@mountainc able.netwrote in message
news:3D******** *************** ***********@mic rosoft.com...
>I'm not very familiar with C#. I have a static class and would like to
define some constants similarily to Math.PI. Could someone show me how
to go about doing this. So far no luck searching.

Regards
Chris Saunders

Jun 27 '08 #4
Chris Dunaway wrote:
On Apr 21, 9:32 am, "n!" <nfactor...@nom ailplease.comwr ote:
>umm, you mean something along the lines of:

public static class MyClass
{
public const MyPI = 3.14157f;

}

? That should have been incredibly easy to find.

ave

"Chris Saunders" <e...@mountainc able.netwrote in message

news:3D******* *************** ************@mi crosoft.com...
>>I'm not very familiar with C#. I have a static class and would
like to define some constants similarily to Math.PI. Could someone
show me how to go about doing this. So far no luck searching.
>>Regards
Chris Saunders

Don't you need to make that const static?
A C# const field is automatically both static and literal.
>
Chris

Jun 27 '08 #5
Chris Saunders wrote:
First thanks for the response. For learning I'm trying to define a
class that represents complex numbers so I don't think it is quite so
simple (although it may be). For my class I have only one
constructor that just takes two doubles for the real and imaginary
parts. What you described I did find out about easily. I am
attempting to define a static class for doing math functions on
complex numbers and I wish to define some simple constants. My
apologies if my question seems silly.
class Complex
{
...

public static readonly i = new Complex(0, 1);
}

They aren't compile-time literals, only built-in types can be compile-time
literal.
>
Regards
Chris Saunders

"n!" <nf********@nom ailplease.comwr ote in message
news:us******** ******@TK2MSFTN GP03.phx.gbl...
>umm, you mean something along the lines of:

public static class MyClass
{
public const MyPI = 3.14157f;
}

? That should have been incredibly easy to find.

ave

"Chris Saunders" <ev**@mountainc able.netwrote in message
news:3D******* *************** ************@mi crosoft.com...
>>I'm not very familiar with C#. I have a static class and would
like to define some constants similarily to Math.PI. Could someone
show me how to go about doing this. So far no luck searching.

Regards
Chris Saunders

Jun 27 '08 #6
Ah, thanks very much - this is what I was after. Hopefully before too long
my questions won't suggest I'm quite so simple.

Regards
Chris Saunders

"Ben Voigt [C++ MVP]" <rb*@nospam.nos pamwrote in message
news:uR******** ******@TK2MSFTN GP02.phx.gbl...
Chris Saunders wrote:
>First thanks for the response. For learning I'm trying to define a
class that represents complex numbers so I don't think it is quite so
simple (although it may be). For my class I have only one
constructor that just takes two doubles for the real and imaginary
parts. What you described I did find out about easily. I am
attempting to define a static class for doing math functions on
complex numbers and I wish to define some simple constants. My
apologies if my question seems silly.

class Complex
{
...

public static readonly i = new Complex(0, 1);
}

They aren't compile-time literals, only built-in types can be compile-time
literal.
>>
Regards
Chris Saunders

"n!" <nf********@nom ailplease.comwr ote in message
news:us******* *******@TK2MSFT NGP03.phx.gbl.. .
>>umm, you mean something along the lines of:

public static class MyClass
{
public const MyPI = 3.14157f;
}

? That should have been incredibly easy to find.

ave

"Chris Saunders" <ev**@mountainc able.netwrote in message
news:3D****** *************** *************@m icrosoft.com...
I'm not very familiar with C#. I have a static class and would
like to define some constants similarily to Math.PI. Could someone
show me how to go about doing this. So far no luck searching.

Regards
Chris Saunders

Jun 27 '08 #7
n!
Ah, thanks very much - this is what I was after. Hopefully before too
long my questions won't suggest I'm quite so simple.
I was not suggesting you were "quite so simple" ;) I was just surprised that
the answer was not easily discovered, and as I see it was simply phrased in
way I misunderstood. By 'simple constants' I assume you meant floating
point, integer etc. numeric constants. Ben pointed out the correct method
for more complex constants (making them readonly and using a static
constructor), I'd agree this is harder to find in the documentation but
didn't realise you meant these ones.

Everyone needs to ask (and shouldn't be afraid to ask) questions, no matter
how simple. But the fact you stated you looked and couldn't find it confused
me :)

ave
Jun 27 '08 #8

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

Similar topics

10
1868
by: b83503104 | last post by:
I know this is illegal: class XYZ { const int myConst = 1; ....}; But then, does it mean I have to use #define ? Thanks
2
7221
by: David Green | last post by:
Ok, i'm a real c n00b but i needed a piece of code for some work i was doing. Initially i was running the stuff under linux and using gcc to compile the c code and it worked fine but now i need to port it to windows and when i try to compile the same code with the visual studio command line tool "cl" i get this error: const.c(92) : error C2099: initializer is not a constant const.c(97) : error C2099: initializer is not a constant with...
8
6609
by: Fan Zhang | last post by:
Hi group, This is a follow-up of the question I posted this afternoon. I tried to define two constants, PI and radian in that program in the following way, #define PI 3.1415926 #define radian 180./PI I wonder whether they are the same as
18
7339
by: William | last post by:
I have the following javascript function that updates a scroll_list and sends the updated entry (with its index) to a server script ( i.e. http://mkmxg00/cgi/confirmUpload.pl ) for further processing: function saveText( scroll_list, t_area, listToBeUpdated ) { scroll_list.options.text = t_area.text; scroll_list.options.value= t_area.value; var req; var url = "http://mkmxg00/cgi/confirmUpload.pl";
1
4272
by: SpreadTooThin | last post by:
How do I define a constant that I can use in my script... For example lets say I have a file called constants.py and in there I have PI = 3.14 in my test script I do: from constants import * How do I access PI later on?
0
3575
by: CHU Run-min | last post by:
The following code was written by me. It is used for evaluating constant expressions. I think it is good. Any criticism? namespace Churunmin.HelloWorld {
4
2787
by: venkat | last post by:
I have come across some preprossor statements, such as #define PPTR_int #define PPTR_str #define DDAR_baddr & #define DDAR_caddr & What do they mean, but when i compile the code with these i am not getting any errors .
2
2591
by: python | last post by:
I'm parsing a text file for a proprietary product that has the following 2 directives: #include <somefile> #define <name<value> Defined constants are referenced via <#name#syntax. I'm looking for a single text stream that results from processing a file containing these directives. Even better would be an iterator(?) type
8
13438
by: PJ6 | last post by:
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.
0
9621
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
9454
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8937
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7461
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
6716
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
5355
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4009
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 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.