Home All Groups Group Topic Archive Search About

How to compare objects using their typecode

Author
27 Jun 2005 8:10 PM
**Developer**
I do the following:

Select Case mDataType(mColumnToSort)

Case TypeCode.Int32

Result = CInt(xText).CompareTo(CInt(yText))

Case TypeCode.String

Result=...



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

Author
27 Jun 2005 8:29 PM
Armin Zingler
Show quote Hide quote
" **Developer**" <REMOVEdevelo***@a-znet.com> schrieb
> I do the following:
>
> Select Case mDataType(mColumnToSort)
>
> Case TypeCode.Int32
>
> Result = CInt(xText).CompareTo(CInt(yText))
>
> Case TypeCode.String
>
> Result=...
>
>
>
> I wonder if I could somehow write one statement that would compare
> all types using mDataType(mColumnToSort) to automate the conversion?
>
>



result = directcast(xtext, icomparable).comparto(ytext)

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


Armin
Author
27 Jun 2005 11:43 PM
**Developer**
Select Case mDataType(mColumnToSort)
Case TypeCode.Int32
    Result = CInt(xText).CompareTo(CInt(yText))
Case TypeCode.Single
    Result = CSng(xText).CompareTo(CSng(yText))
Case TypeCode.Double
    Result = CDbl(xText).CompareTo(CDbl(yText))
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

Show quoteHide quote
>>
>> I wonder if I could somehow write one statement that would compare
>> all types using mDataType(mColumnToSort) to automate the conversion?
>>
>>
>
>
>
> result = directcast(xtext, icomparable).comparto(ytext)
>
> Assumed that xtext implements IComparable. IComparable is there to support
> the concept of comparing objects.
>
>
> Armin
>
Author
28 Jun 2005 12:46 AM
Armin Zingler
Show quote Hide quote
" **Developer**" <REMOVEdevelo***@a-znet.com> schrieb
>
> Select Case mDataType(mColumnToSort)
> Case TypeCode.Int32
>    Result = CInt(xText).CompareTo(CInt(yText))
> Case TypeCode.Single
>    Result = CSng(xText).CompareTo(CSng(yText))
> Case TypeCode.Double
>    Result = CDbl(xText).CompareTo(CDbl(yText))
> 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.

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
Author
28 Jun 2005 12:58 AM
**Developer**
> As there is no general parse method for all data types,
> you will have to handle them individually.

That what I was wondering about.
Thanks
Author
27 Jun 2005 11:39 PM
**Developer**
Added a litte more
I do the following:

Select Case mDataType(mColumnToSort)
Case TypeCode.Int32
    Result = CInt(xText).CompareTo(CInt(yText))
Case TypeCode.Single
    Result = CSng(xText).CompareTo(CSng(yText))
Case TypeCode.Double
    Result = CDbl(xText).CompareTo(CDbl(yText))
Case TypeCode.String
    Result=...



I wonder if I could somehow write one statement that would compare all
types
using mDataType(mColumnToSort) to automate the conversion?
Author
28 Jun 2005 2:40 PM
Jay B. Harlow [MVP - Outlook]
**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.ChangeType(xText, dataType)
        yObject = Convert.ChangeType(yText, dataType)

        result = Comparer.Default.Compare(xObject, yObject)

- or -

        result = Comparer.DefaultInvariant.Compare(xObject, yObject)


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

Comparer.Default does the comparison based on Thread.CurrentCulture, while
Comparer.DefaultInvariant does the comparison based on
CultureInfo.InvariantCulture.

Hope this helps
Jay


Show quoteHide quote
" **Developer**" <REMOVEdevelo***@a-znet.com> wrote in message
news:OeePuR1eFHA.4040@TK2MSFTNGP14.phx.gbl...
|I do the following:
|
| Select Case mDataType(mColumnToSort)
|
| Case TypeCode.Int32
|
| Result = CInt(xText).CompareTo(CInt(yText))
|
| Case TypeCode.String
|
| Result=...
|
|
|
| I wonder if I could somehow write one statement that would compare all
types
| using mDataType(mColumnToSort) to automate the conversion?
|
|
Author
28 Jun 2005 3:09 PM
**Developer**
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


