简体   繁体   中英

Referencing a string name after equals in VB.NET?

NEW EDITS:

This is what I am trying to avoid:

ViewDetail1.strFirstName = assignValue("strFirstName")
ViewDetail1.strLastName = assignValue("strLastName")
ViewDetail1.strStreet = assignValue("strStreet")
ViewDetail1.strCompany = assignValue("strCompany")

I would rather have the code to this instead, where ?? is the magical way to know the string name:

ViewDetail1.strFirstName = assignValue(??)
ViewDetail1.strLastName = assignValue(??)
ViewDetail1.strStreet = assignValue(??)
ViewDetail1.strCompany = assignValue(?)

And then my function would return value based on my string name that I am trying to assign.

My string names are already defined in my user control like so:

  Public Property strFirstName() As String
    Get
        Return Me._strFirstName
    End Get
    Set(ByVal value As String)
        Me._strFirstName = value
    End Set
End Property

Public Property strLastName() As String
    Get
        Return Me._strLastName
    End Get
    Set(ByVal value As String)
        Me._strLastName = value
    End Set
End Property


' Fields
Private _strFirstName As String
Private _strLastName As String

Where as my function would be something like:

Function assignValue(strValue as string)
     Dim myColumn as string = strValue.Remove(3,0)
     'make call to database 
     strSQL = "SELECT " & myColumn & " FROM myTable WHERE ...." 
       database connections...
       myValue = myReader(myColumn)
      assignValue = myValue
End Function

.

Forgive me if this is a stupid question or if I've used wrong terminology but I'm hoping there is a way to do this. I'm looking for a way to reference the name of the string I am trying to assign value to without having to retype string name.

I am wanting to use a function that will use the name of the string as a variable in function to determine the final value. My string names correlate with datafield column names. I am populating a user control. I'd like to put in the name of string where question marks are below in this function call. Any way to do this without having to retype the string name?

ViewDetail1.myStringNameDataColumnA = assignValue(??)

My string names are already defined in my user control. So in above scenario, I would take the name of the string and run a function that checks that string name and assigns its value based on the actual name of the string (since it correlates to a pre-existing datacolumn name)

Sorry, code is not magic. This is not possible without jumping through serious hoops, to little gain. If code could write itself, there'd be no need for programmers.

ViewDetail1.strFirstName = assignValue(??)
ViewDetail1.strLastName = assignValue(??)
ViewDetail1.strStreet = assignValue(??)
ViewDetail1.strCompany = assignValue(?)

Theoretically, it might be possible to use reflection to load your assembly, look at the value being assigned, and then find the value based on the named of the variable. But that code would be complex, prone to break, and not provide any value over just doing this:

ViewDetail1.strFirstName = assignValue("strFirstName")
ViewDetail1.strLastName = assignValue("strLastName")
ViewDetail1.strStreet = assignValue("strStreet")
ViewDetail1.strCompany = assignValue("strCompany")

Perhaps you could parse it into a view model , and pass the view model around (but you'll still have code similar to ViewDetail1.strFirstName = assignValue("strFirstName") when assigning properties of the view model).

The end result might look something like this.

ViewDetail1.LoadDetails(viewModel)

From your question, I believe you are looking for a Dictionary(Of String, String) .

Dim fields As New Dictionar(Of String, String)()

fields.Add("CustomerName", "Fred Smith")

Dim name As String = fields("CustomerName")

If that isn't what you're looking for, you may need to clarify your question and add additional details.

EDIT:

For getting the "CustomerName" string, see my answer here: How to get the Name of an application setting in C#? . This uses some sleight of hand to get the name of a property. Actually, since it is in C#, I'll duplicate it here:

Calling:

Dim name As String = fields(GetPropertyName(Function() ViewDetail1.myStringName))

The magic routine:

Public Shared Function GetPropertyName(Of T)(expression As Expression(Of Func(Of T))) As String
    Dim body As MemberExpression = DirectCast(expression.Body, MemberExpression)
    Return body.Member.Name
End Function

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM