对象属性的可变数据类型而不是属性的值?

时间:2014-11-06 08:59:18

标签: vb.net winforms visual-studio variables types

是否存在用于将对象属性表示为变量的数据类型?即TextBox1.Text作为变量?

我没有尝试将其文本作为字符串数据类型变量实现,也不尝试将控件作为对象数据类型实现。相反,我试图实现......

Dim userCountry As String
favouriteCar = ComboBox1.Text

Dim mostPopularCar As ?
mostPopularCar = TextBox1.Text

Select Case userCountry
    Case "Canada"
        mostPopularCar = "Hyundai"
    Case "USA"
        mostPopularCar = "Ford"
    Case "China"
        mostPopularCar = "Volkswagon"
    Case "Uganda"
        mostPopularCar = "Toyota"
End Select

最终,我不想写TextBox1.Text =,而是写mostPopularCar =

...或者,我只是简单地在定义任何变量时过于苛刻,而我真正需要做的就是将.Text附加到每个对象的每次迭代中?

2 个答案:

答案 0 :(得分:0)

你的问题不够明确(至少对我而言)。

但如果我理解正确,您可以使用System.Reflection.PropertyInfo类型按名称访问对象的属性 即。

Dim textProperty As System.Reflection.PropertyInfo = TextBox1.GetType().GetProperty("Text")
Dim textValue As String = textProperty.GetValue(TextBox1)

答案 1 :(得分:0)

你可以这样做,如果你真的想使用一个变量(也许是因为你想保存旧值或根据它来做某事)

Sub whatever()
        Dim userCountry As String = "USA"
        changeText(Textbox1.Text, userCountry)
    End Sub

    Private Sub changeText(ByRef mostPopularCar As String, userCountry As String)
        Select Case userCountry
            Case "Canada"
                mostPopularCar = "Hyundai"
            Case "USA"
                mostPopularCar = "Ford"
            Case "China"
                mostPopularCar = "Volkswagon"
            Case "Uganda"
                mostPopularCar = "Toyota"
        End Select
    End Sub