473,804 Members | 3,380 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 1937
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.co m

"ESPN Lover" <es**@lover.com > wrote in message
news:%2******** *******@TK2MSFT NGP11.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******** *******@TK2MSFT NGP11.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.LAUNC H_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.c om> wrote in
message news:up******** ******@TK2MSFTN GP09.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
HearingInDecibe ls,SmellFromDis tance 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
1940
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 use them obviously by placing their names in place of values, henc the ideal behind using Const. My question is... is there a way to get the actual Variable name for specific value that is returned to me?
19
2808
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 version of that type. Is this a bug that is specific to gcc, or is it a flaw in the language specification that gcc diligently implements? For example, the below program produces the output Constant Mutable
23
6647
by: Hans | last post by:
Hello, Why all C/C++ guys write: const char* str = "Hello"; or const char str = "Hello";
4
2254
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 modify values in program for several propose, and 2. const takes memory but #define not. However, most modern textbook (for example, C++ Primal Plus 4/e) still suggest to use const to replace #define. I know that use #define will have some...
7
4368
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 type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
2
2503
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 section to flash memory and that would be OK. I have a structure with one member of it being const char** array;
4
11736
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 right away. Here is my code snippet and the results that I am seeing: #include <map> #include <iostream> int
1
4609
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 including this header file will have a copy of the string, as well as code to construct and destruct the string, even if the string is not used within the CPP file.
15
2148
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 question when I was thinking about implementation of a class implementing some lazy data structure or cache. The C++ rules allow among other possibilities making all member functions non-const, or making all member functions const and all members...
0
9706
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, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
10575
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...
1
10319
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
9144
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, and deploymentwithout 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...
0
5520
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
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4297
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
3816
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
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.