Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
//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.


What I have tried:

Using VS 2019 C#:
The labels display only the string (language0[0]) NOT array's value. It should pick up item from selected language array
Posted
Updated 1-Aug-21 9:50am
Comments
Adérito Silva 31-Jul-21 22:52pm    
Your code is working exactly as you telling it to do. You're basically telling it to display the value of 'lang' plus the value 'selectedLang' plus "[0]". It would output exactly "language0[0]".

To get the value from the language array, you would need to access a specific array value, like language0[2], which would output "Previous".

I'm not sure if I understood what you meant.
The Other John Ingram 31-Jul-21 22:58pm    
this is not how you index into an array.

i would use a switch statement based of of selectedlang and make lang equal to the selected language you want to use. lang = language0[index] for example.

There are two different "times" in C# computing: Compile time, and run time.

Compile time happens once for each version of you application: the system processes your source code and produces an executable file.
This does not produce any results that a user can see, and the logic of your application isn't involved, just the syntax of the actual code.
If you think about cars instead, compile time is when the manufacturer builds your specific car - until this has happened you can;t drive it!

Run time happens each time you try to execute your program to make it do what the user wants.
This is when all interaction with the user happens, and the logic of your app comes into play.
For cars, run time begins each time you you sit in the driver's seat and start the engine, and ends when the engine stops running.

At compile time, the system uses your variable names.
At run time, your variables names are irrelevant and even forgotten because your user can never see them!

So if you try to build a variable name as a string and use it at run time to access a variable, that won't work.
Instead, use a 2D array, and access the languages from that:
C#
string[,] languages =  {{ "Exercise 1", "Next", "Previous" },
                        { "Övning 1", "Nästa", "Förgående" },
                        { "blabla 1", "bla", "bla" } };
const int English = 0;
const int Swedish = 1;
const int Blah = 2;

Then you can assess the strings like this:
C#
Console.WriteLine(languages[English, 0]);
Console.WriteLine(languages[Swedish, 0]);
Console.WriteLine(languages[Blah, 0]);

Or even like this:
C#
for (int lang = 0; lang < languages.GetLength(0); lang++)
    {
    for (int str = 0; str < languages.GetLength(1); str++)
        {
        Console.WriteLine(languages[lang, str]);
        }
    }
 
Share this answer
 
This will not work
C#
String lang = "language" + selectedLang.ToString();

Here, you concatenate 2 stings, there is no magic to transform the string into a variable name.
The easiest is probably to use a if-else structure to choose the variable of the language.

Try to replace
C#
label1.Text = lang+"[0]"; // Want to display first item of selected array.

with
C#
String lang = language0;
label1.Text = lang[0];


Advice: Learn seriously the language, the time spend now in learning will be recovered largely later.
 
Share this answer
 
v2
Thanks for your input. I fount a solution by creating dictionary:
<pre>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]);
 
Share this answer
 
Comments
Adérito Silva 1-Aug-21 16:36pm    
Usually, using dictionaries is best for languages. However, the whole language system of an application should be a bit more powerful than a simple dictionary. It is normal for a language to be represented by a culture in .NET, instead of a name, and a culture to be represented by a numeric code or a standard string pattern representation. A language has more than simple literal sentences, it has specific rules for how numbers, dates and time are represented, as well as singular and plural rules. It would be better to create your own Language class and a specialized collection for creating collections of that class, which could use a Dictionary or a HashSet internally.

Also, a dictionary of string keys needs attention to culture and casing for the comparison of keys. You would want a dictionary that behaves the same way independently of the current thread culture or UI culture. For example, in a simple dictionary use like in your example, I would specify a StringComparer in its constructor to avoid using the default comparer.
Richard Deeming 2-Aug-21 5:30am    
Since your selectedLang is an integer, why not use integers for your keys?
var translations = new Dictionary<int, string[]>
{
    [0] = { "Exercise 1", "Next", "Previous" },
    [1] = { ... },
    [2] = { ... },
};

Console.WriteLine(translations[1][2]);

If the index values are sequential with no breaks, you could also use a List<string[]>, or even a string[][] or string[,].

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900