2

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.

Edit: 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
John Saunders
  • 160,644
  • 26
  • 247
  • 397
Phil
  • 81
  • 4
  • 11

2 Answers2

4

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!")
Community
  • 1
  • 1
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
  • I would recommend Tim's approach if you are only setting text and only use my approach if you must have the flexibility to set any property in an Object or control. – Tom 'Blue' Piddock Nov 09 '10 at 10:55
  • That last section is essentially what I just wrote :| – Tom 'Blue' Piddock Nov 09 '10 at 11:41
  • Yes, i have seen later. But i think we're directing him in the wrong direction. I'm sure that that the reflection approach is overhead in his situation. – Tim Schmelter Nov 09 '10 at 11:46
  • Agreed, I would go with the original recommendation of keeping it strict and explicit with just the text property or calling the control as the object itself. – Tom 'Blue' Piddock Nov 09 '10 at 11:50
  • Thanks the reflection was what I was looking for. I had actually looked into reflection but kept getting stranded on setvalue and figured I was doing something wrong. But your code works and fixes my problem! – Phil Nov 09 '10 at 20:07
  • The reason the setting text method doesn't work for me is because I need to be able to pass different property types. – Phil Nov 09 '10 at 20:08
  • And you don't know the properties at runtime or is it just a laziness function? Reflection is always slower than normal setters. – Tim Schmelter Nov 09 '10 at 20:30
  • Kinda hard to explain but yeah more on the side of laziness as I would have to write out nearly every property of each control. – Phil Nov 09 '10 at 20:36
1

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.

Nicolas
  • 1,151
  • 3
  • 15
  • 28
Tom 'Blue' Piddock
  • 2,131
  • 1
  • 21
  • 36