?
Solved

Convert function from vb6 to vb.net

Posted on 2007-04-02
7
Medium Priority
?
682 Views
Last Modified: 2012-06-27
I used this routine in vb6 to capture any null values and return a valid I could do this with.  I would like to convert this to vb.net but doesnt seem to be taken very well.  I was running this in a bas module and would like to do the same tyoe thing.



Global Const ZERODATE As Date = "1/1/1900"

'Enumerated type to define the data types accepted by CDBNull
Public Enum DBType
    dbText = 0
    dbNumber = 1
    dbDate = 2
    dbBool = 3
End Enum

Public Function CDBNull(varInputValue As Variant, Optional intType As DBType = dbText) As Variant

    On Error Resume Next
    If (IsNull(varInputValue)) Then
        Select Case intType
            Case dbText
                CDBNull = ""
            Case dbNumber
                CDBNull = 0
            Case dbDate
                CDBNull = ZERODATE
            Case dbBool
                CDBNull = False
            Case Else
                CDBNull = ""
        End Select
    Else
        Select Case intType
            Case dbText
                CDBNull = CStr(varInputValue)
            Case dbNumber
                If (IsNumeric(varInputValue)) Then
                    CDBNull = CCur(varInputValue)
                Else
                    CDBNull = "0"
                End If
            Case dbDate
                If (IsDate(varInputValue)) Then
                    CDBNull = CDate(varInputValue)
                Else
                    CDBNull = ZERODATE
                End If
            Case dbBool
                CDBNull = CBool(varInputValue)
            Case Else
                CDBNull = CStr(varInputValue)
        End Select
    End If
End Function
0
Comment
Question by:mgmhicks
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18837695
you have to replace VARIANT by OBJECT
you have to check for null like this:
    If varInputValue == DBNull.Value Then


0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18837705
optional parameters is no longer possible in vb.net, but function overloading:

Public Function CDBNull(varInputValue As object ) As Variant
  return CDBNull(varInputValue, dbText)
End function

Public Function CDBNull(varInputValue As object, Optional intType As DBType ) As object
  ...
End Function
0
 

Author Comment

by:mgmhicks
ID: 18838158
Ok,  not sure things arent working at all.  Can we start with the enum statement.  Is that ok, and then here's what the code is looking like now.  Its having a issue with the inttypes.

Public Function CDBNull(ByVal varInputValue As Object) As Object
        Return CDBNull(varInputValue, dbText)


        On Error Resume Next
        If varInputValue = DBNull.Value Then
            Select Case intType
                Case Text
                    CDBNull = ""
                Case Number
                    CDBNull = 0
                Case dbDate
                    CDBNull = ZERODATE
                Case dbBool
                    CDBNull = False
                Case Else
                    CDBNull = ""
            End Select
        Else
            Select Case intType
                Case dbText
                    CDBNull = CStr(varInputValue)
                Case dbNumber
                    If (IsNumeric(varInputValue)) Then
                        CDBNull = CCur(varInputValue)
                    Else
                        CDBNull = "0"
                    End If
                Case dbDate
                    If (IsDate(varInputValue)) Then
                        CDBNull = CDate(varInputValue)
                    Else
                        CDBNull = ZERODATE
                    End If
                Case dbBool
                    CDBNull = CBool(varInputValue)
                Case Else
                    CDBNull = CStr(varInputValue)
            End Select
        End If
    End Function

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18838690

Public Function CDBNull(ByVal varInputValue As Object) As Object
        Return CDBNull(varInputValue, dbText)
End Function

Public Function CDBNull(ByVal varInputValue As Object, inttypes as DBType  ) As Object
        On Error Resume Next
        If varInputValue = DBNull.Value Then
            Select Case intType
                Case Text
                    CDBNull = ""
                Case Number
                    CDBNull = 0
                Case dbDate
                    CDBNull = ZERODATE
                Case dbBool
                    CDBNull = False
                Case Else
                    CDBNull = ""
            End Select
        Else
            Select Case intType
                Case dbText
                    CDBNull = varInputValue.ToString()
                Case dbNumber
                    If (Int32.TryParse(varInputValue)) Then
                        CDBNull = Int32.Parse(varInputValue)
                    Else
                        CDBNull = 0
                    End If
                Case dbDate
                    Try
                        CDBNull = DateTime.Parse(varInputValue)
                    Catch ex as Exception
                        CDBNull = ZERODATE
                    End Try
                Case dbBool
                    CDBNull = varInputValue
                Case Else
                    CDBNull = varInputValue.ToString
            End Select
        End If
    End Function
0
 
LVL 3

Accepted Solution

by:
r1937 earned 1000 total points
ID: 18838772
   Public Const ZERODATE As Date = #1/1/1900#

    'Enumerated type to define the data types accepted by CDBNull
    Public Enum DBType
        dbText = 0
        dbNumber = 1
        dbDate = 2
        dbBool = 3
    End Enum

    Public Function CDBNull(ByVal varInputValue As Object, Optional ByVal intType As DBType = DBType.dbText) As Object

        On Error Resume Next
        If Not varInputValue Is Nothing Then
            Select Case intType
                Case DBType.dbText
                    CDBNull = ""
                Case DBType.dbNumber
                    CDBNull = 0
                Case DBType.dbDate
                    CDBNull = ZERODATE
                Case DBType.dbBool
                    CDBNull = False
                Case Else
                    CDBNull = ""
            End Select
        Else
            Select Case intType
                Case DBType.dbText
                    CDBNull = CStr(varInputValue)
                Case DBType.dbNumber
                    If (IsNumeric(varInputValue)) Then
                        CDBNull = CDec(varInputValue)
                    Else
                        CDBNull = "0"
                    End If
                Case DBType.dbDate
                    If (IsDate(varInputValue)) Then
                        CDBNull = CDate(varInputValue)
                    Else
                        CDBNull = ZERODATE
                    End If
                Case DBType.dbBool
                    CDBNull = CBool(varInputValue)
                Case Else
                    CDBNull = CStr(varInputValue)
            End Select
        End If
    End Function

--------------------------------------------------------------------------
Things to say:
Currency data type is not supported. Double is used instead.
Though 'on error' statement is still supported Try-Catch-Finally is preffered.
Late Binding is not a good practice. In this case Function Overloading is suggested.

0
 
LVL 3

Expert Comment

by:r1937
ID: 18838793
Sorry Decimal is used instead of Currency.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 18838807
>Late Binding is not a good practice. In this case Function Overloading is suggested.

I have to agree 100% with that!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question