473,378 Members | 1,346 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,378 software developers and data experts.

Comparing elegantly when there may be a type mismatch

I'm handing the sort event of a DataGridView, because if I don't handle it,
there's an exception whenever I try to sort a column whose elements are not
all the same type.

I'd like to write a routine which:

- Tries to sort each pair of elements using the comparer that is appropriate
to their type;
- If they are not the same type or there is no comparer, then does a
ToString() and compares the strings.

I have a kluge working (which tries several common types one by one), but
what is the most elegant way to do this?
May 30 '06 #1
3 1171
"Michael A. Covington" <lo**@ai.uga.edu.for.address> wrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
I'd like to write a routine which:

- Tries to sort each pair of elements using the comparer that is
appropriate to their type;
- If they are not the same type or there is no comparer, then does a
ToString() and compares the strings.

I have a kluge working (which tries several common types one by one), but
what is the most elegant way to do this?


Actually, here is something I came up with that seems to fill the bill.
e.CellValue1 and e.CellValue2 are objects to be compared; they are of
unknown type but are usually common things (int, double, or string) and are
usually of the same type. Comments would be appreciated.

Type t = e.CellValue1.GetType();
if (e.CellValue2.GetType() == t && t is IComparable)
{
e.SortResult = (e.CellValue1 as IComparable).CompareTo(e.CellValue2);
}
// Otherwise compare their string representations
else
{
e.SortResult = System.String.Compare(
e.CellValue1.ToString(), e.CellValue2.ToString());
}

May 30 '06 #2
Michael A. Covington wrote:
"Michael A. Covington" <lo**@ai.uga.edu.for.address> wrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
I'd like to write a routine which:

- Tries to sort each pair of elements using the comparer that is
appropriate to their type;
- If they are not the same type or there is no comparer, then does a
ToString() and compares the strings.

I have a kluge working (which tries several common types one by one), but
what is the most elegant way to do this?
Actually, here is something I came up with that seems to fill the bill.
e.CellValue1 and e.CellValue2 are objects to be compared; they are of
unknown type but are usually common things (int, double, or string) and are
usually of the same type. Comments would be appreciated.

Type t = e.CellValue1.GetType();
if (e.CellValue2.GetType() == t && t is IComparable)
{
e.SortResult = (e.CellValue1 as IComparable).CompareTo(e.CellValue2);
}
// Otherwise compare their string representations
else
{
e.SortResult = System.String.Compare(
e.CellValue1.ToString(), e.CellValue2.ToString());
}


'Twas the way I originally thought but what behaviour do you want in the
case of float and int?
maybe:

.... else
{ double a, b;
if(double.TryParse(e.CellValue1.ToString(), out a) &&
double.TryParse(e.CellValue2.ToString(), out b))
{
e.SortResult = a.CompareTo(b);
}
else
{ e.SortResult = System.String.Compare(
e.CellValue1.ToString(), e.CellValue2.ToString());
}

}

Always depends if you want this behaviour and it might introduce another
level of complexity in terms of culture specific parsing etc...
May 30 '06 #3
Good suggestion. Mixed floating-point and integer values might be fairly
common, in fact. Thanks!

"John B" <jb******@yahoo.com> wrote in message
news:44**********@news.iprimus.com.au...
Michael A. Covington wrote:
"Michael A. Covington" <lo**@ai.uga.edu.for.address> wrote in message
news:%2******************@TK2MSFTNGP03.phx.gbl...
I'd like to write a routine which:

- Tries to sort each pair of elements using the comparer that is
appropriate to their type;
- If they are not the same type or there is no comparer, then does a
ToString() and compares the strings.

I have a kluge working (which tries several common types one by one),
but what is the most elegant way to do this?


Actually, here is something I came up with that seems to fill the bill.
e.CellValue1 and e.CellValue2 are objects to be compared; they are of
unknown type but are usually common things (int, double, or string) and
are usually of the same type. Comments would be appreciated.

Type t = e.CellValue1.GetType();
if (e.CellValue2.GetType() == t && t is IComparable)
{
e.SortResult = (e.CellValue1 as
IComparable).CompareTo(e.CellValue2);
}
// Otherwise compare their string representations
else
{
e.SortResult = System.String.Compare(
e.CellValue1.ToString(), e.CellValue2.ToString());
}


'Twas the way I originally thought but what behaviour do you want in the
case of float and int?
maybe:

...
else
{

double a, b;
if(double.TryParse(e.CellValue1.ToString(), out a) &&
double.TryParse(e.CellValue2.ToString(), out b))
{
e.SortResult = a.CompareTo(b);
}
else
{
e.SortResult = System.String.Compare(
e.CellValue1.ToString(), e.CellValue2.ToString());
}

}

Always depends if you want this behaviour and it might introduce another
level of complexity in terms of culture specific parsing etc...

May 30 '06 #4

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

Similar topics

1
by: LJgrnl | last post by:
I've got a type mismatch error that's driving me nutty. Variable blnNoData has the initial value False. If a recordset comes back empty (both .EOF and ..BOF are true) then blnNoData is set to...
2
by: Jim H | last post by:
I'm trying to get a bunch of records based on client id and a date range. I keep getting an error when I enclose my date string in quotes in the where cleause. The error is: Microsoft JET...
1
by: webstuff | last post by:
Hi, I'm getting a 'Type mismatch' exception when calling the Word.Application.Documents.Open method when using the Office XP 2003 PIAs. the actual error is: ...
19
by: Dennis | last post by:
I have a public variable in a class of type color declared as follows: public mycolor as color = color.Empty I want to check to see if the user has specified a color like; if mycolor =...
5
by: daniel.hedz | last post by:
I am generating a usercontrol dynamically successfully, but when I try to find that usercontrol I get a type mismatch. This is what I am doing: //Loading my usercontrol...
7
by: Nevil Lesdog | last post by:
What do you think is the best way to handle a compiler warning about comparing an unsinged value to a singed value? Cast to silence it? Disable that warning altogether? Or just live with it? On...
3
by: vishwaskothari | last post by:
What is wrong in the following Statement? Dim mydate As Date Format(mydate, "m/d/yy") rs.Open "SELECT * from accounts where date='" & mydate & "';", con, adOpenStatic, adLockOptimistic
2
by: cobraun | last post by:
I have a subroutine that is intended to check if a string is contained anywhere within another string. compare_strings($string1,$string2); sub compare_strings { my...
4
by: jupi13 | last post by:
i have this code..i don't know what where is the error in this one..it says data type mismatch..... Dim Mydate As Date Dim MydateString As String MydateString = "Text1.Text" Mydate =...
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...
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...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.