In most object oriented languages, there are obvious ways of creating true
constants that have a lifetime that is the lifetime of the whole
application, live outside of any object, are visible by code in other files,
and usable within multiple objects.
In C#, by contrast, I am getting the impression that a constant may only be
defined inside a classe, so that you have to create an object of that class
first, then reference the constant as being a member of that class. Is my
impression wrong?
All I am trying to do is to make a separate file that contains some
constants that live outside of any objects (so I don't have to create some
object before I can use the constant), and which constants are visible from
code in multiple objects. In other words, like one would do with a C header
file that only included constant
declarations. I haven't been able to find any examples of how to do this in
C#: every code example that I have seen that even uses constants seems to
create them inside a class.
Is it possible to create and use true constants in C#? If so, could someone
scratch out a crude 10-line outline of an example? I'd really appreciate
it!
Best regards,
Dr. Bob Miller
Trace Systems, Inc.
Certainly you don't have to instantiate a class instance. Just declare your
constants as static, read-only properties, and the classname starts to
function like a logical grouping.
using System;
namespace Project2
{
class MathConst
{
public static double Pi
{
get{return 3.1427;}
}
public static string Lie
{
get{return "0 = 1";}
}
}
class Class
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine(MathConst.Pi.ToString());
Console.WriteLine(MathConst.Lie);
Console.WriteLine((10 * MathConst.Pi).ToString());
Console.ReadLine();
}
}
}
I found I could shorten things down to one-liners by declaring the constants
as static
fields, like so:
using System;
namespace Project2
{
class MathConst
{
// Shorten this...
//public static double Pi
//{
//get{return 3.1427;}
//}
// down to one line by using a static field
public static double Pi = 3.1427;
// Shorten this..
public static string Lie
{
get{return "0 = 1";}
}
// down to one line by using a static field
public static string Lie = "0 = 1";
}
class Class
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine(MathConst.Pi.ToString());
Console.WriteLine(MathConst.Lie);
Console.WriteLine((10 * MathConst.Pi).ToString());
Console.ReadLine();
}
}
}
Many thanks for your help!
Best regards,
Dr. Bob Miller
"Bob Dawson" <RBDa...@prodigy.net> wrote in message
news:44f3b155$1...@newsgroups.borland.com...
| I found I could shorten things down to one-liners by declaring the
constants
| as static
| fields, like so:
Noooooo!!!!!
This will not give you consts, it will give you public fields that can be
changed.
Try the following :
static void Main(string[] args)
{
MathConst.Pi = 654.89;
}
If you really want to save typing, at least use readonly fields.
namespace Project2
{
class MathConst
{
public static readonly double Pi = 3.1427;
public static readonly string Lie = "0 = 1";
}
class Class
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
Console.WriteLine(MathConst.Pi.ToString());
Console.WriteLine(MathConst.Lie);
Console.WriteLine((10 * MathConst.Pi).ToString());
Console.ReadLine();
}
}
}
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
Yep. "Static" in C# is similar to "class" in a Delphi method declaration,
though there are a couple of differences in how class-level and
instance-level stuff works.
> You still have to refer to the "constant" with the name of the class it is
> defined in, I see.
Yeah, but like I said, just look at that as an organizational tool. You just
write MathConstants. or FinanceConstants. or YourDomainConstants. and let
code completion take over. the fact that it's a class really doesn't matter
that much.
> public static double Pi = 3.1427;
Like Joanna said, specify "readonly", but otherwise that's going to work the
same. I have an old OO-ers tic of never declaring public fields, though in
this case there's really nothing wrong with it.
bobD
> In most object oriented languages, there are obvious ways of creating true
> constants that have a lifetime that is the lifetime of the whole
> application, live outside of any object, are visible by code in other files,
> and usable within multiple objects.
I hate to contradict what has already been suggested but you don't
want to use static or readonly, you want to use the 'const' keyword.
'const' has an advantage over static readonly fields as the values are
calculated at compile time. eg.
class MyClass
{
public int x;
public static readonly int c2 = 5;
}
public static void Main()
{
Console.WriteLine("c1 = {0}, c2 = {1}", MyClass.c1, MyClass.c2
Console.WriteLine("c1*4 = {0}, c2*4 = {1}", MyClass.c1*4, MyClass.c2*4
);
In the second WriteLine the compiler will pre calculate the value 20,
for c2 it will perform the calculation on each execution.
A couple of points 'const' fields are automatically static and
read-only.
'readonly' fields aren't always.
--
Marc Rohloff [TeamB]
marc rohloff -at- myrealbox -dot- com
| class MyClass
| {
| public int x;
| public static readonly int c2 = 5;
| }
|
| public static void Main()
| {
| Console.WriteLine("c1 = {0}, c2 = {1}", MyClass.c1, MyClass.c2
| Console.WriteLine("c1*4 = {0}, c2*4 = {1}", MyClass.c1*4, MyClass.c2*4
| );
|
| In the second WriteLine the compiler will pre calculate the value 20,
| for c2 it will perform the calculation on each execution.
|
| A couple of points 'const' fields are automatically static and
| read-only.
Yes, I had forgotten that they were static, even if not declared as such.
But declaring public fields, not marked as const, should still be a real
no-no. Bob's suggestion of read-only properties is a valid solution, for
those who don't mind typing :-)
Well, this whole thing has shore been a lesson to me!
Many thanks to all who responded. This is why the Borland (Devco?)
community is so great!
Best regards,
Dr. Bob Miller
"Joanna Carter [TeamB]" <joa...@not.for.spam> wrote in message
news:44f5...@newsgroups.borland.com...