I'm a newbie at programming. I have this php code, it's basically used for translating some sentences from txt files:
$LANG = array();
$LANG['en'] = array(
11 => "Name",
20 => "Surname",
21 => "Age",
22 => "Profession",
);
$LANG['es'] = array(
11 => "Nombre",
20 => "Apellido",
21 => "Edad",
22 => "Profesión",
);
I'm trying to do the same in c#, something like this:
Dictionary<int, string>[] LANG = new Dictionary<int, string>[]
{
new Dictionary<int, string>(),
LANG['en']=new Dictionary<int, string>()
{
{11, "Name"},
{20, "Surname"},
{21, "Age"},
{22, "Profession"}
},
LANG['es']=new Dictionary<int, string>()
{
{11, "Nombre"},
{20, "Apellido"},
{21, "Edad"},
{22, "Profesión"}
}
};
1) Is this c# doing exactly the same as the php code? Should i do an array of dictionaries, or should i do a dictionary of dictionaries?
2) For LANG['en']
and LANG['es']
, i'm getting the error message "A field initializer cannot reference the non-static field, method or property 'Form1.LANG'. What am i doing wrong?
1) Yes. PHP
associative array equivalent in C#
would be a Dictionary
. Array in C#
doesn't support non-integer indexes so you need to use Dictionary
with string
key and therefore you'll have to use dictionary of dictionaries.
2) Apparently the syntax is invalid. To turn your PHP
code into C#
do the following
//instantiate dictionary of dictionaries
var LANG = new Dictionary<string, Dictionary<int, string>>();
//set dictionary for "en" key
LANG["en"] = new Dictionary<int, string>()
{
{ 11, "Name" },
{ 20, "Surname" },
{ 21, "Age" },
{ 22, "Profession" }
};
//set dictionary for "es" key
LANG["es"] = new Dictionary<int, string>()
{
{ 11, "Nombre" },
{ 20, "Apellido" },
{ 21, "Edad" },
{ 22, "Profesión" }
};