Convert WebControls.Unit to String

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
 
K

Karl Seguin [MVP]

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Problem with type conversion 1
Custom web control string 2
Property 1
Enum Problem 1
Use property in custom control 1
Reflection 2
Property. Control or View State? 1
System.Drawing.Color to String 2

Top