casting an object to a specific type

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Teeples

    casting an object to a specific type

    I have a method that passes in two string objects (both numerical numbers)
    and a string identifying their type.

    public bool DoCompare(strin g num1, string num2, string theirType)
    {
    System.Type type = System.Type.Get Type(theirType) ;
    return ((type)num1 <= (type)num2) ? true : false;
    }

    The compiler doesn't like the (type) casting that I do. What is the proper
    way of casting an object to its correct type to do such a mathematical
    operation?

    --
    Steve
  • Steve Teeples

    #2
    RE: casting an object to a specific type

    This question was answered in another thread. Thanks.

    "Steve Teeples" wrote:
    [color=blue]
    > I have a method that passes in two string objects (both numerical numbers)
    > and a string identifying their type.
    >
    > public bool DoCompare(strin g num1, string num2, string theirType)
    > {
    > System.Type type = System.Type.Get Type(theirType) ;
    > return ((type)num1 <= (type)num2) ? true : false;
    > }
    >
    > The compiler doesn't like the (type) casting that I do. What is the proper
    > way of casting an object to its correct type to do such a mathematical
    > operation?
    >
    > --
    > Steve[/color]

    Comment

    • news.tiscalinet.ch

      #3
      Re: casting an object to a specific type

      Steve Teeples wrote:[color=blue]
      > I have a method that passes in two string objects (both numerical numbers)
      > and a string identifying their type.
      >
      > public bool DoCompare(strin g num1, string num2, string theirType)
      > {
      > System.Type type = System.Type.Get Type(theirType) ;
      > return ((type)num1 <= (type)num2) ? true : false;
      > }
      >
      > The compiler doesn't like the (type) casting that I do. What is the proper
      > way of casting an object to its correct type to do such a mathematical
      > operation?
      >[/color]

      I was looking at some of your other threads and I saw a REALLY big if,
      but here is a simpler version and number safe...

      public bool DoCompare( string num1, string num2) {
      Decimal num1 = Decimal.Parse( num1);
      Decimal num2 = Decimal.Parse( num2);

      return Decimal.op_Less ThanOrEqual( num1, num2);
      }

      Christian

      Comment

      • Steve Teeples

        #4
        Re: casting an object to a specific type

        I see you're simply converting everything to a double and managing it that
        way. Simple and dependable. Thanks for the insight.

        "news.tiscaline t.ch" wrote:
        [color=blue]
        > Steve Teeples wrote:[color=green]
        > > I have a method that passes in two string objects (both numerical numbers)
        > > and a string identifying their type.
        > >
        > > public bool DoCompare(strin g num1, string num2, string theirType)
        > > {
        > > System.Type type = System.Type.Get Type(theirType) ;
        > > return ((type)num1 <= (type)num2) ? true : false;
        > > }
        > >
        > > The compiler doesn't like the (type) casting that I do. What is the proper
        > > way of casting an object to its correct type to do such a mathematical
        > > operation?
        > >[/color]
        >
        > I was looking at some of your other threads and I saw a REALLY big if,
        > but here is a simpler version and number safe...
        >
        > public bool DoCompare( string num1, string num2) {
        > Decimal num1 = Decimal.Parse( num1);
        > Decimal num2 = Decimal.Parse( num2);
        >
        > return Decimal.op_Less ThanOrEqual( num1, num2);
        > }
        >
        > Christian
        >[/color]

        Comment

        Working...