473,546 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Relational Operator from String

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
12 2468
On Jun 25, 2:11 pm, ujjc...@gmail.c om 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(thi s 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
On Jun 25, 2:05*pm, Mick Wilson <mick.wil...@gm ail.comwrote:
On Jun 25, 2:11 pm, ujjc...@gmail.c om 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(thi s 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
On Jun 25, 2:13*pm, ujjc...@gmail.c om wrote:
On Jun 25, 2:05*pm, Mick Wilson <mick.wil...@gm ail.comwrote:


On Jun 25, 2:11 pm, ujjc...@gmail.c om 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(thi s 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
On Jun 25, 2:15*pm, ujjc...@gmail.c om wrote:
On Jun 25, 2:13*pm, ujjc...@gmail.c om wrote:


On Jun 25, 2:05*pm, Mick Wilson <mick.wil...@gm ail.comwrote:
On Jun 25, 2:11 pm, ujjc...@gmail.c om 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(thi s 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
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
On Jun 25, 3:13*pm, ujjc...@gmail.c om wrote:
On Jun 25, 2:05*pm, Mick Wilson <mick.wil...@gm ail.comwrote:


On Jun 25, 2:11 pm, ujjc...@gmail.c om 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(thi s 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
On Jun 25, 3:41*pm, Mick Wilson <mick.wil...@gm ail.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
On Jun 25, 3:19 pm, ujjc...@gmail.c om 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
On Jun 25, 3:02*pm, Mick Wilson <mick.wil...@gm ail.comwrote:
On Jun 25, 3:19 pm, ujjc...@gmail.c om 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

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

Similar topics

2
6392
by: wongjoekmeu | last post by:
Hello All, I have a question about a C++ listing that I don't understand from a book that I use to learn C++. In the listing a class String is declared and defined. The beginning look like this ---------- #include <iostream> #include <string.h> using namespace std; Class String
49
3279
by: Mike MacSween | last post by:
I frequently hear that there isn't a commercially available dbms that fully implements the relational model. Why not? And which product comes closest. Mike MacSween
49
14862
by: raju | last post by:
hi can we compare two integers without using relational operators (== != < <= > >=) thanks rajesh s
1
1246
by: Locia | last post by:
I would like compare all type. I try with this function but I get System.NullReference.Exception if pass in Compare function two int type. Why relational operation isn't defined for basic type as int? How can I do? private bool Compare(Object leftOperand,String op,Object rightOperand) { if (op.Equals(">"))
2
1204
by: Steve | last post by:
I'm confused with creating the == operator for my class. It appears that c# requires the signature: public static bool operator==(Type a, Type b); I want to use: public bool operator==(Type RightHandSide) { return this.SomeMember = RightHandSide.SomeMember; }
11
1455
by: Janus | last post by:
Hi, This is a very basic question. What will happen if there is a statement with a relational operator outside while, for, or if conditions? Something like this... ..
14
2541
by: AliceB.Toklas | last post by:
In my Absolute Beginner's Guide to C book by Greg Perry where it is instruction how relational operators work it gives the following example: int i = 5; so the following statement is true: int i == 1
1
1300
by: jsparks57 | last post by:
I want to cin two characters for any of the six relational operators. '>=' no problem or '!=' no problem. The problem is for reading single character '<' and '>'. My code wants to read two characters but what works for a second character for a single character relational operator. The blank character ' ' is not accepted. Maybe I should...
13
1594
by: sulyokpeti | last post by:
I have made a simple python module to handle SQL databases: https://fedorahosted.org/pySQLFace/wiki Its goal to separate relational database stuff (SQL) from algorythmic code (python). A SQLFace is a facade initialized with a configuration file (XML). It provides callable command objects for each sql query. The call substitutes template...
0
7504
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7694
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.