By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,299 Members | 777 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,299 IT Pros & Developers. It's quick & easy.

How to write an universal function isNegative?

P: 37
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?
Jul 24 '08 #1
Share this Question
Share on Google+
7 Replies

shweta123
Expert 100+
P: 692
Hi,

You can try this code :

Expand|Select|Wrap|Line Numbers
  1.   Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.         Dim i As Double
  3.         i = 12.0
  4.  
  5.         ''Call function to check if no is positive or negative
  6.         If (isNegative(i)) Then
  7.             MsgBox("No is negative")
  8.         Else
  9.             MsgBox("No is Positive")
  10.         End If
  11.     End Sub
  12.  
  13.     Public Function isNegative(ByVal obj As Object) As Boolean
  14.  
  15.         If (obj < 0) Then
  16.                    'given no is negative
  17.             Return True
  18.         Else
  19.              'Given no is not negative
  20.             Return False
  21.         End If
  22.     End Function
Is this that you want?
Jul 24 '08 #2

P: 37
NO. You cannot compare object type just like that!
Jul 24 '08 #3

Frinavale
Expert Mod 5K+
P: 9,731
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:

Expand|Select|Wrap|Line Numbers
  1. Private Function isNegative(byval value as float)
  2. '...
  3. End Function
  4.  
  5. Private Function isNegative(byval value as integer)
  6. '...
  7. End Function
  8. 'And so on....
  9.  
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
Jul 24 '08 #4

P: 37
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.
Jul 24 '08 #5

Frinavale
Expert Mod 5K+
P: 9,731
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
Jul 24 '08 #6

Curtis Rutland
Expert 2.5K+
P: 3,256
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.
Jul 24 '08 #7

Plater
Expert 5K+
P: 7,872
Course instead of writing out functions you could just do "< 0" whereever you wanted....
Jul 24 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.