Relational Operator from String

  • Thread starter Thread starter ujjc001
  • Start date Start date
U

ujjc001

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?
 
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.
 
If you're going to make a method call anyway, an easier to read
approach might be to make extension methods that behave similarly.

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.
 
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
 
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.
 
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.
 
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.
 
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.
 
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.
 
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
 
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.
 
Jeroen said:
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.
 
Should be "this int x" to get extensioney goodness. Or badness, rather.

Well, I'm not using 3.0 but hey, that's pretty sweet! Thanks for the
ideas.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top