Thread View: borland.public.csharpbuilder.language.csharp
8 messages
8 total messages
Started by Dr. Bob Miller
Tue, 29 Aug 2006 02:47
True Constants in C# ?
Author: Dr. Bob Miller
Date: Tue, 29 Aug 2006 02:47
Date: Tue, 29 Aug 2006 02:47
31 lines
2394 bytes
2394 bytes
I am a longtime programmer in many other languages (Delphi, C/C++, VB, VB.NET, etc), but am brand new to C#. I am particularly puzzled by how to really use constants. 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.
Re: True Constants in C# ?
Author: Bob Dawson
Date: Tue, 29 Aug 2006 04:16
Date: Tue, 29 Aug 2006 04:16
42 lines
1802 bytes
1802 bytes
"Dr. Bob Miller" wrote
>
> 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!
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();
}
}
}
Re: True Constants in C# ?
Author: Dr. Bob Miller
Date: Tue, 29 Aug 2006 14:27
Date: Tue, 29 Aug 2006 14:27
70 lines
2694 bytes
2694 bytes
OK, I get the picture. "Static" at least allows you to avoid having to
instantiate a class.
You still have to refer to the "constant" with the name of the class it is
defined in, I see.
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" <RBDawson@prodigy.net> wrote in message
news:44f3b155$1@newsgroups.borland.com...
> "Dr. Bob Miller" wrote
>>
>> 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!
>
> 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.
>
Re: True Constants in C# ?
Author: Joanna Carter [TeamB]
Date: Tue, 29 Aug 2006 15:19
Date: Tue, 29 Aug 2006 15:19
52 lines
2071 bytes
2071 bytes
"Dr. Bob Miller" <drbob@tracesystemsinc.com> a �crit dans le message de
news: 44f44eba$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
Re: True Constants in C# ?
Author: Bob Dawson
Date: Tue, 29 Aug 2006 18:55
Date: Tue, 29 Aug 2006 18:55
23 lines
2024 bytes
2024 bytes
"Dr. Bob Miller" wrote > OK, I get the picture. "Static" at least allows you to avoid having to > instantiate a class. 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
Re: True Constants in C# ?
Author: Marc Rohloff TeamB
Date: Wed, 30 Aug 2006 00:27
Date: Wed, 30 Aug 2006 00:27
36 lines
2278 bytes
2278 bytes
On Mon, 28 Aug 2006 22:47:18 -0400, Dr. Bob Miller wrote:
> 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
Re: True Constants in C# ?
Author: Joanna Carter [TeamB]
Date: Wed, 30 Aug 2006 09:08
Date: Wed, 30 Aug 2006 09:08
32 lines
1870 bytes
1870 bytes
"Marc Rohloff [TeamB]" <"on request"> a �crit dans le message de news:
1j04hi3wixlhw.dlg@dlg.marcrohloff.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 :-)
Joanna
--
Joanna Carter [TeamB]
Consultant Software Engineer
Re: True Constants in C# ?
Author: Dr. Bob Miller
Date: Wed, 30 Aug 2006 11:44
Date: Wed, 30 Aug 2006 11:44
48 lines
2498 bytes
2498 bytes
> But declaring public fields, not marked as const, should still be a real
> no-no.
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]" <joanna@not.for.spam> wrote in message
news:44f555aa@newsgroups.borland.com...
> "Marc Rohloff [TeamB]" <"on request"> a �crit dans le message de news:
> 1j04hi3wixlhw.dlg@dlg.marcrohloff.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 :-)
>
> Joanna
>
> --
> Joanna Carter [TeamB]
> Consultant Software Engineer
>
>
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads