Hi
I have a VB6 routine which i have been using it in many of my VB6 projects.
I am trying to upgrade/convert VB6 project and see no of getting a function
similar to this:
-----------
Public Function ChkNull(varValue As Variant, Optional varValueIfNull As
Variant) As Variant
'This function is used to remove a null from a key
If IsNull(varValue) Then
If Not IsMissing(varValueIfNull) Then
ChkNull = varValueIfNull
Else
ChkNull = ""
End If
Else
ChkNull = varValue
End If
End Function
-----------------------------
In VS 2005, Variant data type is not supported and I am asked to use Object
instead. But, Object doesn't have .IsNullOrEmpty() method.
Any help, pls
TIA
Mack
Martin H. - 29 Jul 2008 14:47 GMT
Hello MackS,
To check for Null alone, you can use the "IsDBNull" function.
To check for NullOrEmpty, you have to convert the object into a string
as you can see in the following example:
Dim vValue As Object = System.DBNull.Value
Try
Return String.IsNullOrEmpty(vValue.ToString)
Catch
Return False
End Try
OR
if you need it all the time, you could use this approach:
Public Class MyObject
Private vValue As Object
Public Sub New()
vValue = Nothing
End Sub
Public Sub New(ByVal [Value] As Object)
vValue = [Value]
End Sub
Public Property Value() As Object
Get
Return vValue
End Get
Set(ByVal value As Object)
vValue = [value]
End Set
End Property
Public Function IsNullOrEmpty() As Boolean
Try
Return String.IsNullOrEmpty(vValue.ToString)
Catch
Return False
End Try
End Function
Public Function CheckNull() As Object
Return CheckNull(String.Empty)
End Function
Public Function CheckNull(ByVal varValueIfNull As Object) As Object
If IsDBNull(vValue) Then
Return varValueIfNull
Else
Return vValue
End If
End Function
End Class
The class MyObject offers only one property (Value) which is of the type
object and is used to assign and read values to the variable vValue.
The function IsNothingOrEmpty does the same check as the one listed
above, but now you can use it directly from the variable. The only
difference is that you CAN'T do this:
Dim test As MyObject = "ABC"
as it is not an object type, but a class. However, as a replacement, you
can do this:
Dim test As New MyObject("ABC")
Dim test2 As New MyObject(System.DBNull.Value)
Or (without initialization)
Dim test As New MyObject 'contains value Nothing
As VisualBasic now offers overloading, this is used rather than the
"optional" parameter. The reason is that as far as I know other .NET
languages (e.g. C#) do not support optional parameters and it's good
practice to use overloading, especially if maybe later you want to
release some DLLs (which could then also be used by C# programmers).
Best regards,
Martin
> Hi
> I have a VB6 routine which i have been using it in many of my VB6 projects.
[quoted text clipped - 24 lines]
>
> Mack
MackS - 29 Jul 2008 19:23 GMT
hi Martin
Thank you for your proper guidance. I am working on your solution.
Thank you again
> Hello MackS,
>
[quoted text clipped - 115 lines]
>>
>> Mack
Jack Jackson - 29 Jul 2008 20:53 GMT
How you test for Null depend on what kind of Null it is.
There is System.DbNull, which is returned from databases when a field
value has a database value of NULL.
Then there is Nothing, which is usually used for objects in your
program.
To test for the former you use:
If obj Is System.DbNull.Value
For the latter use:
If obj Is Nothing
or
If obj IsNot Nothing
Strings are a special case. The test:
Dim str As String
...
If str = "" Then
will test both for str Is Nothing and str equal to "".
In .NET I would recommend either writing individual methods for each
data type you want to support (string, integer, etc.), or use a
generic method. That way you have type safety - the routine will
return the same data type as the argument.
>Hi
>I have a VB6 routine which i have been using it in many of my VB6 projects.
[quoted text clipped - 24 lines]
>
>Mack