Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
'FIXIT: Declare 'varValue' with an early-bound data type                                   FixIT90210ae-R1672-R1B8ZE
Public Function NullToText(varValue As Variant) As String

    ' Append an empty string, forcing a conversion
    ' of a Null value to a string, leaving normal
    ' strings as is.
    NullToText = varValue & vbNullString

End Function
Posted

1 solution

What is exactly that you're trying to do? First of all, basically all objects have ToString()[^] method since it's defined on Object class and derived on all other classes.

If you're wondering what is the equivalent for variant, use object.

If the problem lies in conversion if varValue is Nothing, then you could use something like:
VB
Public Function NullToText(varValue As Object) As String
    If varValue Is Nothing Then
        NullToText = ""
    Else
        NullToText = varValue.ToString()
    End If
End Function

However, depending on your environment and what you use this function for, that may not work correctly if decimal numbers, dates etc. are passed to the function and different locales are used since the conversion should be culture aware. For example, see: Int32.ToString Method (IFormatProvider)[^]
 
Share this answer
 
Comments
Tony Radu 23-Dec-11 16:55pm    
Trying to concatenate Nothing with varValue but & is unknown..
Wendelius 23-Dec-11 17:01pm    
Sorry, but don't understand. What do yo mean that & is unknown? If this is related to some code, please post the relevant code also.
Tony Radu 23-Dec-11 17:17pm    
Sorry, i mean the error says Operator '&' is not defined for type 'InternalField' and 'Nothing'.
Wendelius 23-Dec-11 17:33pm    
Is internal field your own class? If it is, try using/overloading ToString() method for your class
Tony Radu 27-Dec-11 14:24pm    
Private Sub ShowData()

mfDataChanged = False
With rstForms
If .RecordCount > 0 Then
txtFormTitle.Text = NullToText(.Fields("Form_title"))
End If

End With

Not reading the value of Form_title it passes ADODB.InternalFields for NullToText thanks...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900