Finding if a type is a numeric type

Updated on January 1, 2017 in  [R] Scripts
Share on Facebook0Tweet about this on Twitter0Share on Google+0Share on Reddit0
0 on January 1, 2017

I just recently had a need in one of my projects to check if a certain type is a numeric type.
And in case you are wondering, you can use it freely and there is no reason to credit me. Just please don’t post it and say you made it…that just won’t be nice. I won’t do anything to you, so if you really don’t feel like being nice, feel free to re-post it as your own, just know it’s not nice.

The thing itself:
First of all, I made two methods to check it…I mean, I found two methods online to check it, and put them in a static class:

Click to Expand

This is good to check if a type is numeric, but what if you wanted to use operators you can only use on numeric types, like <? In that case I needed to make a new class. I made two overloads of it.

Object Overload:

Click to Expand

Generic Overload:

Click to Expand

As you can see, I have used a custom exception, here it is:

Click to Expand

This is basically all you have to copy paste into some files in your project. I am sorry for the code not being formatted nicely…the forum just doesn’t like tabs. But you could easily fix it when you put it in your IDE.

How to use it:
Here is a good example:
I made some attribute which needs to compares things with anonymous types. So here is how I did it:

Click to Expand

EDIT:
I put all the code in a spoiler thing, so it won’t take that much space. I put the spoiler on the final explanation as well because…well… I will let this explain: http://imgur.com/a/hi3fn

ANOTHER EDIT:
For anyone who is too lazy to copy-paste, you can download it here: https://drive.google.com/drive/folders/0BwN2sNbqJn9mLXNqNnFWd2dUUFk

  • Liked by
  • BinaryBeal
  • Job
  • Dion Dokter
Reply