Convert WebControls.Unit to String

  • Thread starter Thread starter shapper
  • Start date Start date
S

shapper

Hello,

In a custom control property I have the following:

If CStr(ViewState("InfoWidth")) Is Nothing Then ...

I am getting the error:

Conversion from type 'Unit' to type 'String' is not valid.

Can someone tell me how can I convert the InfoWidth (Type =
WebControls.Unit) to a string?

Thanks,
Miguel
 
If you are checking against null, you don't need to convert anything,
simply:

if ViewState("InfoWidth") is nothing Then
...
end if

will work.

That's actually the right way to do it, check for null BEFORE trying to
convert anything, otherwise you'll get nullreferenceexceptions.

Once you are sure it isn't null, use it's ToString() method. This is what it
looks like:

Public Function ToString(ByVal formatProvider As IFormatProvider) As String
Dim text1 As String
If Me.IsEmpty Then
Return String.Empty
End If
If (Me.type = UnitType.Pixel) Then
text1 = CInt(Me.value).ToString(formatProvider)
Else
text1 = CSng(Me.value).ToString(formatProvider)
End If
Return (text1 & Unit.GetStringFromType(Me.type))
End Function
 
Back
Top