简体   繁体   English

在VB.NET中等于引用字符串名称?

[英]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). 也许您可以将其解析为视图模型 ,并传递视图模型(但在分配视图模型的属性时,您仍将拥有类似于ViewDetail1.strFirstName = assignValue("strFirstName")代码)。

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) . 从你的问题,我相信你正在寻找一个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#? 要获取"CustomerName"字符串,请在此处查看我的答案: 如何在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: 实际上,因为它是在C#中,我将在这里复制它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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