473,320 Members | 1,950 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to compare objects using their typecode

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?
Nov 21 '05 #1
9 2996
" **Developer**" <RE*************@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

Nov 21 '05 #2
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?

Nov 21 '05 #3

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

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

Nov 21 '05 #4
" **Developer**" <RE*************@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

Nov 21 '05 #5
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
Nov 21 '05 #6
**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**" <RE*************@a-znet.com> wrote in message
news:Oe**************@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?
|
|
Nov 21 '05 #7
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]" <Ja************@msn.com> wrote in message
news:%2*****************@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**" <RE*************@a-znet.com> wrote in message
news:Oe**************@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?
|
|

Nov 21 '05 #8
**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**" <RE*************@a-znet.com> wrote in message
news:%2****************@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]" <Ja************@msn.com> wrote in message
| news:%2*****************@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**" <RE*************@a-znet.com> wrote in message
| > news:Oe**************@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?
| > |
| > |
| >
| >
|
|
Nov 21 '05 #9

"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:Oz**************@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.
Nov 21 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Matt | last post by:
I'm new to Java but experienced with PL/SQL. I've found what appears to be strange behaviour (a bug?) when attempting to create java stored objects using the UNIX version of Oracle SQL*PLUS...
1
by: Dany | last post by:
I am trying to find a nice way to compare objects (tables, colums, indexes, primary and fk) between my production and my test dabatase. I would like to known if their are tricks or tools...
8
by: Kenneth Baltrinic | last post by:
I am trying to compare values coming out of a database record with known default values. The defaults are in an array of type object (because they can be of any basic data type, I am not working...
2
by: Frank | last post by:
I have 2 objects that have the same complex structure and I would like to know if a member has change and which one(s). For example: public struct Person { public long m_nID; public...
6
by: tony | last post by:
Hello! When exactly is it important or advisable to use this form load event handler compare to using the C-tor. For example here I create an event handler called dataBoundGridForm that is...
0
by: mailsandiproy | last post by:
case: class employee { int roll; string name; } just try to sort an array of 10 objects using the name field ( asceding / descending)
3
sammyboy78
by: sammyboy78 | last post by:
I'm trying to display an array of objects using a GUI. My instructions are that the CD class and it's sublcass don't need to change I just need to modify class CDInventory to include the GUI. I'm not...
1
by: nagarwal | last post by:
hi frnds, I am facing a prblm in displaying the contents of an ArrayList which contains 'String' Objects using logic:iterate tag.. example: in the Action : String s=null; String s1=null;...
1
by: vai | last post by:
hi all, i'm developing a stock management system. in this system i'm developing a billing system which on daily & monthly basis. i'm using ms-acess database as backend & visual baic as front end....
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.