Solved

Convert function from vb6 to vb.net

Posted on 2007-04-02
7
662 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
  • 4
  • 2
7 Comments
 
LVL 142

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 142

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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 142

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 250 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 142

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now