473,378 Members | 1,314 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.

Interesting C# puzzle

JV
I have 3 string values that I want to perform a range comparison on:

string minValue = "1050";
string maxValue = "5300";
string theValue = "3239";

I have another string that tells me the type I need to convert the above
strings to before comparing:

string convertToType = "int";

The code cannot assume anything about the above values or the type that it
must convert to for the comparison, except that the type is a supported type
in C#. So, logically you would do something like:

object oMin = Convert.ChangeType(minValue, Type.GetType(convertToType));
// etc. giving you 3 object variables which are actually of type int

The problem is the comparison. You can't do the following because object
does not support the operators:

bool isInRange = (oValue >= oMin) && (oValue <= oMax);

So, other than creating a giant switch() statement with a case for each
possible type you care to support, how would you go about performing the
above comparison?
Apr 23 '06 #1
10 6793
JV <oy********@spammenot.com> wrote:

<snip>
The problem is the comparison. You can't do the following because object
does not support the operators:

bool isInRange = (oValue >= oMin) && (oValue <= oMax);

So, other than creating a giant switch() statement with a case for each
possible type you care to support, how would you go about performing the
above comparison?


I'd assume that the types involved implemented IComparable, and use
that after doing the conversion. Indeed, comparisons without advance
knowledge of the types involved is precisely what the IComparable
interface is for :)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 23 '06 #2
if you are sure that the type where you convert is IComparable you can do

IComparable cVal = (IComparable) oVal;
if (cVal.CompareTo(oMin) < 0) do something etc...

if you are not sure, you protect the construct in a try/catch.

/LM

"JV" <oy********@spammenot.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
I have 3 string values that I want to perform a range comparison on:

string minValue = "1050";
string maxValue = "5300";
string theValue = "3239";

I have another string that tells me the type I need to convert the above
strings to before comparing:

string convertToType = "int";

The code cannot assume anything about the above values or the type that it
must convert to for the comparison, except that the type is a supported
type in C#. So, logically you would do something like:

object oMin = Convert.ChangeType(minValue, Type.GetType(convertToType));
// etc. giving you 3 object variables which are actually of type int

The problem is the comparison. You can't do the following because object
does not support the operators:

bool isInRange = (oValue >= oMin) && (oValue <= oMax);

So, other than creating a giant switch() statement with a case for each
possible type you care to support, how would you go about performing the
above comparison?

Apr 23 '06 #3
Uh, isn't it kind of dumb not to use an int in the first place?

--
<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/


"JV" <oy********@spammenot.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
I have 3 string values that I want to perform a range comparison on:

string minValue = "1050";
string maxValue = "5300";
string theValue = "3239";

I have another string that tells me the type I need to convert the above
strings to before comparing:

string convertToType = "int";

The code cannot assume anything about the above values or the type that it
must convert to for the comparison, except that the type is a supported
type in C#. So, logically you would do something like:

object oMin = Convert.ChangeType(minValue, Type.GetType(convertToType));
// etc. giving you 3 object variables which are actually of type int

The problem is the comparison. You can't do the following because object
does not support the operators:

bool isInRange = (oValue >= oMin) && (oValue <= oMax);

So, other than creating a giant switch() statement with a case for each
possible type you care to support, how would you go about performing the
above comparison?

Apr 23 '06 #4
"Luc E. Mistiaen" <lu**********@advalvas.be.no.spam> wrote in message
news:ee**************@TK2MSFTNGP05.phx.gbl...
if you are sure that the type where you convert is IComparable you can do

IComparable cVal = (IComparable) oVal;
if (cVal.CompareTo(oMin) < 0) do something etc...

if you are not sure, you protect the construct in a try/catch.


Or provide a switch statement for those that aren't IComparable.

Michael
Apr 24 '06 #5
JV
Well, no it's not dumb. I just didn't give you the full context of the
problem. But, in this case it involves a string field containing candidate
key information which sometimes is all numeric and sometimes is not. The
rules being processed are pretty complex.
"clintonG" <cs*********@REMOVETHISTEXTmetromilwaukee.com> wrote
Uh, isn't it kind of dumb not to use an int in the first place?

--
<%= Clinton Gallagher
NET csgallagher AT metromilwaukee.com
URL http://clintongallagher.metromilwaukee.com/

Apr 24 '06 #6
JV
Thanks. I knew there had to be an interface that value types implemented.
I just wasn't quite sure how to go about finding out what it was.

I suppose I could also use the "is" operator rather than try/catch.

"Luc E. Mistiaen" <lu**********@advalvas.be.no.spam> wrote in message
news:ee**************@TK2MSFTNGP05.phx.gbl...
if you are sure that the type where you convert is IComparable you can do

IComparable cVal = (IComparable) oVal;
if (cVal.CompareTo(oMin) < 0) do something etc...

if you are not sure, you protect the construct in a try/catch.

/LM

"JV" <oy********@spammenot.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
I have 3 string values that I want to perform a range comparison on:

string minValue = "1050";
string maxValue = "5300";
string theValue = "3239";

I have another string that tells me the type I need to convert the above
strings to before comparing:

string convertToType = "int";

The code cannot assume anything about the above values or the type that
it must convert to for the comparison, except that the type is a
supported type in C#. So, logically you would do something like:

object oMin = Convert.ChangeType(minValue, Type.GetType(convertToType));
// etc. giving you 3 object variables which are actually of type int

The problem is the comparison. You can't do the following because object
does not support the operators:

bool isInRange = (oValue >= oMin) && (oValue <= oMax);

So, other than creating a giant switch() statement with a case for each
possible type you care to support, how would you go about performing the
above comparison?


Apr 24 '06 #7
> I suppose I could also use the "is" operator rather than try/catch.

Of course. I just thought this was a case that was not supposed to happen,
in which case exception are probably clearer.

/LM
Apr 24 '06 #8
> I suppose I could also use the "is" operator rather than try/catch.

But wouldn't that depend on what your definition of "is" is?

And what you do when you try and get caught?

--
Sorry, couldn't help myself,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.

"JV" <oy********@spammenot.com> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
Thanks. I knew there had to be an interface that value types implemented.
I just wasn't quite sure how to go about finding out what it was.

I suppose I could also use the "is" operator rather than try/catch.

<snip>
Apr 25 '06 #9
You should be aware that comparing string using IComparable is not the best
thing to do because you are not explicitly indicating how the strings are
compared.

The default CompareTo performs a culture-Sensitive comparation (I believe
base on the thread culture info). This could cause you problems if your
application is being used in multiples countries.

Just a heads up note.
"JV" <oy********@spammenot.com> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
I have 3 string values that I want to perform a range comparison on:

string minValue = "1050";
string maxValue = "5300";
string theValue = "3239";

I have another string that tells me the type I need to convert the above
strings to before comparing:

string convertToType = "int";

The code cannot assume anything about the above values or the type that it
must convert to for the comparison, except that the type is a supported
type in C#. So, logically you would do something like:

object oMin = Convert.ChangeType(minValue, Type.GetType(convertToType));
// etc. giving you 3 object variables which are actually of type int

The problem is the comparison. You can't do the following because object
does not support the operators:

bool isInRange = (oValue >= oMin) && (oValue <= oMax);

So, other than creating a giant switch() statement with a case for each
possible type you care to support, how would you go about performing the
above comparison?

May 15 '06 #10
The data types are your friends. Don't try to fight them.

switch(convertToType)
{
case "int";
int iMinValue = Convert.ToInt32(minValue);
int iMaxValue = Convert.ToInt32(maxValue):
int iValue = Convert.ToInt32(theValue)
bool isInRange = (iValue >= iMin) && (iValue <= oMax);
break;

case "string":
// etc.

}

May 15 '06 #11

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

Similar topics

1
by: Developwebsites | last post by:
Hi all, I've made a sliding puzzle game in shockwave which works just fine, except I dont know how to have it solve itself. the URL is: http://members.aol.com/rglukov/games/selfsolve.htm ...
1
by: xavier vazquez | last post by:
I have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of...
0
by: xavier vazquez | last post by:
have a problem with a program that does not working properly...when the program run is suppose to generate a cross word puzzle , when the outcome show the letter of the words overlap one intop of the...
5
by: ashish0799 | last post by:
HI I M ASHISH I WANT ALGORYTHMUS OF THIS PROBLEM Jigsaw puzzles. You would have solved many in your childhood and many people still like it in their old ages also. Now what you have got to do...
3
by: oncue01 | last post by:
Word Puzzle Task You are going to search M words in an N × N puzzle. The words may have been placed in one of the four directions as from (i) left to right (E), (ii) right to left (W), (iii) up...
6
by: Phoe6 | last post by:
Hi All, I would like to request a code and design review of one of my program. n-puzzle.py http://sarovar.org/snippet/detail.php?type=snippet&id=83 Its a N-puzzle problem solver ( Wikipedia page...
11
by: Renu | last post by:
Hi, Just click on this link n use ur common sence to navigate. It has 23 pages one after the other, starting from this first Page. The trick is to find a way to go to the next page. If u...
2
by: Gio | last post by:
I'm getting K&R (it's on the way), should I also get the Answer Book? And while I'm there, should I get the Puzzle Book? Or should I save the Puzzle Book for when I'm more advanced? - Gio ...
4
by: honey777 | last post by:
Problem: 15 Puzzle This is a common puzzle with a 4x4 playing space with 15 tiles, numbered 1 through 15. One "spot" is always left blank. Here is an example of the puzzle: The goal is to...
5
by: dmf1207 | last post by:
Hi All! I'm new to javascript and need a little help with a simple puzzle im trying to design. I have a 600x100 pixel picture that I have sliced into 6 100x100 rectangles making a table of of 6...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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.