473,805 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A Question of Style

Should constant class member variables be held like this:

public class Foo
{
public const int ConstVar = 100;
}

Or like this:

public class Foo
{
public int ConstVar
{
get { return 100; }
}
}

Or even:

public class Foo
{
private const int constVar = 100;

public int ConstVar
{
get { return constVar; }
}
}

Since I've been using C# and .NET (about two weeks), I've been using
the first way, i.e. public member variables.

Which is the "correct" way to do this, according to common style?
Nov 15 '05 #1
3 1273
Either the first or the third would be the accepted definitions of
constants. Your choice of these are really whether your constant is used
only by the class or by the world.

Properties are used, primarily, to access an object's state. Constants are
not state, they are just values.
"C# Learner" <cs****@learner .here> wrote in message
news:ei******** *************** *********@4ax.c om...
Should constant class member variables be held like this:

public class Foo
{
public const int ConstVar = 100;
}

Or like this:

public class Foo
{
public int ConstVar
{
get { return 100; }
}
}

Or even:

public class Foo
{
private const int constVar = 100;

public int ConstVar
{
get { return constVar; }
}
}

Since I've been using C# and .NET (about two weeks), I've been using
the first way, i.e. public member variables.

Which is the "correct" way to do this, according to common style?

Nov 15 '05 #2
L#
On Sun, 01 Feb 2004 14:11:13 +0000, C# Learner <cs****@learner .here>
wrote:
Should constant class member variables be held like this:

public class Foo
{
public const int ConstVar = 100;
}

Or like this:

public class Foo
{
public int ConstVar
{
get { return 100; }
}
}

Or even:

public class Foo
{
private const int constVar = 100;

public int ConstVar
{
get { return constVar; }
}
}

Since I've been using C# and .NET (about two weeks), I've been using
the first way, i.e. public member variables.

Which is the "correct" way to do this, according to common style?


I would say that the first one is the correct and most performant one.

In the second example you expose the constant as a property, which is
overhead. A property should be used when some extra code is to be
executed before returning the value. In the case of a constant I don't
see the need for this.

In the third example you put the constant private, to hide it. I don't
think there's need to do this, for the samen reason as above. The
constant cannot be changed anymore, private or public.

My 2 cents,

Ludwig
Nov 15 '05 #3
"Peter Rilling" <pe***@nospam.r illing.net> wrote:
Either the first or the third would be the accepted definitions of
constants. Your choice of these are really whether your constant is used
only by the class or by the world.
In this scenario, the constants are to be used by the world.
Properties are used, primarily, to access an object's state. Constants are
not state, they are just values.


Thanks.
Nov 15 '05 #4

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

Similar topics

4
5486
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) { document.getElementById(nameOfDiv).style.visibility='visible'; document.getElementById(nameOfDiv).style.height='auto'; if (nameOfDiv != 'weblogs')
1
2038
by: Roy | last post by:
Hey all. All I'm trying to do is get a darn double solid bar in my datagrid footer. Doesn't seem to work. Any tips? The weird thing is all the other stylesheet attributes work. If I increase the font size it's reflected, etc... Here's my style sheet code: ..Footer { border-top-style:double; border-top-color:Black;
0
2627
by: cedoucette | last post by:
I just wrote code to support sortable columns in a datagrid. It seems to work fine; but, it doesn't look right. The problem is that I have a generic style for links and a different style for the header row - and they conflict. The sortable column apparently uses the generic style for <a> elements and the rest of the header uses "headerRow". Can anyone tell me how to get consistent styles for each column in my datagrid header?
1
9041
by: RonY | last post by:
I have a dropdown which calls SetTimePeriod method on change the selection. In the JS function, I reset the field style.display based on what the selection is. This works fine with IE but not working with Firefox browser. The firefox browser has problem to display a field after its style.display is reset. At the end of the JS function, I printed out the fields style.display value. They are set correctly. How to resovle this? My JS is : ...
8
6703
by: JT | last post by:
Hi, I have done a fair amount of style editing inline in ASP. I'm now using VS 2005 with a standard web project (not Web Application Project). This is my first foray into CSS in a style sheet and also my first true attempt at using master pages. I tried setting up a style sheet with a simple setting to float an image to the right and it had no effect on the image. Then, I tried putting the style code in my ASPX file as such,
6
13779
by: rongchaua | last post by:
Hi all, I want to change the style of a button. But I don't know how to do it. For example, I have already a button OK on form. I want to add these styles to this button (WS_CHILD || WS_VISIBLE || WS_CLIPSIBLINGS || WS_TABSTOP || BS_FLAT). What should I do now? All help will be appreciated. rca.
1
15333
by: Armin Gajda | last post by:
Hi, I have the following problem: An input field get a border assigned by a style class (e.g. 2px solid red). When the field gets the focus, we set the border to green. element.style.border = "2px solid green"; When the field looses the focus, the border should change back to red.
0
2651
by: =?Utf-8?B?QXR1bA==?= | last post by:
When .Net 1.0 webservice (VS2003) generates a wsdl - <wsdl:binding name="TestSoap" type="tns:TestSoap"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/(note: style attribute present) <wsdl:operation name="HelloWorld"> <soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" /> But when .Net 2.0 webservice (VS2005) generates a wsdl -
3
1926
by: Michellevt | last post by:
Hi I am working on a project (for college) and wondered if anyone can help me with my problem. In the project we are not allowed to make use of any "style" attributes but "class" attributes instead. The following is the java script that i am using and i am having trouble in using a class instead of a style tag because simply replacing the style=\"position:absolute;\" tag with class=\"posiAbs\" where in the external css ".posiAbs...
3
1959
Claus Mygind
by: Claus Mygind | last post by:
I want to move some style setting into a javaScript function so I can make them variable but I get "invalid assignment left-hand side" error? see my question at the bottom of this message. It works fine when I stream it out in my html page like this: <DIV ID="popSearch" STYLE=" position:absolute; visibility:hidden;
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9179
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
7644
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
6874
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
5536
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
5676
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4316
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
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.