How to pass a null and another value into a function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vertigo262
    New Member
    • Jul 2007
    • 62

    #1

    How to pass a null and another value into a function

    Ok, I keep running into this problem

    In my aspx I am calling a simple function.

    But the problem is, that the function is either, string, integer, etc.

    If there's null's in the data, the function explodes because it's set to a string or integer, etc.

    How can I make the function accept both a null, and another data type so that I can

    write some if then's to do different things if it is null or another data type.

    Here is the code to give you an idea

    Code:
     
    .aspx code
    
    <%# GetRatingFormat(Container.DataItem("Rating") %>
    
    
    .vb code
    
    Function GetRatingFormat(ByVal UserRating As Decimal) As String
    
            If IsNothing(UserRating) Or IsDBNull(UserRating) Then
                Return "Not Yet Rated"
            Else
                Return Format(UserRating, "0.0") & "%"
            End If
    
    
        End Function
    UserRating needs to accept both null and decimal

    any help would be greatly appreciated
  • danp129
    Recognized Expert Contributor
    • Jul 2006
    • 323

    #2
    Try declaring UserRating as Object.

    Comment

    • vertigo262
      New Member
      • Jul 2007
      • 62

      #3
      Hi Dan,

      thanks for the reply. that's how I had it originally. The object was dropping the value of the decimal and making it a 0.0

      Comment

      • danp129
        Recognized Expert Contributor
        • Jul 2006
        • 323

        #4
        Format requires a numeric data type and it was likely being passed a string. This should work.

        Code:
            Function GetRatingFormat(ByVal userRating As Object) As String
                Dim decimalRating As Decimal = Nothing
        
                If TypeOf userRating Is Decimal Then
                    decimalRating = DirectCast(userRating, Decimal)
                ElseIf IsNumeric(userRating) Then
                    decimalRating = CDec(userRating)
                End If
        
                If decimalRating = Nothing Then
                    Return "Not Yet Rated"
                Else
                    Return Format(decimalRating, "0.0") & "%"
                End If
        
            End Function

        Comment

        • vertigo262
          New Member
          • Jul 2007
          • 62

          #5
          Works great!

          I don't really understand what's going on in your code, but it works. Was hoping there was a generic way of doing it in here "(ByVal userRating As Object"

          in case I want to do other functions with both null and datatype.

          But I can work with this :)

          Your help is much appreciated!

          Thank you

          Comment

          • danp129
            Recognized Expert Contributor
            • Jul 2006
            • 323

            #6
            You can use overloads if you want.

            Code:
                 Overloads Function GetRatingFormat(ByVal userRating As Object) As String
                      ' Your code here
                 End Function
            
                 Overloads Function GetRatingFormat(ByVal userRating As String) As String
                      ' Your code here
                 End Function
            
                 Overloads Function GetRatingFormat(ByVal userRating As Decimal) As String
                      ' Your code here
                 End Function
            
            ' etc...

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              I would have gone with Overloading the method or even using Optional parameters. There are quite a few ways to solve this problem :)

              -Frinny

              Comment

              • vertigo262
                New Member
                • Jul 2007
                • 62

                #8
                wouldn't happen to have an example code of Optional Parameters?

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Something like this

                  Code:
                  Function GetRatingFormat(Optional ByVal userRating  As Object = Nothing, Optional ByVal userRatingDec As Decimal = 0, Optional ByVal  userRatingStr As String = "")
                    'In this function you will have to validate each
                    'parameter to determine which one has bee provided
                  End Function
                  After typing out the example...I don't like this recommendation any more. The overloaded version is your best approach in my opinion.

                  -Frinny

                  Comment

                  • vertigo262
                    New Member
                    • Jul 2007
                    • 62

                    #10
                    OK!!!!! thanks for the input Frinny and everyone else!

                    Comment

                    Working...