Vignesh
Vignesh

Reputation: 165

Storing Key value pair in C#

In Java , i can use property file as shown below to store the elements ID and implement as the same

CountryName=USA

driver.findElement(By.name(prop.getProperty("CountryName"))).click();

Is there any such way in c# where i can store the key value pairand retrieve the same while execution.

Upvotes: 2

Views: 2855

Answers (3)

Dennis
Dennis

Reputation: 11

Dictionary can directly provide details by providing KEY as element as shown

Dictionary<string, string> DE = new Dictionary<string, string>();
 DE.Add("ID1", "34 Employees, 50 Computers");
 DE.Add("ID2", "100 Employees, 67 Computers");
 string details = DE["ID1"]; // Key "ID1" shall give the detals as "34 Employees, 50 Computers"

Upvotes: 0

Eric
Eric

Reputation: 5743

In addition to use a Dictionary suggested by SandySands, you can locate the value by the key

var countries = new Dictionary<string, string>()
{
  {"USA", "id_1"},
  {"UK", "id_2"},
};

// locate the value by key: countries[key]
driver.findElement(countries["USA"]).click()

If the type of id is just int, I would recommend to use Enum instead.

Public Enum Country
{
  USA = 1,
  UK = 2,
}

driver.findElement(((int)CountryCountry.USA).ToString()).click()

Upvotes: 0

SanyTiger
SanyTiger

Reputation: 666

You can always use Dictionary

Example:

Dictionary<string, int> intMap = new Dictionary<string, int>();
intMap.Add("One", 1);
intMap.Add("Two", 2);

foreach (KeyValuePair<string, int> keyValue in intMap) 
{
    Console.WriteLine("{0} -> {1}", keyValue.Key, keyValue.Value);
}

Or Simply use this code:

KeyValuePair<string, string> NAME_HERE = new KeyValuePair<string,string>("defaultkey", "defaultvalue");

Upvotes: 2

Related Questions