Convert IsMissing, IsNull, VBempty to vb.net

  • Thread starter Thread starter briancfk
  • Start date Start date
B

briancfk

I now converting vb6 code to vb.net code

Let me descrip my problem first

i got a Function e.g.

Public Function Method1(Byval ar as object, Optional ByVal strWHFrom As
String, Optional ByVal strWHTo As String) as string
........
If IsMissing(strWHFrom) And IsMissing(strWHTo) Then
strSql = "............"
ElseIf IsMissing(strWHFrom) And Not IsMissing(strWHTo) Then
strSql = "............"
ElseIf Not IsMissing(strWHFrom ) And IsMissing(strWHTo) Then
strSql = "............."
Else
If IsNull(strWHFrom ) And IsNull(strWHTo) Then
strSql = "................"
ElseIf Len(strWHFrom )=0 And len(strWHTo)=0 Then
strSql = "..............."
Else
If Trim$(strWHFrom ) = Trim$(strWHTo) Then
strSql = "............."
Else
strSql = "............."
End If
End If
End If
End Funciton

This function work well in vb6

But How I going to code this with vb.net, vb.net seem like cant
differentciate the parameter got pass in or not. and also how to
convert the IsNull

Is it mean that I must put some SPEACIAL default value to the Optional
Parameter to detect that particular parameter got pass in or not?

Thank you
 
The function is now IsDBNull

For optional parameters AFAIK a default value is now required (making
IsMissing not needed any more).
 
You could record it as (note the default values for the optional
parameters):

Public Function Method1(Byval ar as object, Optional ByVal strWHFrom As
String = String.Empty, Optional ByVal strWHTo As String = String.Empty) As
String
....
If strWHFrom Is String.Empty AndAlso strWHTo Is String.Empty Then
strSql = "............"
ElseIf strWHFrom Is String.Empty Then
strSql = "............"
ElseIf strWHTo Is String.Empty Then
strSql = "............."
Else
If strWHFrom.Trim = strWHTo.Trim Then
strSql = "............."
Else
strSql = "............."
End If
End Function

or you could create overloaded methods instead:

Public Function Method1(Byval ar as object) As String

Return Method1(ar, String.Empty, String.Empty)

End Function

Public Function Method1(Byval ar as object, ByVal strWHFrom As String) As
String

Return Method1(ar, strWHFrom, String.Empty)

End Function

Public Function Method1(Byval ar as object, ByVal strWHFrom As String,
ByVal strWHTo As String) As String
....
If strWHFrom Is String.Empty AndAlso strWHTo Is String.Empty Then
strSql = "............"
ElseIf strWHFrom Is String.Empty Then
strSql = "............"
ElseIf strWHTo Is String.Empty Then
strSql = "............."
Else
If strWHFrom.Trim = strWHTo.Trim Then
strSql = "............."
Else
strSql = "............."
End If
End Function
 
Stephany,

Thanks for reply

For my case, it is not a good way to solve this problem by using the
overloaded method, cause I will have more that 2 optional parameter.

At first I am so happy that you provided a solution of i never think
before
Public Function Method1(Byval ar as object, Optional ByVal strWHFrom
As
String = String.Empty, Optional ByVal strWHTo As String =
String.Empty) As
String

But, after i tried, I found that an optional variable MUST assign with
a constant value, and the String.Empty make the program fail. Too
bad.....cause if this can work, this can be a very good solution

Anyway, if really dont have any better solution for this case, I think
I have to follow Patrice way(set a special default value to detact)

Thank you
 
Sorry, I keep forgetting that String.Empty is not actually a constant.

Use:

Public Function Method1(Byval ar as object, Optional ByVal strWHFrom As
String = "", Optional ByVal strWHTo As String = "") As String

instead.
 
Back
Top