How to compare objects using their typecode

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • **Developer**

    #1

    How to compare objects using their typecode

    I do the following:

    Select Case mDataType(mColu mnToSort)

    Case TypeCode.Int32

    Result = CInt(xText).Com pareTo(CInt(yTe xt))

    Case TypeCode.String

    Result=...



    I wonder if I could somehow write one statement that would compare all types
    using mDataType(mColu mnToSort) to automate the conversion?


  • Armin Zingler

    #2
    Re: How to compare objects using their typecode

    " **Developer**" <REMOVEdevelope r@a-znet.com> schrieb[color=blue]
    > I do the following:
    >
    > Select Case mDataType(mColu mnToSort)
    >
    > Case TypeCode.Int32
    >
    > Result = CInt(xText).Com pareTo(CInt(yTe xt))
    >
    > Case TypeCode.String
    >
    > Result=...
    >
    >
    >
    > I wonder if I could somehow write one statement that would compare
    > all types using mDataType(mColu mnToSort) to automate the conversion?
    >
    >[/color]



    result = directcast(xtex t, icomparable).co mparto(ytext)

    Assumed that xtext implements IComparable. IComparable is there to support
    the concept of comparing objects.


    Armin

    Comment

    • **Developer**

      #3
      Re: How to compare objects using their typecode

      Added a litte more
      I do the following:

      Select Case mDataType(mColu mnToSort)
      Case TypeCode.Int32
      Result = CInt(xText).Com pareTo(CInt(yTe xt))
      Case TypeCode.Single
      Result = CSng(xText).Com pareTo(CSng(yTe xt))
      Case TypeCode.Double
      Result = CDbl(xText).Com pareTo(CDbl(yTe xt))
      Case TypeCode.String
      Result=...



      I wonder if I could somehow write one statement that would compare all
      types
      using mDataType(mColu mnToSort) to automate the conversion?



      Comment

      • **Developer**

        #4
        Re: How to compare objects using their typecode


        Select Case mDataType(mColu mnToSort)
        Case TypeCode.Int32
        Result = CInt(xText).Com pareTo(CInt(yTe xt))
        Case TypeCode.Single
        Result = CSng(xText).Com pareTo(CSng(yTe xt))
        Case TypeCode.Double
        Result = CDbl(xText).Com pareTo(CDbl(yTe xt))
        Case TypeCode.String
        Result=...

        I added a little more above.
        Notice the conversions before the comparisons.
        I believe the suggestion below will always compare the text as text.

        Thanks
        [color=blue][color=green]
        >>
        >> I wonder if I could somehow write one statement that would compare
        >> all types using mDataType(mColu mnToSort) to automate the conversion?
        >>
        >>[/color]
        >
        >
        >
        > result = directcast(xtex t, icomparable).co mparto(ytext)
        >
        > Assumed that xtext implements IComparable. IComparable is there to support
        > the concept of comparing objects.
        >
        >
        > Armin
        >[/color]


        Comment

        • Armin Zingler

          #5
          Re: How to compare objects using their typecode

          " **Developer**" <REMOVEdevelope r@a-znet.com> schrieb[color=blue]
          >
          > Select Case mDataType(mColu mnToSort)
          > Case TypeCode.Int32
          > Result = CInt(xText).Com pareTo(CInt(yTe xt))
          > Case TypeCode.Single
          > Result = CSng(xText).Com pareTo(CSng(yTe xt))
          > Case TypeCode.Double
          > Result = CDbl(xText).Com pareTo(CDbl(yTe xt))
          > Case TypeCode.String
          > Result=...
          >
          > I added a little more above.
          > Notice the conversions before the comparisons.
          > I believe the suggestion below will always compare the text as text.[/color]

          If xText and yText are strings, you are right. What types do you expect? If
          it's Int32, Single and double only, you could simplify it by using cdbl for
          these types because the value range includes the other types. String must be
          handled seperatly. As there is no general parse method for all data types,
          you will have to handle them individually.


          Armin

          Comment

          • **Developer**

            #6
            Re: How to compare objects using their typecode

            [color=blue]
            > As there is no general parse method for all data types,
            > you will have to handle them individually.[/color]

            That what I was wondering about.
            Thanks


            Comment

            • Jay B. Harlow [MVP - Outlook]

              #7
              Re: How to compare objects using their typecode

              **Developer**
              In addition to the other comments.

              I would use something like:

              Dim dataType As TypeCode
              Dim xText, yText As String
              Dim xObject, yObject As Object
              Dim result As Integer

              xObject = Convert.ChangeT ype(xText, dataType)
              yObject = Convert.ChangeT ype(yText, dataType)

              result = Comparer.Defaul t.Compare(xObje ct, yObject)

              - or -

              result = Comparer.Defaul tInvariant.Comp are(xObject, yObject)


              Convert.ChangeT ype will change the text variable to the requested type code
              (Int32 for example). Be certain to review ChangeType's overloads!

              Comparer.Defaul t does the comparison based on Thread.CurrentC ulture, while
              Comparer.Defaul tInvariant does the comparison based on
              CultureInfo.Inv ariantCulture.

              Hope this helps
              Jay


              " **Developer**" <REMOVEdevelope r@a-znet.com> wrote in message
              news:OeePuR1eFH A.4040@TK2MSFTN GP14.phx.gbl...
              |I do the following:
              |
              | Select Case mDataType(mColu mnToSort)
              |
              | Case TypeCode.Int32
              |
              | Result = CInt(xText).Com pareTo(CInt(yTe xt))
              |
              | Case TypeCode.String
              |
              | Result=...
              |
              |
              |
              | I wonder if I could somehow write one statement that would compare all
              types
              | using mDataType(mColu mnToSort) to automate the conversion?
              |
              |


              Comment

              • **Developer**

                #8
                Re: How to compare objects using their typecode

                That's great. Just what I wanted!

                One aside, Some numbers as text are of format 123,456,789

                If I used Cint it is smart enough to handle them.

                But Convert.Change to Int32 is not.
                I can make this a special case but do you happen to know a type that
                supports numbers like that?


                Thanks a lot


                "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                news:%234mBM9%2 3eFHA.580@TK2MS FTNGP15.phx.gbl ...[color=blue]
                > **Developer**
                > In addition to the other comments.
                >
                > I would use something like:
                >
                > Dim dataType As TypeCode
                > Dim xText, yText As String
                > Dim xObject, yObject As Object
                > Dim result As Integer
                >
                > xObject = Convert.ChangeT ype(xText, dataType)
                > yObject = Convert.ChangeT ype(yText, dataType)
                >
                > result = Comparer.Defaul t.Compare(xObje ct, yObject)
                >
                > - or -
                >
                > result = Comparer.Defaul tInvariant.Comp are(xObject, yObject)
                >
                >
                > Convert.ChangeT ype will change the text variable to the requested type
                > code
                > (Int32 for example). Be certain to review ChangeType's overloads!
                >
                > Comparer.Defaul t does the comparison based on Thread.CurrentC ulture, while
                > Comparer.Defaul tInvariant does the comparison based on
                > CultureInfo.Inv ariantCulture.
                >
                > Hope this helps
                > Jay
                >
                >
                > " **Developer**" <REMOVEdevelope r@a-znet.com> wrote in message
                > news:OeePuR1eFH A.4040@TK2MSFTN GP14.phx.gbl...
                > |I do the following:
                > |
                > | Select Case mDataType(mColu mnToSort)
                > |
                > | Case TypeCode.Int32
                > |
                > | Result = CInt(xText).Com pareTo(CInt(yTe xt))
                > |
                > | Case TypeCode.String
                > |
                > | Result=...
                > |
                > |
                > |
                > | I wonder if I could somehow write one statement that would compare all
                > types
                > | using mDataType(mColu mnToSort) to automate the conversion?
                > |
                > |
                >
                >[/color]


                Comment

                • Jay B. Harlow [MVP - Outlook]

                  #9
                  Re: How to compare objects using their typecode

                  **Developer**,
                  Have you looked at the overloads to ChangeType? One of them accepts a format
                  provider, Is there a format provider you can pass that will ignore the
                  commas?

                  If I get a chance to later I will look. Post if you find something.

                  Hope this helps
                  Jay

                  " **Developer**" <REMOVEdevelope r@a-znet.com> wrote in message
                  news:%23bZIsN$e FHA.2700@tk2msf tngp13.phx.gbl. ..
                  | That's great. Just what I wanted!
                  |
                  | One aside, Some numbers as text are of format 123,456,789
                  |
                  | If I used Cint it is smart enough to handle them.
                  |
                  | But Convert.Change to Int32 is not.
                  | I can make this a special case but do you happen to know a type that
                  | supports numbers like that?
                  |
                  |
                  | Thanks a lot
                  |
                  |
                  | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                  | news:%234mBM9%2 3eFHA.580@TK2MS FTNGP15.phx.gbl ...
                  | > **Developer**
                  | > In addition to the other comments.
                  | >
                  | > I would use something like:
                  | >
                  | > Dim dataType As TypeCode
                  | > Dim xText, yText As String
                  | > Dim xObject, yObject As Object
                  | > Dim result As Integer
                  | >
                  | > xObject = Convert.ChangeT ype(xText, dataType)
                  | > yObject = Convert.ChangeT ype(yText, dataType)
                  | >
                  | > result = Comparer.Defaul t.Compare(xObje ct, yObject)
                  | >
                  | > - or -
                  | >
                  | > result = Comparer.Defaul tInvariant.Comp are(xObject, yObject)
                  | >
                  | >
                  | > Convert.ChangeT ype will change the text variable to the requested type
                  | > code
                  | > (Int32 for example). Be certain to review ChangeType's overloads!
                  | >
                  | > Comparer.Defaul t does the comparison based on Thread.CurrentC ulture,
                  while
                  | > Comparer.Defaul tInvariant does the comparison based on
                  | > CultureInfo.Inv ariantCulture.
                  | >
                  | > Hope this helps
                  | > Jay
                  | >
                  | >
                  | > " **Developer**" <REMOVEdevelope r@a-znet.com> wrote in message
                  | > news:OeePuR1eFH A.4040@TK2MSFTN GP14.phx.gbl...
                  | > |I do the following:
                  | > |
                  | > | Select Case mDataType(mColu mnToSort)
                  | > |
                  | > | Case TypeCode.Int32
                  | > |
                  | > | Result = CInt(xText).Com pareTo(CInt(yTe xt))
                  | > |
                  | > | Case TypeCode.String
                  | > |
                  | > | Result=...
                  | > |
                  | > |
                  | > |
                  | > | I wonder if I could somehow write one statement that would compare all
                  | > types
                  | > | using mDataType(mColu mnToSort) to automate the conversion?
                  | > |
                  | > |
                  | >
                  | >
                  |
                  |


                  Comment

                  • **Developer**

                    #10
                    Re: How to compare objects using their typecode


                    "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_MVP @msn.com> wrote in message
                    news:OzB1nW$eFH A.2732@TK2MSFTN GP14.phx.gbl...[color=blue]
                    > **Developer**,
                    > Have you looked at the overloads to ChangeType? One of them accepts a
                    > format
                    > provider, Is there a format provider you can pass that will ignore the
                    > commas?[/color]

                    I'll check but if that doesn't work out I just pass Nothing or Empty or
                    something to indicate comma seperated integer. I'd maybe make it the Option
                    default value but I like String for that.
                    [color=blue]
                    >
                    > If I get a chance to later I will look. Post if you find something.[/color]
                    Don't spend any more time on it. I really appreciate the help.
                    [color=blue]
                    >
                    > Hope this helps
                    > Jay
                    >[color=green]
                    >> | But Convert.Change to Int32 is not.[/color]
                    > | I can make this a special case but do you happen to know a type that
                    > | supports numbers like that?
                    > |[/color]
                    All I had to do is check the TypeCode enum and I could answer my own
                    question: No there isn't.


                    Comment

                    Working...