How to write an universal function isNegative?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mynkow
    New Member
    • Jul 2007
    • 37

    How to write an universal function isNegative?

    Hi,

    I want to write an universal function called isNegative( param ) where the param can be float, int, double, long etc. The result will be true/false. Any comments?
  • shweta123
    Recognized Expert Contributor
    • Nov 2006
    • 692

    #2
    Hi,

    You can try this code :

    Code:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim i As Double
            i = 12.0
    
            ''Call function to check if no is positive or negative
            If (isNegative(i)) Then
                MsgBox("No is negative")
            Else
                MsgBox("No is Positive")
            End If
        End Sub
    
        Public Function isNegative(ByVal obj As Object) As Boolean
    
            If (obj < 0) Then
                       'given no is negative
                Return True
            Else
                 'Given no is not negative
                Return False
            End If
        End Function
    Is this that you want?

    Comment

    • mynkow
      New Member
      • Jul 2007
      • 37

      #3
      NO. You cannot compare object type just like that!

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by mynkow
        NO. You cannot compare object type just like that!

        You can if the param can be float, int, double, long etc (like you originally asked for)....it'll throw errors if the object cannot be compared in this way.

        The alternative to this is to write overloaded function....eg functions with the same name but different signatures:

        Code:
        Private Function isNegative(byval value as float)
        '...
        End Function
        
        Private Function isNegative(byval value as integer)
        '...
        End Function
        'And so on....
        I think the first solution is a bit easier to implement though....and all you have to do is add a Try Catch block around it...

        Give it a try and see what happens.

        -Frinny

        Comment

        • mynkow
          New Member
          • Jul 2007
          • 37

          #5
          I am using C# and I get a compile error when I try to do such compare.

          Operator '<' cannot be applied to operands of type 'object'.

          Also I do not want to create hundreds of functions. I think it is possible with runtime type cast or something like that.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by mynkow
            I am using C# and I get a compile error when I try to do such compare.

            Operator '<' cannot be applied to operands of type 'object'.

            Also I do not want to create hundreds of functions. I think it is possible with runtime type cast or something like that.

            Sounds good, give it a try.

            You can use the TypeOf operator to determine the type of the object passed in...

            Then determine if a comparison can be made on the object passed in to determine if it is negative....

            If it can be made, use a Switch to make the appropriate comparison on the object after casting it into it's proper type.


            -Frinny

            Comment

            • Curtis Rutland
              Recognized Expert Specialist
              • Apr 2008
              • 3264

              #7
              Originally posted by mynkow
              I am using C# and I get a compile error when I try to do such compare.

              Operator '<' cannot be applied to operands of type 'object'.

              Also I do not want to create hundreds of functions. I think it is possible with runtime type cast or something like that.

              Well, you don't have to write hundreds, since (as far as I know) there are only 7 signed primitive data types: sbyte, short, int, long, float, double, and decimal. It wouldn't be much work to write all seven, just copy, paste, change type, repeat. You can even make a class library, so you can just add the DLL to any project and have these functions.

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                Course instead of writing out functions you could just do "< 0" whereever you wanted....

                Comment

                Working...