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? 12 2458
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.
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.
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
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.
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.
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.
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.
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.
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 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.
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.
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
|
by: raju |
last post by:
hi
can we compare two integers without using relational operators (== != <
<= > >=)
thanks
rajesh s
|
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...
|
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...
|
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...
..
|
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:
...
|
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...
|
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...
|
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...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
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...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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
|
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...
|
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...
| |