|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
How to compare objects using their typecodeI 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?
Show quote
Hide quote
" **Developer**" <REMOVEdevelo***@a-znet.com> schrieb result = directcast(xtext, icomparable).comparto(ytext)> 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? > > Assumed that xtext implements IComparable. IComparable is there to support the concept of comparing objects. Armin 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 >
Show quote
Hide quote
" **Developer**" <REMOVEdevelo***@a-znet.com> schrieb If xText and yText are strings, you are right. What types do you expect? If> > 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. 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, That what I was wondering about.> you will have to handle them individually. Thanks 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? **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? | | 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? > | > | > > **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? | > | | > | | > | > | | "Jay B. Harlow [MVP - Outlook]" <Jay_Harlow_***@msn.com> wrote in message I'll check but if that doesn't work out I just pass Nothing or Empty or 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? something to indicate comma seperated integer. I'd maybe make it the Option default value but I like String for that. > Don't spend any more time on it. I really appreciate the help.> If I get a chance to later I will look. Post if you find something. > All I had to do is check the TypeCode enum and I could answer my own > 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? > | question: No there isn't. |
|||||||||||||||||||||||