开发者

Reference Label1.Text as Object

If I pass Label1.Text through a function it treats it as an object and not a string. Which is good.

But when I do something like:

Dim temp As Object = Label1.Text

And then pass it through a function it treats it as a string, which is to be expected. How can I make it so I can set Label1.Text to an object the same way it would if I just passed it through a function.

开发者_如何学GoEdit: Here is specifically what I want to do

Public Sub test()
    setIt(Label1.Text, "Test") 'this works

    Dim temp As Object = Label1.Text
    setIt(temp, "Test") 'this doesnt work
End Sub
Public Sub setIt(ByRef e, ByVal v)
    e = v
End Sub


A string is a string, regardless of you downcasting it to an object or not. You can check this with typeof temp is String (typeof-keyword).

I don't understand where your actual problem is.

You cannot set a Label's Text property with a given object because Label.Text must be of Type String. But you could use Object's ToString to get a String represantation of your Object or if you know(check with typeof) that your Object is of type String you can simply cast it back:

Label1.Text = DirectCast(temp , String)

EDIT: According to your updates, i strongly recommend to set Option Strict! Why don't you simply assign the value to the Text property?

Label1.Text = "Test"

You're ByRef approach is very error-prone and not very readable. If you really need such thing and you only want to set the Text property of different controls, try this:

Public Sub setControlText(ByVal ctrl As Control, ByVal text String)
   ctrl.Text = text
End Sub

or if your "text" must be of type Object:

Public Sub setControlText(ByVal ctrl As Control, ByVal value As Object)
       If Not value Is Nothing Then ctrl.Text = value.ToString
End Sub

or you can use reflection to set every property, but that should not be standard

Public Sub setProperty(ByVal obj As Object, ByVal propName As String, ByVal newValue As Object)
    Dim prop As Reflection.PropertyInfo = obj.GetType().GetProperty(propName)
    If Not prop Is Nothing AndAlso prop.CanWrite Then
        prop.SetValue(obj, newValue, Nothing)
    End If
End Sub

You can call this function in the following way(consider that the property-name is case sensitive):

setProperty(Label1, "Text", "Hello World!")


Although you can convert and wrap up using this:

Dim objTemp As Object = CObj(Label1.Text)

or

Dim objTemp As Object = CType(Label1.Text, Object)

Then when you need it later, just unwrap it using the opposite

Dim strTemp As String = CType(objTemp, String)

Edit:

If you use this wee function and call it using a string declaration of the property then you can dynamically edit your objects properties.

 Public Shared Sub SetPropertyValue(ByVal o As Object, _
                     ByVal propertyName As String, ByVal newValue As Object)
        Dim pi As Reflection.PropertyInfo
        pi = o.[GetType]().GetProperty(propertyName)
        If pi Is Nothing Then
            Throw New Exception("No Property [" & propertyName & "] in Object [" & o.[GetType]().ToString() & "]")
        End If
        If Not pi.CanWrite Then
            Throw New Exception("Property [" & propertyName & "] in Object [" & o.[GetType]().ToString() & "] does not allow writes")
        End If
        pi.SetValue(o, newValue, Nothing)
    End Sub

Call:

SetPropertyValue(objTemp, "Phone", newVal)

or in your case with Label1:

Dim newVal as String = "Test"
SetPropertyValue(Label1, "Text", newVal)

Hope that helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