>How can I define constants that are shared amongst several forms? In the same project, for a start.
>
>For example, I have several forms that access different tables, in the same Access database, so for simplicity, I would like to have a shared constant for a connection string (so that I can quick switch between two different connection strings between home and the academy where I am learning .NET).
>
>TIA,
>
>Hilmar.
Hilmar,
Besides ConfigurationSettings there is of course const class member. You could have your constants in a namespace like:
namespace myApplication
{
class definitions
{
public const string myConStr = "...";
}
}
and refer to it as:
myApplication.definitions.myConStr
from anywhere. However like VFP if you change the value of myConStr, any other classes depending on the value should be recompiled. An alternative is readonly fields.
namespace myApplication
{
class definitions
{
public static readonly string myConStr = "...";
}
}
you again refer to it as:
myApplication.definitions.myConStr
However if value is changed it's only 'definitions' class that you need a recompile (other classes obtain value at runtime despite compile time if it's a field and not a constant). Also with constants and fields you're not limited to simple types and could get the value using a method (and say read from a txt,xml file or ask to user etc). ie:
namespace myApplication
{
class definitions
{
public static readonly DataLocation myDataLoc = new DataLocation();
}
class DataLocation
{
public DataLocation() {...}
}
}
Cetin