🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.csharpbuilder.language.csharp
Article #154

Re: True Constants in C# ?

#154
From: Dr. Bob Miller
Date: Tue, 29 Aug 2006 14:27
70 lines
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.
>
Technical Details
Message-ID: <44f44eba$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <44f3aab7$1@newsgroups.borland.com> <44f3b155$1@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 1efe35,1326b8d2515daa80
X-Google-Attributes: gid1efe35,public
X-Google-Language: ENGLISH,ASCII-7-bit
Reply-To: "Dr. Bob Miller" <drbob@tracesystemsinc.com>
From: "Dr. Bob Miller" <drbob@tracesystemsinc.com>
Newsgroups: borland.public.csharpbuilder.language.csharp
References: <44f3aab7$1@newsgroups.borland.com> <44f3b155$1@newsgroups.borland.com>
Subject: Re: True Constants in C# ?
Date: Tue, 29 Aug 2006 10:27:03 -0400
Lines: 72
Organization: Trace Systems, Inc.
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2900.2869
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2869
X-RFC2646: Format=Flowed; Original
NNTP-Posting-Host: 68.48.46.9
Message-ID: <44f44eba$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1156861626 68.48.46.9 (29 Aug 2006 07:27:06 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news1.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail