Get Key Value Pair from a Class

Jassim Al Rahma 1,616 Reputation points
2022-11-26T20:43:01.55+00:00

Hi,

I am currently having this in my App.xaml.cs:

internal const string TargetAddress = "www.google.com";  
internal const string TargetAddress_ae = "www.google.ae";  
internal const string TargetAddress_uk = "www.google.uk";  
internal const string TargetAddress_sa = "www.google.sa";  
........................  
........................  
........................  

but I want t get rid of the above from the App.xaml.cs and move it to a separate class with a key value pair like this:

var country = new List<KeyValuePair<string, int>>()   
{   
      new KeyValuePair<string, string>("X", "www.google.com"),  
      new KeyValuePair<string, string>("ae", "www.google.ae"),  
      new KeyValuePair<string, string>("uk", "www.google.uk"),  
      new KeyValuePair<string, string>("us", "www.google.us"),  
};  

then based on the device geo country (ISO) I want to pick up the correct value from the above.

If the device country ISO is null then I want the return the X value which is www.google.com

How can I achieve that, please?

Thanks,
Jassim

Developer technologies | C#
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2022-11-26T20:59:12.337+00:00

    Try something like this:

    private static Dictionary<string, string> data = new Dictionary<string, string>  
    {  
        {"X", "www.google.com"},  
        {"ae", "www.google.ae"},  
        {"uk", "www.google.uk"},  
        {"us", "www.google.us" },  
    };  
      
    public static string GetValue( string country )  
    {  
        if( country == null ) country = "X";  
      
        return data[country];  
    }  
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.