Click here to Skip to main content
15,887,746 members
Home / Discussions / C#
   

C#

 
QuestionWhat is best way to declare constants set? Pin
JP_Rocks27-Jul-11 19:26
JP_Rocks27-Jul-11 19:26 
AnswerRe: What is best way to declare constants set? Pin
lukeer27-Jul-11 21:13
lukeer27-Jul-11 21:13 
AnswerRe: What is best way to declare constants set? Pin
Rob Philpott27-Jul-11 23:22
Rob Philpott27-Jul-11 23:22 
AnswerRe: What is best way to declare constants set? Pin
Dave Kreskowiak28-Jul-11 2:20
mveDave Kreskowiak28-Jul-11 2:20 
AnswerRe: What is best way to declare constants set? Pin
PIEBALDconsult28-Jul-11 2:51
mvePIEBALDconsult28-Jul-11 2:51 
AnswerRe: What is best way to declare constants set? Pin
Eddy Vluggen28-Jul-11 6:52
professionalEddy Vluggen28-Jul-11 6:52 
AnswerRe: What is best way to declare constants set? Pin
RobCroll28-Jul-11 13:53
RobCroll28-Jul-11 13:53 
AnswerRe: What is best way to declare constants set? Pin
BillWoodruff28-Jul-11 18:09
professionalBillWoodruff28-Jul-11 18:09 
JP_Rocks wrote:
I cannot change from CountryRequests class to collection as it is used by multiple projects.
One question: is the CountryRequests class in a library that is now being compiled, and which the other projects are now using by embedding a reference to the compiled class ? In other words, are all the projects using CountryRequests under your source-control scope ?

I ask this because of the fact that the compiler in-lines const types where they are referenced, and unless both the CountryRequests class, and all other projects using it are re-compiled together, a client class may persist an out of date value[^].

The idea of using Reflection to pre-compile ... say by using 'Emit ... a "hard-coded" look-up to replace a big Switch statement might be worthwhile if these constant values were being accessed with great frequency, but I doubt that's the case here.

Another idea, based on the constraint that you can't touch 'CountryRequests," would be to create a kind of "singleton" class that effectively "extends" it through inheritance.

disclaimer: experimental: this code tested quickly in VS 2010 Pro, .NET 4.0 Client FrameWork; I have no idea what the implications of using this type of inherited class with call to a private (internal) constructor done by a public static method in a variation on the "singleton" technique, and using a mix of static and public classes might mean in a real-world use case of multi-threaded environment, remoting, multiple consumers of the class, etc.:
C#
public class CountryRequestsServer : CountryRequests
{
    internal static CountryRequestsServer instanceCRServer;

    internal static Dictionary<string, string> countryLookUp;

    internal CountryRequestsServer()
    {
        countryLookUp = new Dictionary<string, string>()
        {
            {"JAP", JAP_ABC},
            { "USA", US_POP},
            { "UK", ENG_POP}
        };
    }

    public static string getCountry(string request)
    {
        // CTor called here if null
        if(instanceCRServer == null)
        {
            instanceCRServer = new CountryRequestsServer();
        }

        // handle lookup error
        return countryLookUp.ContainsKey(request) 
        ? 
            countryLookUp[request] 
        : 
            "No Match: Input = " + request;        
    }
}
Tested like this:
string myCountryRequest = CountryRequestsServer.getCountry("JAP");
// ==> "JAP_012686"
good luck, Bill
"In the River of Delights, Panic has not failed me." Jorge Luis Borges

GeneralRe: What is best way to declare constants set? Pin
PIEBALDconsult29-Jul-11 3:01
mvePIEBALDconsult29-Jul-11 3:01 
AnswerRe: What is best way to declare constants set? Pin
BobJanova29-Jul-11 5:31
BobJanova29-Jul-11 5:31 
QuestionC# and SQL Server :: Find Most Common Via Junction Table Pin
Matt U.27-Jul-11 14:25
Matt U.27-Jul-11 14:25 
AnswerRe: C# and SQL Server :: Find Most Common Via Junction Table Pin
Łukasz Nowakowski27-Jul-11 23:28
Łukasz Nowakowski27-Jul-11 23:28 
QuestionHow to close a file? Pin
Member 806979527-Jul-11 8:09
Member 806979527-Jul-11 8:09 
AnswerRe: How to close a file? Pin
Ennis Ray Lynch, Jr.27-Jul-11 9:07
Ennis Ray Lynch, Jr.27-Jul-11 9:07 
AnswerRe: How to close a file? Pin
PIEBALDconsult27-Jul-11 14:44
mvePIEBALDconsult27-Jul-11 14:44 
GeneralRe: How to close a file? Pin
Member 806979529-Jul-11 4:29
Member 806979529-Jul-11 4:29 
GeneralRe: How to close a file? Pin
PIEBALDconsult30-Jul-11 5:18
mvePIEBALDconsult30-Jul-11 5:18 
AnswerRe: How to close a file? Pin
jjdacl28-Jul-11 16:46
jjdacl28-Jul-11 16:46 
QuestionDbDataReader: Matching fields to order or occurrence? Pin
Goalie3527-Jul-11 4:14
Goalie3527-Jul-11 4:14 
AnswerRe: DbDataReader: Matching fields to order or occurrence? Pin
dasblinkenlight27-Jul-11 4:26
dasblinkenlight27-Jul-11 4:26 
AnswerRe: DbDataReader: Matching fields to order or occurrence? Pin
Ennis Ray Lynch, Jr.27-Jul-11 5:41
Ennis Ray Lynch, Jr.27-Jul-11 5:41 
AnswerRe: DbDataReader: Matching fields to order or occurrence? Pin
PIEBALDconsult27-Jul-11 14:45
mvePIEBALDconsult27-Jul-11 14:45 
AnswerRe: DbDataReader: Matching fields to order or occurrence? Pin
Eddy Vluggen28-Jul-11 9:49
professionalEddy Vluggen28-Jul-11 9:49 
GeneralRe: DbDataReader: Matching fields to order or occurrence? Pin
PIEBALDconsult28-Jul-11 13:57
mvePIEBALDconsult28-Jul-11 13:57 
Questiondeveloping program Pin
albertodiprima27-Jul-11 4:08
albertodiprima27-Jul-11 4:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.