How to compare objects using their typecode

  • Thread starter Thread starter **Developer**
  • Start date Start date
D

**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?
 
**Developer** said:
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
 
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?
 
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
 
**Developer** said:
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
 
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
 
**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


|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?
|
|
 
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
 
**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

| 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
|
|
| | > **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
| >
| >
| > | > |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?
| > |
| > |
| >
| >
|
|
 
Jay B. Harlow said:
**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

| 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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top