By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,467 Members | 1,448 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,467 IT Pros & Developers. It's quick & easy.

Relational Operator from String

P: n/a
Here's one for ya. I want to create a relational operator from a
string object, i.e. I want to somehow be able to say:
string opString = ">=";
int i1 = "20";
int i2 = "10";

if (i1 somemethodhere(opString) i2) {
//do the true here
}
else {
//do the false here
}

Don't ask why :)
Don't want a clunky switch statement.
Thoughts?
Jun 27 '08 #1
Share this Question
Share on Google+
12 Replies


P: n/a
On Jun 25, 2:11 pm, ujjc...@gmail.com wrote:
if (i1 somemethodhere(opString) i2) {
//do the true here}
If you're going to make a method call anyway, an easier to read
approach might be to make extension methods that behave similarly.

------------
static bool GreaterThan(this string c)
{
//your custom comparison code
}
------------

from calling code:
------------
if (i1.GreaterThan(i2)) {
//
} else { ; }
------------

However, if your comparison requirements are as simple as your
example, you can probably get away with something easier.
Jun 27 '08 #2

P: n/a
On Jun 25, 2:05*pm, Mick Wilson <mick.wil...@gmail.comwrote:
On Jun 25, 2:11 pm, ujjc...@gmail.com wrote:
if (i1 somemethodhere(opString) i2) {
//do the true here}

If you're going to make a method call anyway, an easier to read
approach might be to make extension methods that behave similarly.

------------
static bool GreaterThan(this string c)
{
* * //your custom comparison code}

------------

from calling code:
------------
if (i1.GreaterThan(i2)) {
* * //} else { ; }

------------

However, if your comparison requirements are as simple as your
example, you can probably get away with something easier.
The problem is that I don't know what the operator will be. It could
be "=" or ">" or ">=" I would still have to have a switch statement
to know which method to run based on the string input of opString.
Jun 27 '08 #3

P: n/a
On Jun 25, 2:13*pm, ujjc...@gmail.com wrote:
On Jun 25, 2:05*pm, Mick Wilson <mick.wil...@gmail.comwrote:


On Jun 25, 2:11 pm, ujjc...@gmail.com wrote:
if (i1 somemethodhere(opString) i2) {
//do the true here}
If you're going to make a method call anyway, an easier to read
approach might be to make extension methods that behave similarly.
------------
static bool GreaterThan(this string c)
{
* * //your custom comparison code}
------------
from calling code:
------------
if (i1.GreaterThan(i2)) {
* * //} else { ; }
------------
However, if your comparison requirements are as simple as your
example, you can probably get away with something easier.

The problem is that I don't know what the operator will be. *It could
be "=" or ">" or ">=" *I would still have to have a switch statement
to know which method to run based on the string input of opString.- Hide quoted text -

- Show quoted text -
also, I don't want "custom comparison code", I literally want to
compare with the opString value i.e. if I say:
if (i1 opString i2)...
I just want a bool result of the comparison when used with the
operator that is in the string. (Can you store an operator as a
property? Tried, couldn't figure out).
Jeff
Jun 27 '08 #4

P: n/a
On Jun 25, 2:15*pm, ujjc...@gmail.com wrote:
On Jun 25, 2:13*pm, ujjc...@gmail.com wrote:


On Jun 25, 2:05*pm, Mick Wilson <mick.wil...@gmail.comwrote:
On Jun 25, 2:11 pm, ujjc...@gmail.com wrote:
if (i1 somemethodhere(opString) i2) {
//do the true here}
If you're going to make a method call anyway, an easier to read
approach might be to make extension methods that behave similarly.
------------
static bool GreaterThan(this string c)
{
* * //your custom comparison code}
------------
from calling code:
------------
if (i1.GreaterThan(i2)) {
* * //} else { ; }
------------
However, if your comparison requirements are as simple as your
example, you can probably get away with something easier.
The problem is that I don't know what the operator will be. *It could
be "=" or ">" or ">=" *I would still have to have a switch statement
to know which method to run based on the string input of opString.- Hide quoted text -
- Show quoted text -

also, I don't want "custom comparison code", I literally want to
compare with the opString value i.e. if I say:
if (i1 opString i2)...
I just want a bool result of the comparison when used with the
operator that is in the string. *(Can you store an operator as a
property? Tried, couldn't figure out).
Jeff- Hide quoted text -

- Show quoted text -
maybe this is more clear
return (i1 opString i2);
which I know wont work, need to figure out how to take opString and
convert it to an operator.
Jun 27 '08 #5

P: n/a
maybe this is more clear
return (i1 opString i2);
which I know wont work, need to figure out how to take opString and
convert it to an operator.
You're going to have to write *some* custom code. The only operators
defined for string, string (according to Reflector) are == and != .
Even if you could somehow get the compiler to know that

(i1 opString i2)

was really

(i1 >= i2)

....it still wouldn't what that means.
Jun 27 '08 #6

P: n/a
On Jun 25, 3:13*pm, ujjc...@gmail.com wrote:
On Jun 25, 2:05*pm, Mick Wilson <mick.wil...@gmail.comwrote:


On Jun 25, 2:11 pm, ujjc...@gmail.com wrote:
if (i1 somemethodhere(opString) i2) {
//do the true here}
If you're going to make a method call anyway, an easier to read
approach might be to make extension methods that behave similarly.
------------
static bool GreaterThan(this string c)
{
* * //your custom comparison code}
------------
from calling code:
------------
if (i1.GreaterThan(i2)) {
* * //} else { ; }
------------
However, if your comparison requirements are as simple as your
example, you can probably get away with something easier.

The problem is that I don't know what the operator will be. *It could
be "=" or ">" or ">=" *I would still have to have a switch statement
to know which method to run based on the string input of opString.- Hide quoted text -

- Show quoted text -
HI,

Have you think about how difficult it can be for somebody else to know
the new meaning of your operator?
IMO it's better to simply use a method.
Jun 27 '08 #7

P: n/a
On Jun 25, 3:41*pm, Mick Wilson <mick.wil...@gmail.comwrote:
You're going to have to write *some* custom code. The only operators
defined for string, string
Sorry. Misread your first post. I see now that you're comparing ints.
I was confused by the double quotes around the numbers.
Jun 27 '08 #8

P: n/a
On Jun 25, 3:19 pm, ujjc...@gmail.com wrote:
maybe this is more clear
return (i1 opString i2);
which I know wont work, need to figure out how to take opString and
convert it to an operator.
Think about it this way. At runtime, you want to have that string
converted to an operator. But at compile time, your compiler sees
this:

return (someInt someString someInt)

Until you make your statements valid for the parser, any thoughts on
how to handle your situation at runtime aren't really worth worrying
about. It's probably best to use the switch statement that you had
earlier rejected or some equivalent, as inelegant as they might look.

Jun 27 '08 #9

P: n/a
On Jun 25, 3:02*pm, Mick Wilson <mick.wil...@gmail.comwrote:
On Jun 25, 3:19 pm, ujjc...@gmail.com wrote:
maybe this is more clear
return (i1 opString i2);
which I know wont work, need to figure out how to take opString and
convert it to an operator.

Think about it this way. At runtime, you want to have that string
converted to an operator. But at compile time, your compiler sees
this:

return (someInt someString someInt)

Until you make your statements valid for the parser, any thoughts on
how to handle your situation at runtime aren't really worth worrying
about. It's probably best to use the switch statement that you had
earlier rejected or some equivalent, as inelegant as they might look.
Mick, Sorry about the double quotes, was just typing and they came
out :)
You're right on here, I guess what I was wondering is if there was
some convert out there that could do this. Or, some other crazy thing
I don't know about.
I guess switch will have to do (on top of all this, I want to have
dynamic data types in there too). It's in the works now so I'll just
give up on anything elegent for the conversion of a string to an
operator. Thanks for the help though!
Jeff
Jun 27 '08 #10

P: n/a
uj*****@gmail.com wrote:
Here's one for ya. I want to create a relational operator from a
string object, i.e. I want to somehow be able to say:
string opString = ">=";
int i1 = "20";
int i2 = "10";

if (i1 somemethodhere(opString) i2) {
//do the true here
}
else {
//do the false here
}

Don't ask why :)
Don't want a clunky switch statement.
Thoughts?
OK, "no clunky switch statement", we can do that. Here's some C# 3.0:

static class IntExtensions {
static readonly IDictionary<string, Func<int, int, bool>operators =
new Dictionary<string, Func<int, int, bool>{
{ "<", (x, y) =x < y },
{ "=", (x, y) =x == y },
{ ">", (x, y) =x y }
};

public static bool Compare(int x, string opstring, int y) {
return operators[opstring](x, y);
}
}

To be used as, for example,

string opString = ">";
int i1 = 20;
int i2 = 10;
if (i1.Compare(opString, i2)) {
...
}

However, this is more an exercise in coolness than it is useful. I wouldn't
advocate extensions methods here; a dictionary of delegates is the central
idea. If you're going to actually process expressions in string form more
complicated than this, you may want to take a look at proper parsing. Search
the web; any example that builds a command-line calculator in C# will
outline approaches.

--
J.
Jun 27 '08 #11

P: n/a
Jeroen Mostert wrote:
static class IntExtensions {
public static bool Compare(int x, string opstring, int y) {
Should be "this int x" to get extensioney goodness. Or badness, rather.

--
J.
Jun 27 '08 #12

P: n/a
On Jun 25, 6:52*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
Jeroen Mostert wrote:
* static class IntExtensions {
* * public static bool Compare(int x, string opstring, int y) {

Should be "this int x" to get extensioney goodness. Or badness, rather.

--
J.
Well, I'm not using 3.0 but hey, that's pretty sweet! Thanks for the
ideas.
Jun 27 '08 #13

This discussion thread is closed

Replies have been disabled for this discussion.