Home > front end >  C# How to get variable value through concated string?
C# How to get variable value through concated string?

Time:08-29

i am switching from different programing language and i would like to ask if this approach is correct, or if i should use different method.

Simple example: I have set of variables with values, and based on user input i want to use concated string to read value from correct variable.

int userSelection;
int finalPointValue;

int pointID_1 = 10;
int pointID_2 = 3;
int pointID_3 = 80;
...

// read user selection
...
// obtain value of selected variable
finalPointValue = "pointID_"  userSelection;

So if user select 3 then final value would be 80, and so on (i am really trying to simplify this example).

I did not find straight answer on internet, but i get this code to do the work (bit different to the example but concept is the same):

using System.Reflection;

public class Form1
{
  public string langID;
  public static string sText_EN = "Text in english";
  public static string sText_SK = "Text in slovak";

  // some code to obtain "EN" or "SK" in langID
  ...

  private void ChangeLanguage()
  {
    lblText.Text = Convert.ToString(typeof(Form1).GetField("sText_"  langID).GetValue(null));
  }
}

I mean, this code works for me, but it looks like this approach is not commonly used (or at least i had quite difficul time to found this solution).

So, is there any other way how to directly read value from variable with concated string? Or do you use different method for this kind of situations?

Thanks in advance.

CodePudding user response:

Instead of resorting to reflection you can store your language options in a Dictionary and use the user input as the key to access the language ID's corresponding value.

var options = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
{
    { "EN", "Text in English" },
    { "SK", "Text in Slovak" }
};

// Read the users language ID selection.
var langID = Console.ReadLine();
if (options.TryGetValue(langID, out var value))
{
    Console.WriteLine(value);
}

CodePudding user response:

Since the input is an integer value you can use it to index an array. This is very fast and simple.

//      index =        0           1  2   3
int[] pointId = { 0 /* unused */, 10, 3, 80 };

int finalPointValue = pointId[userSelection];

Array indices are 0-based, so I inserted a dummy value at the beginning of the array since the user inputs start at 1. If the user selection was starting at a higher number, (e.g., 100, 101, 102), then you would just subtract this offset.

int[] pointId = { 10, 3, 80 };

int finalPointValue = pointId[userSelection - 100];

This works because the user inputs are in sequence with no holes. If they were arbitrary values or strings for example, you could use a dictionary.

Example:

Dictionary<string, int> pointId = new() {
    ["one"] = 10,
    ["two"] = 3,
    ["three"] = 80
};
string input = Console.ReadLine();
if (pointId.TryGetValue(input, out int id)) {
    Console.WriteLine($"The Id of point {input} is {id}");
}

A dictionary lookup is not as fast as indexing an array; however, it is an O(1) operation. I.e., the speed does not depend on the size of the dictionary.

  • Related