const string values

E

ESPN Lover

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!
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
J

Jay B. Harlow [MVP - Outlook]

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
 
W

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/
 
T

tribal

-----Original Message-----
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.



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
 
G

Guest

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top