Home All Groups Group Topic Archive Search About

Convert IsMissing, IsNull, VBempty to vb.net

Author
30 Mar 2006 8:54 AM
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

Author
30 Mar 2006 9:25 AM
Patrice
The function is now IsDBNull

For optional parameters AFAIK a default value is now required (making
IsMissing not needed any more).


--
Patrice

<brian***@gmail.com> a écrit dans le message de news:
1143708861.770906.306***@i39g2000cwa.googlegroups.com...
Show quoteHide quote
>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
>
Author
30 Mar 2006 10:33 AM
Stephany Young
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


<brian***@gmail.com> wrote in message
Show quoteHide quote
news:1143708861.770906.306820@i39g2000cwa.googlegroups.com...
>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
>
Author
31 Mar 2006 2:05 AM
briancfk
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
Author
31 Mar 2006 2:59 AM
Stephany Young
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.

<brian***@gmail.com> wrote in message
Show quoteHide quote
news:1143770755.460690.4130@u72g2000cwu.googlegroups.com...
> 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
>