[RESOLVED] how to parameter the name of the property of a variable
how to parameter the name of the property of a variable
I have this code for example:
if (drpDefaultLanguage.SelectedValue == "EN") then
lstKeyWords.Items.Add(txtKeywordEN.Text);
else if (drpDefaultLanguage.SelectedValue == "FR") then
lstKeyWords.Items.Add(txtKeywordEN.Text)
else if (drpDefaultLanguage.SelectedValue == "ES") then
lstKeyWords.Items.Add(txtKeywordES.Text)....etc
I need to put a variable called mySuffix which will hold one of the values : EN, FR, ES
So that instead of accessing the french value using: txtKeywordEN.Text it will be something that looks like: txtKeywordmySuffix.Text. So mySuffix is not a litteral but a variable itself.
This will allow me not to write the same code above: if else if else if ...... for as many langages as i have. Sometimes the total languages can be few dozens and that d be a lot of typing to hard code the loops like that.
Is that possible pls and how
Thanks
Re: how to parameter the name of the property of a variable
Code:
Controls("txtKeyword" + mySuffix).Text
Re: how to parameter the name of the property of a variable
that s cool amigo thank thee :)
Re: [RESOLVED] how to parameter the name of the property of a variable
I would suggest not using the names like that. Store the controls in a Dictionary keyed on the suffix:
vb Code:
Dim myDictionary As New Dictionary(Of String, TextBox)
myDictionary.Add("EN", Me.txtKeywordEN)
myDictionary.Add("FR", Me.txtKeywordFR)
myDictionary.Add("ES", Me.txtKeywordES)
You can then simply do this:
vb Code:
lstKeyWords.Items.Add(myDictionary(CStr(drpDefaultLanguage.SelectedValue)).Text)
Re: [RESOLVED] how to parameter the name of the property of a variable
yeah that s true, may be better performance as well.
Thanks jmcilhinney