Search code examples
c#wpfstringcsvstreamreader

HOW TO work with a variable, that is saved in a string content (in C#, WPF)


I've got a for loop to read line by line of a stream reader. This works very well. Now I've problems with reading the file of the stream reader. It's a csv file that contains two columns with information. The first one (A) contains a C# element like a textbox or a label (just like "label_1_title"), which is declared in my project. The second one (B) contains a normal simple string like "Hello".

Now I want to convert the string content of column A to the real existing element. Example: I've got "label_1_title" written in column A and that's an element, that exists in my project. Now I want to use this element to (for example) change it's content to the content of the column B of that line.

public void SetSprachpaket(string Sprachpaket)
{
    StreamReader StreamReader = new StreamReader(Sprachpaket, Encoding.UTF8);
    string Inhalt = StreamReader.ReadLine();

    for (int i = 1; Inhalt != null; i++)
    {
        var Items = Inhalt.Split(new Char[] { ';' });

        object Element = Items[0].GetType(); // Convert the string content of Items[1] to the existing object
        Element = Items[1]; // Take this existing object and give it the string content of Items[2]

        Inhalt = StreamReader.ReadLine();
    }
    StreamReader.Close();
}

I hope you can help me. Thanks in advance. Kind regards.

EDIT:

object Element = Items[0].GetType(); // Get (let's say) the string "myString"
Element = Items[1]; // --> myString = ...

Solution

  • Simply by using foreach

    foreach(control x in this.Control)
    {
        if(x.Name == A)  // For A means the name of the object
        { x.Text = B; }  // For B means the string
    }