none
Table and Column Name Replacement by variables. RRS feed

  • Question

  • In the line below I am using the table, "lkpDepartment" and the column, "chrDepartment". I would like to replace their names with variables.  I already know that a string or object type variable won't work.  What kind of variable can I use to replace the names?

    .txtName.DataBindings.Add(New Binding("Text", frmChangeRequest._MasterBase_1_1DataSet, "lkpDepartment.chrDepartment"))


    gwboolean

    Tuesday, July 3, 2018 5:06 PM

Answers

  • The parameter type is String so why not string variables?

    Dim lkpVar As String
    Dim chrVar As String
    
    .txtName.DataBindings.Add(New Binding("Text", frmChangeRequest._MasterBase_1_1DataSet, $"{lkpVar}.{chrVar}"))
    (if you are using pre VS2015 just use normal string concatenation instead of the $"{}" syntax)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Tuesday, July 3, 2018 7:17 PM
    Moderator

All replies

  • The parameter type is String so why not string variables?

    Dim lkpVar As String
    Dim chrVar As String
    
    .txtName.DataBindings.Add(New Binding("Text", frmChangeRequest._MasterBase_1_1DataSet, $"{lkpVar}.{chrVar}"))
    (if you are using pre VS2015 just use normal string concatenation instead of the $"{}" syntax)


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"


    Tuesday, July 3, 2018 7:17 PM
    Moderator
  • Maybe try NameOf(frmChangeRequest._MasterBase_1_1DataSet.lkpDepartment) & "." & NameOf(frmChangeRequest._MasterBase_1_1DataSet.lkpDepartment.chrDepartment).


    Tuesday, July 3, 2018 7:58 PM
  • Reed,

    Ahhh, Bach!

    That works perfectly.  So can you point me to where I might understand what I just did that worked so well?

    Viorel

    I can kind of see what you are doing.  However, I get a syntax error with the NameOf.  and was unable to figure out how to make it work.

    Thanks!!


    gwboolean

    Tuesday, July 3, 2018 9:25 PM
  • If you want to get rid of strings like “chrDepartment”, where you can easily make a mistake, and which becomes invalid when you rename the variable or members, but forget to adjust the string, in some cases you can use NameOf(variable): https://msdn.microsoft.com/en-us/magazine/dn890368.aspx. It depends on details about your program.


    Wednesday, July 4, 2018 6:29 AM
  • Reed,

    Ahhh, Bach!

    That works perfectly.  So can you point me to where I might understand what I just did that worked so well?


    I think this is what you are asking for?  If not, please let me know and I'll try to find something more relevant.

    https://msdn.microsoft.com/en-us/magazine/dn890368.aspx

    Item #12 (String Interpolation) in the above linked article.


    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, July 4, 2018 1:08 PM
    Moderator
  • Oops, sorry Viorel, just realized we linked the same article. :)

    Reed Kimble - "When you do things right, people won't be sure you've done anything at all"

    Wednesday, July 4, 2018 1:11 PM
    Moderator