Results 1 to 5 of 5

Thread: [RESOLVED] how to parameter the name of the property of a variable

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    589

    Resolved [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
    Thanks a lot for your help.

  2. #2
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091

    Re: how to parameter the name of the property of a variable

    Code:
    Controls("txtKeyword" + mySuffix).Text
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    589

    Re: how to parameter the name of the property of a variable

    that s cool amigo thank thee
    Thanks a lot for your help.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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:
    1. Dim myDictionary As New Dictionary(Of String, TextBox)
    2.  
    3. myDictionary.Add("EN", Me.txtKeywordEN)
    4. myDictionary.Add("FR", Me.txtKeywordFR)
    5. myDictionary.Add("ES", Me.txtKeywordES)
    You can then simply do this:
    vb Code:
    1. lstKeyWords.Items.Add(myDictionary(CStr(drpDefaultLanguage.SelectedValue)).Text)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    589

    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
    Thanks a lot for your help.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width