473,569 Members | 2,844 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Constants

OK, my brain has turned to mush on this one.

I want to implement some constant integers that can be used by several
classes in the same namespace. i.e.

namespace MySpace
{
const int THEBOXWIDTH = 24;
const int THEBOXHEIGHT = 48;

class A
{
private int theHeight = THEBOXHEIGHT;
}

class B
{
private int theHeight = THEBOXHEIGHT;
}
}

How can I achieve this? Am I going to have to use enums?

Thanks in advance

Mike

Jan 12 '06 #1
3 1393
Publicjoe wrote:
OK, my brain has turned to mush on this one.

I want to implement some constant integers that can be used by several
classes in the same namespace. i.e.

namespace MySpace
{
const int THEBOXWIDTH = 24;
const int THEBOXHEIGHT = 48;

<snip>

Constants must be placed inside a class.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vk arlsen.no
PGP KeyID: 0x2A42A1C2
Jan 12 '06 #2
You could just wrap them in another class.

internal class Constants
{
public const int THEBOXWIDTH = 24;
public const int THEBOXHEIGHT = 48;
}

public class A
{
private int theHeight = Constants.THEBO XHEIGHT;
}

public class B
{
private int theHeight = Constants.THEBO XHEIGHT;
}

--
Tim Wilson
..NET Compact Framework MVP

"Publicjoe" <mi**@publicjoe .co.uk> wrote in message
news:us******** *****@TK2MSFTNG P15.phx.gbl...
OK, my brain has turned to mush on this one.

I want to implement some constant integers that can be used by several
classes in the same namespace. i.e.

namespace MySpace
{
const int THEBOXWIDTH = 24;
const int THEBOXHEIGHT = 48;

class A
{
private int theHeight = THEBOXHEIGHT;
}

class B
{
private int theHeight = THEBOXHEIGHT;
}
}

How can I achieve this? Am I going to have to use enums?

Thanks in advance

Mike

Jan 12 '06 #3
Thanks, works a treat.

Mike

"Tim Wilson" <TIM(UNDERSCORE )WILSON(AT)ROGE RS(PERIOD)COM> wrote in message
news:el******** ******@TK2MSFTN GP10.phx.gbl...
You could just wrap them in another class.

internal class Constants
{
public const int THEBOXWIDTH = 24;
public const int THEBOXHEIGHT = 48;
}

public class A
{
private int theHeight = Constants.THEBO XHEIGHT;
}

public class B
{
private int theHeight = Constants.THEBO XHEIGHT;
}

--
Tim Wilson
.NET Compact Framework MVP

"Publicjoe" <mi**@publicjoe .co.uk> wrote in message
news:us******** *****@TK2MSFTNG P15.phx.gbl...
OK, my brain has turned to mush on this one.

I want to implement some constant integers that can be used by several
classes in the same namespace. i.e.

namespace MySpace
{
const int THEBOXWIDTH = 24;
const int THEBOXHEIGHT = 48;

class A
{
private int theHeight = THEBOXHEIGHT;
}

class B
{
private int theHeight = THEBOXHEIGHT;
}
}

How can I achieve this? Am I going to have to use enums?

Thanks in advance

Mike


Jan 12 '06 #4

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

Similar topics

8
1861
by: Raymond Hettinger | last post by:
Comments are invited on the following proposed PEP. Raymond Hettinger ------------------------------------------------------- PEP: 329
0
3046
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for many of the top/height/left settings. I noticed that some of the constants were defined by using other constant values, and I was impressed that...
13
6072
by: Andrew | last post by:
I use conditional compiler constants, set through the VBA IDE in Tools, <projectname> Properties, that I refer to throughout my code to control which code is used during development, and which during production. Usually, this only wraps code used to control quitting the whole app versus just shutting a form, but it can also control many...
4
51166
by: Amadelle | last post by:
Hi all and thanks again in advance, What is the best way of defining global constants in a C# application? (A windows application with no windows forms - basically a set of classes). Would it be a wise idea to create a clsCommonApp and let all other classes to be derived from that class? and define all constants in that base class? Any...
8
2875
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger class that I want its instance to be accessible from any other classe in the project. There is also a bunch of constants that I want to be public...
34
3353
by: newsposter0123 | last post by:
The code block below initialized a r/w variable (usually .bss) to the value of pi. One, of many, problem is any linked compilation unit may change the global variable. Adjusting // rodata const long double const_pi=0.0; lines to // rodata
6
3117
by: PC | last post by:
Gentlesofts, Forgive me. I'm an abject newbie in your world, using VB 2005 with the dot-Net wonderfulness. So, I'm writing a wonderful class or two to interface with a solemnly ancient database. In times recently past, I would have done this with Borland Delphi. So, that's my perspective and I have my old Delphi code to crib from. ...
17
2054
by: Neil Cerutti | last post by:
The Glk API (which I'm implementing in native Python code) defines 120 or so constants that users must use. The constants already have fairly long names, e.g., gestalt_Version, evtype_Timer, keycode_PageDown. Calls to Glk functions are thus ugly and tedious. scriptref = glk.fileref_create_by_prompt( glk.fileusage_Transcript |...
2
2505
by: Leslie Sanford | last post by:
I want to define a set of floating point constants using templates insteand of macros. I'd like to determine whether these constants are floats or doubles at compile time. In my header file, I have this: template<bool DoublePrecision> struct Constants { typedef double SampleType; static const double pi;
54
3558
by: shuisheng | last post by:
Dear All, I am always confused in using constants in multiple files. For global constants, I got some clues from http://msdn.microsoft.com/en-us/library/0d45ty2d(VS.80).aspx So in header file writing: const double PI = 3.14; Every time using it, include the header file.
0
7695
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8119
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7668
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7964
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6281
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...
1
5509
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...
0
5218
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...
0
3653
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...
0
3637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.