shared variables or messages in C#

  • Thread starter Thread starter Ramsin Savra
  • Start date Start date
R

Ramsin Savra

Hi group,

Back to my C and Pascal programming under windows we used to have a
segment or external file to keep common messages (error, warning and ...)!
now what?
how can I have my messages (associated with a project) in a unit or file ?
then I would be able to modifiy just a file instead of the whole messages in
project files ?

thanks in advance
 
You can do the same by having a class that holds all static variables to the messages, errors, etc

public class MessageHolde

public static int Message1 = 10
public static string ErrorMessageXYZ = "Test"
..


Tu-Thac

----- Ramsin Savra wrote: ----

Hi group

Back to my C and Pascal programming under windows we used to have
segment or external file to keep common messages (error, warning and ...)
now what
how can I have my messages (associated with a project) in a unit or file
then I would be able to modifiy just a file instead of the whole messages i
project files

thanks in advanc
 
Ramsin Savra said:
Hi group,

Back to my C and Pascal programming under windows we used to have a
segment or external file to keep common messages (error, warning and ...)!
now what?
how can I have my messages (associated with a project) in a unit or file ?
then I would be able to modifiy just a file instead of the whole messages in
project files ?

thanks in advance

You could create a class whose sole purpose is to hold the messages.

e.g.:

// Messages.cs

namespace MyApp
{

class ErrorMessages
{
public const string SomeError = "Some error has occured.";
//...
}

}

Here we have created a constant *static* string object (constant
members are automatically static in C#).

Now you can use it like this:

namespace MyApp
{

class MainForm
{
public void Foo()
{
if (anErrorOccured) {
MessageBox.Show(ErrorMessages.SomeError);
}
}
}

}

HTH
 
The name wasn't so good...
class ErrorMessages
{
public const string General = "Some error has occured.";
}

MessageBox.Show(ErrorMessages.General);

That's better.
 
In addition to the other replies, you may want to consider writing a class
to load messages from a stream or the resources class and use resources or
an xml or name=value style file you parse yourself to hold your error
messages. It will definatly come in handy if you intend to localize your
code(just modify the file, no code changes). Infact researching localization
will probably turn up the best techniques to use anyway, even if you don't
intend to localize.
 
Back
Top