473,508 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

const string values

I'm fairly new to the whole .NET and OOP programming so bear with me.

In the past if I wanted to have constants defined that I could use in my
code, I'd include them either the file or as an include file. But with .NET
and OOP, there has to be a different way than just including them in each of
the classes I want to utilize them in.

Here's an example of what's in the definition of each of the classes I need
to use it in.

private const string LAUNCH_TEXT = "Welcome to the Program";
private const string QUIT_TEXT = "Goodbye";

Can this be put into it's own file and then "included" with each class I
want to use it in? What would be the proper way? Thanks!
Nov 16 '05 #1
6 1919
ESPN Lover (happy 25th),

What you would do in this situation is to make the constants public, and
then reference the assembly from other assemblies where you want to use it.
Then, you would just have to use your type name and the constant name, like
so:

// In the assembly that has the value defined.
public class Values
{
public const string Value1 = "Value1";
}

// In the assembly in a class where you want to get Value1 and have a
reference to where Values is kept.
string value1 = Values.Value1;

Also, if you want to expose these values, you should abide by the naming
conventions for publically accessible members.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"ESPN Lover" <es**@lover.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm fairly new to the whole .NET and OOP programming so bear with me.

In the past if I wanted to have constants defined that I could use in my
code, I'd include them either the file or as an include file. But with
.NET
and OOP, there has to be a different way than just including them in each
of
the classes I want to utilize them in.

Here's an example of what's in the definition of each of the classes I
need
to use it in.

private const string LAUNCH_TEXT = "Welcome to the Program";
private const string QUIT_TEXT = "Goodbye";

Can this be put into it's own file and then "included" with each class I
want to use it in? What would be the proper way? Thanks!

Nov 16 '05 #2
ESPN,
Due to Encapsulation I would put the constants specific to a class in that
class, if a constant was shared between classes, then I consider putting
them in a base class. If the "constants" happen to be integers, I would
consider creating an Enum for related constants...

Is LAUNCH_TEXT the same for each class, or does it vary?

If LAUNCH_TEXT varies then I would consider a polymorphic constant function
(or readonly property).

The base class would declare LAUNCH_TEXT as an abstract function or readonly
property, each derived class would implement the function or property
returning a literal.

The base class could then use LAUNCH_TEXT much like a constant.

If LAUNCH_TEXT is the same, then I would use a base class or a "static"
class that groups related constants...

Hope this helps
Jay

"ESPN Lover" <es**@lover.com> wrote in message
news:%2***************@TK2MSFTNGP11.phx.gbl...
I'm fairly new to the whole .NET and OOP programming so bear with me.

In the past if I wanted to have constants defined that I could use in my
code, I'd include them either the file or as an include file. But with
.NET
and OOP, there has to be a different way than just including them in each
of
the classes I want to utilize them in.

Here's an example of what's in the definition of each of the classes I
need
to use it in.

private const string LAUNCH_TEXT = "Welcome to the Program";
private const string QUIT_TEXT = "Goodbye";

Can this be put into it's own file and then "included" with each class I
want to use it in? What would be the proper way? Thanks!

Nov 16 '05 #3
Wes
Hello ESPN,

You could create, for example, another class called constants.

public class Constants
{
public const string LAUNCH_TEXT = "Welcome to the Program";
public const string QUIT_TEXT = "Goodbye";
}

Then wherever you want to use them just use Constants.LAUNCH_TEXT, etc. Also if you want to be sure that no can create an instance of this class for .Net Framework 1.1 and lower just add a dummy private constructor and mark the class sealed, but in .Net Framework 2.0 there is a static keyword to handle this.

HTH
Wes Haggard
http://weblogs.asp.net/whaggard/
I'm fairly new to the whole .NET and OOP programming so bear with me.

In the past if I wanted to have constants defined that I could use in
my code, I'd include them either the file or as an include file. But
with .NET and OOP, there has to be a different way than just including
them in each of the classes I want to utilize them in.

Here's an example of what's in the definition of each of the classes I
need to use it in.

private const string LAUNCH_TEXT = "Welcome to the Program"; private
const string QUIT_TEXT = "Goodbye";

Can this be put into it's own file and then "included" with each class
I want to use it in? What would be the proper way? Thanks!


Nov 16 '05 #4
Nic, thanks! This is what I was looking for :-)

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.com> wrote in
message news:up**************@TK2MSFTNGP09.phx.gbl...
ESPN Lover (happy 25th),

What you would do in this situation is to make the constants public, and then reference the assembly from other assemblies where you want to use it. Then, you would just have to use your type name and the constant name, like so:

// In the assembly that has the value defined.
public class Values
{
public const string Value1 = "Value1";
}

// In the assembly in a class where you want to get Value1 and have a
reference to where Values is kept.
string value1 = Values.Value1;

Also, if you want to expose these values, you should abide by the naming conventions for publically accessible members.

Nov 16 '05 #5
-----Original Message-----
ESPN Lover (happy 25th),

What you would do in this situation is to make the constants public, andthen reference the assembly from other assemblies where you want to use it.
Then, you would just have to use your type name and the constant name, like
so:

// In the assembly that has the value defined.
public class Values
{
public const string Value1 = "Value1";
}

// In the assembly in a class where you want to get Value1 and have areference to where Values is kept.
string value1 = Values.Value1;

Also, if you want to expose these values, you should abide by the namingconventions for publically accessible members.


If this assembly was of a large size with lots of code
and the constants were needed in more than one assembly
would this approach be feasible..?

Can a class such as Features for example be more
suitable here which would detail the characteristic
features of a dog with constants such as
HearingInDecibels,SmellFromDistance etc

I would appreciate your thoughts as this would broaden my
perspective of thinking in code
Nov 16 '05 #6
hi,

You need to be beware of one thing though. If the constant is defined in
another assembly, the actual constant value gets embedded in the assembly
using the constant. So if you were to change the constant to some other
value, it wouldn't get reflected in the other assembly, unless you recompile
it.
Nov 16 '05 #7

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

Similar topics

7
1920
by: mcdonamw | last post by:
This may sound like a stupid stupid question and I figure it would b more "general" than pertaining to a specific Language. I'm using vb.net and I have a bunch of Const values in my program. can...
19
2761
by: Christian Engstrm | last post by:
If you have a function that returns something by value, the gcc compiler (version 3.2.3 on Windows XP with MinGW) converts the returned value from the type you specify in the code, to the const...
23
6537
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
4
2240
by: Rayer | last post by:
I have got some project src, and find it out that there is no ppl using "const" to define a constance, but using #define still more. Discussing with my friend, he said 1. #define is much easier in...
7
4345
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
2
2484
by: Pavel | last post by:
I am writing software for an embedded application and here is the question. GCC would emit data declared like const char text = "abc"; to .rodata (i.e. "read only data") section. I can put this...
4
11691
by: lada77 | last post by:
All, Just wondering if one of you very helpful guru's out there could comment on some odd behaviour I am seeing. I'm betting it is something obvious but I am not experienced enough to tell...
1
4568
by: C. Jayachandran | last post by:
I've inherited some code which has const std::string values defined in a header file, like const std::string str = "foo"; This causes a large amount of bloat, as all the compilation units...
15
2085
by: Jiří Paleček | last post by:
Hello, I know the rules for const handling in C++, but I'd like to ask what is the "right" way to use them, eg. when is it appropriate to make a member function const? This came across this...
0
7391
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...
1
7054
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...
0
7501
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...
0
5633
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 projectplanning, coding, testing,...
1
5056
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...
0
4713
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...
0
3188
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
768
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
424
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...

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.