0

I have a program that asks the user for an integer (as large as they'd like). It then reads that in and does a lot of calculations with it. What I want to do is cast the input to the "best" type before using it (byte, short, int, long, or BigInteger).

What I'm looking to do is this

if(number < 256)
{
    byte numberToWorkWith = (byte)number;
}
else if(number < 65536)
{
    ushort numberToWorkWith = (ushort)number
}
else if ...

then I want to be able to do all of my calculations/etc with numberToWorkWith. The problem is that after I'm out of the if/else if/ else statement, I lose access to numberToWorkWith.

Here is what I've considered (and why they haven't yet worked)

  1. Declaring a var type numberToWorkWith (which doesn't work because I have to initialize it at declaration)
  2. Declaringdynamic numberToWorkWith; above, then putting something like numberToWorkWith = (byte)number; in the loop (but I need to lock in that type. The program changes the type again later on and causes an error).
  3. Creating a method like below

    private static void WorkWithNumber(Type passedType)
    {
        passedType numberToWorkWith = (passedType)number;
    }
    

    which gives me an error because I can't used passedType as a type (although I'm not sure why)

There is a long block of code that I execute on numberToWorkWith no matter what type it is, so I'd very much rather not copy the code 5 times to put it in each of the if/else if/else statements. I feel like solution 3 (creating a method and passing a type) might have potential ...

Thoughts? Suggestions?

Thank you in advance!!

Brian
  • 81
  • 3
  • 3
    Usually, the answer is to use a type large enough to contain whatever input is provided (so, just use a BigInteger). What *value* or *benefit* do you think you'll gain by tailoring the data type more closely? Also, if there *is* a difference, do you think the user will expect 200 to be processed differently than 300 would be? – Damien_The_Unbeliever Nov 11 '16 at 07:38
  • 1
    Side note: here is starting point to read on generics (which looks like right fit at first, but you can't perform any operations on those values) - http://stackoverflow.com/questions/3329576/generic-constraint-to-match-numeric-types ... Rea – Alexei Levenkov Nov 11 '16 at 07:44
  • @Damien_The_Unbeliever Separating out byte, ushort, uint, and ulong really don't do much for the program, other than minimise the resources used. The big payoff is between long and BigInteger. The program runs significantly slower (takes minutes longer to run ... I'm doing a lot of calculations) if I just use BigInteger for everything – Brian Nov 11 '16 at 07:53
  • @Brian - well, just use `long` and `BigInteger` then, and you've already reduced your duplication by a factor of 5:2. :-) – Damien_The_Unbeliever Nov 11 '16 at 07:56
  • @Damien_The_Unbeliever I could do that, but then I still paste a hefty amount of code into the if block, only to then re-paste the code into the else block. Doable yes, but it doesn't seem like good code. – Brian Nov 11 '16 at 08:00
  • @AlexeiLevenkov: sorry, I probably misread that because there was an answer already, arguing *pro* generics – Thomas Weller Nov 11 '16 at 08:37

0 Answers0