Resolved Cannot display Array's item

Sofi0813

Member
Joined
Jul 25, 2021
Messages
7
Programming Experience
1-3
The labels display only the string (language0[0]) NOT array's value. It should pick up item from selected language array.
//Language array:
string[] language0 = { "Exercise 1", "Next", "Previous"};
string[] language1 = { "Övning 1", "Nästa", "Förgående"};
string[] language2 = { "blabla 1", "bla", "bla"};

int selectedLang = Dashboard.quantity;// selected language coming from Dashboard form. It is integers for ex. 0,1,2....
public void LanguageSetting()
{
String lang = "language" + selectedLang.ToString(); //make array name depending on selected language ex. language0, language1, language2
label1.Text = lang+"[0]"; // Want to display first item of selected array.
label2.Text = lang+"[1]"; //Want to display second item of selected array.
label3.Text = lang+"[2]"; //Want to display 3rd item of selected array.
}
//The labels display only the string (language0[0]) NOT array's value.
 
Solution
Sorry, this forum is not a code writing service.

This should help you get started:
C#:
var translations = new Dictionary<string, string[]>()
{
    ["language0"] = new string[] { "Exercise 1", "Next", "Previous" },
    ["language1"] = new string[] { "Övning 1", "Nästa", "Förgående" },
    ["language2"] = new string[] { "blabla 1", "bla", "bla" },
};

Console.WriteLine(translations["language1"][2]);
Why would you expect the code the pick up the value from the array? Variable names are a compile time mnemonic to help developers understand code. Your code it running at run time, not at compiler time. To get to something that was compiled at compile time during runtime, you would need to write code that uses reflection.

You should really use a dictionary of string arrays to do this kind of look up. It will be more efficient, as well as more straight forward, than trying to use reflection.
 
Thanks for your feedback. I have only basic knowledge in programing. I did not really got your feedback. Could write your solution by an example?
 
Last edited:
Sorry, this forum is not a code writing service.

This should help you get started:
C#:
var translations = new Dictionary<string, string[]>()
{
    ["language0"] = new string[] { "Exercise 1", "Next", "Previous" },
    ["language1"] = new string[] { "Övning 1", "Nästa", "Förgående" },
    ["language2"] = new string[] { "blabla 1", "bla", "bla" },
};

Console.WriteLine(translations["language1"][2]);
 
Solution
Back
Top Bottom