Show quoteHide quote
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@msn.com> wrote in message
news:%234mBM9%23eFHA.580@TK2MSFTNGP15.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.ChangeType(xText, dataType)
>        yObject = Convert.ChangeType(yText, dataType)
>
>        result = Comparer.Default.Compare(xObject, yObject)
>
> - or -
>
>        result = Comparer.DefaultInvariant.Compare(xObject, yObject)
>
>
> Convert.ChangeType will change the text variable to the requested type
> code
> (Int32 for example). Be certain to review ChangeType's overloads!
>
> Comparer.Default does the comparison based on Thread.CurrentCulture, while
> Comparer.DefaultInvariant does the comparison based on
> CultureInfo.InvariantCulture.
>
> Hope this helps
> Jay
>
>
> " **Developer**" <REMOVEdevelo***@a-znet.com> wrote in message
> news:OeePuR1eFHA.4040@TK2MSFTNGP14.phx.gbl...
> |I do the following:
> |
> | Select Case mDataType(mColumnToSort)
> |
> | Case TypeCode.Int32
> |
> | Result = CInt(xText).CompareTo(CInt(yText))
> |
> | Case TypeCode.String
> |
> | Result=...
> |
> |
> |
> | I wonder if I could somehow write one statement that would compare all
> types
> | using mDataType(mColumnToSort) to automate the conversion?
> |
> |
>
>
Author
28 Jun 2005 3:25 PM
Jay B. Harlow [MVP - Outlook]
**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

Show quoteHide quote
" **Developer**" <REMOVEdevelo***@a-znet.com> wrote in message
news:%23bZIsN$eFHA.2700@tk2msftngp13.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_***@msn.com> wrote in message
| news:%234mBM9%23eFHA.580@TK2MSFTNGP15.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.ChangeType(xText, dataType)
| >        yObject = Convert.ChangeType(yText, dataType)
| >
| >        result = Comparer.Default.Compare(xObject, yObject)
| >
| > - or -
| >
| >        result = Comparer.DefaultInvariant.Compare(xObject, yObject)
| >
| >
| > Convert.ChangeType will change the text variable to the requested type
| > code
| > (Int32 for example). Be certain to review ChangeType's overloads!
| >
| > Comparer.Default does the comparison based on Thread.CurrentCulture,
while
| > Comparer.DefaultInvariant does the comparison based on
| > CultureInfo.InvariantCulture.
| >
| > Hope this helps
| > Jay
| >
| >
| > " **Developer**" <REMOVEdevelo***@a-znet.com> wrote in message
| > news:OeePuR1eFHA.4040@TK2MSFTNGP14.phx.gbl...
| > |I do the following:
| > |
| > | Select Case mDataType(mColumnToSort)
| > |
| > | Case TypeCode.Int32
| > |
| > | Result = CInt(xText).CompareTo(CInt(yText))
| > |
| > | Case TypeCode.String
| > |
| > | Result=...
| > |
| > |
| > |
| > | I wonder if I could somehow write one statement that would compare all
| > types
| > | using mDataType(mColumnToSort) to automate the conversion?
| > |
| > |
| >
| >
|
|
Author
28 Jun 2005 4:36 PM
**Developer**
"Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@msn.com> wrote in message
news:OzB1nW$eFHA.2732@TK2MSFTNGP14.phx.gbl...
> **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?

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.

>
> If I get a chance to later I will look. Post if you find something.
Don't spend any more time on it. I really appreciate the help.

>
> Hope this helps
> Jay
>
>> | 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?
> |
All I had to do is check the TypeCode enum and I could answer my own
question:  No there isn't.