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

  • Thread starter Thread starter Chris Saunders
  • Start date Start date
C

Chris Saunders

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
 
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
 
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

Don't you need to make that const static?

Chris
 
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
 
Chris said:
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.
 
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
 
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
 

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

Back
Top