1

I'm searching a method for setting dynamically the values of a list of variables whose names and values are readed from a database.

I've found a lot of code for getting the variable name and value, but nothing that works for setting the value.

I'll try to explain myself better. Think to have a database that contains two columns, say "name" and "value". Say for example that you have two records:

1) Name="string1", Value="message1" 2) Name="string2", Value="message2"

I have the code to read the two records, what i want is a way to take dinamically the names of the variables from the records and assign to them the corresponding values.

The code is this:

SqlCommand cmd = new SqlCommand("Select Name, " + lng + " from dbo.traductions", Global.languageconn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    var v1 = dr["Name"].ToString();
    var v2 = dr[lng].ToString();

    //Something here to assign the value stored in v2 to the variable whose name is stored in v1

}

Thank you all

Jakube
  • 3,353
  • 3
  • 23
  • 40
Massimo P.
  • 55
  • 1
  • 10
  • Why not just create a class with fields for "name" and "value", and read your SQL data into there? – C. Helling Jul 06 '17 at 13:27
  • Because my variables in a second moment will become components' names. For example in the database i'll have records like this: Name="textbox1", Value1="Insert your name", Value2="entrez votre nom", Value3="geben Sie Ihren Namen", Value4="Inserisci il tuo nome" and, depending on which value i'll read, the corresponding component has to take that value (message in this example). So i need a piece of code that allow me to refer to textbox1 passing from a variable that contain "textbox1". So I need something that do it dinamically, depending on the query result from the db. – Massimo P. Jul 06 '17 at 13:41
  • Or you could have a language flag on those records, include a language field in the class, and then select the appropriate object accordingly. – C. Helling Jul 06 '17 at 13:42
  • Yes, at the beginning I thought to this solution, but my boss wants me to do this using a database in the way I've explained, so i have no choice :( – Massimo P. Jul 06 '17 at 13:49
  • For example, to read a value you can use something like this: var fieldValue = source.GetType().GetField(field).GetValue(source); I need something to do the opposite, something to set the value. – Massimo P. Jul 06 '17 at 13:52
  • It sounds like you end up with v1 = "textbox1", v2 = "Insert your name", and what you want is `var textbox1 = "insert your name";`. Is that correct? – C. Helling Jul 06 '17 at 13:53
  • Quite correct: the result must be something equivalent to the instruction textbox1.Text = "insert your name"; (the common c# instruction to set a textbox property) with the difference that i can't write directly textbox1, because one time it could be textbox1, the next time the user wants to change 3 different components, the next time 5 more components. So i need a single instruction that allow me to pass "textbox1" and the other names as a parameter – Massimo P. Jul 06 '17 at 13:57
  • Please see: https://stackoverflow.com/a/20857916/ – C. Helling Jul 06 '17 at 14:22

2 Answers2

1

I don't know why you would want to do this but why not just use a dictionary?

SqlCommand cmd = new SqlCommand("Select Name, " + lng + " from dbo.traductions", Global.languageconn);
SqlDataReader dr = cmd.ExecuteReader();
var variables = new Dictionary<string, string>();
while (dr.Read())
{
    var v1 = dr["Name"].ToString();
    var v2 = dr[lng].ToString();
    variables.Add(v1, v2);
}

Then to use them:

var x= variables[v1];

Update:

I see you added a note saying you need to refer to objects. You could change the Dictionary to hold objects instead. You didn't mention the type so for argument sake I will assume they are all text boxes:

SqlCommand cmd = new SqlCommand("Select Name, " + lng + " from dbo.traductions", Global.languageconn);
    SqlDataReader dr = cmd.ExecuteReader();
    var variables = new Dictionary<string, object>();
    while (dr.Read())
    {
        var v1 = dr["Name"].ToString();
        var v2 = dr[lng].ToString();
        var textbox = new TextBox() { Text = v2 };
        variables.Add(v1, textbox);
    }

Then to use them by name:

var textbox= variables[v1];
JuanR
  • 7,405
  • 1
  • 19
  • 30
0

Based on your comments, you want to access "controls by name"; this is accomplished with Controls.Find():

Control ctl = this.Controls.Find(v1, true).FirstOrDefault();
if (ctl != null && ctl is TextBox)
{
    TextBox tb = (TextBox)ctl;
    tb.Text = v2;
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40