471,318 Members | 3,212 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,318 software developers and data experts.

creating a dynamic if statement via reflection

I have 3 strings containing "10" , "20", ">="

would it be possible via reflection to generate a dynamic if statement
that return true or false and testing it like this

if ( 10 >= 20 )

so a string becomes a test of 2 integers

and how would i code this?
Jan 19 '06 #1
5 7224

Digital Fart wrote:
I have 3 strings containing "10" , "20", ">="

would it be possible via reflection to generate a dynamic if statement
that return true or false and testing it like this

if ( 10 >= 20 )

so a string becomes a test of 2 integers

and how would i code this?


Well, the easy answer is that 10 isn't greater than or equal to 20, so
you don't need that if statement :)

But seriously, we need to know what the actual inputs are. Do you have:

- a string containing the representation of an integer, another such,
and the string ">="
- a string containing the representation of an integer, another such,
and a string containing a C# comparison operator
- a string containing the representation of some kind of number,
another such, and a string containing a C# comparison operator
- three arbitrary strings that when concatenated yield an expression
with a boolean value
- three arbitrary strings that when concatenated might yield an
expression with a boolean value

or something else?

--
Larry Lard
Replies to group please

Jan 19 '06 #2
On 19 Jan 2006 06:08:55 -0800, "Larry Lard" <la*******@hotmail.com>
wrote:

3 strings that contain a value that represent an integer
( i read them from a flatfile )
the operator is something the user can enter as a string

so
string1 = "10"
string2 = "20"
operatorstring = ">="

now i need to know if integer_value_of_string1 >=
integer_value_of_string2

does this make more sence?


Digital Fart wrote:
I have 3 strings containing "10" , "20", ">="

would it be possible via reflection to generate a dynamic if statement
that return true or false and testing it like this

if ( 10 >= 20 )

so a string becomes a test of 2 integers

and how would i code this?


Well, the easy answer is that 10 isn't greater than or equal to 20, so
you don't need that if statement :)

But seriously, we need to know what the actual inputs are. Do you have:

- a string containing the representation of an integer, another such,
and the string ">="
- a string containing the representation of an integer, another such,
and a string containing a C# comparison operator
- a string containing the representation of some kind of number,
another such, and a string containing a C# comparison operator
- three arbitrary strings that when concatenated yield an expression
with a boolean value
- three arbitrary strings that when concatenated might yield an
expression with a boolean value

or something else?


Jan 19 '06 #3
bb
what about using an eval class (similar to the javascript one)

an example of which is here http://www.osix.net/modules/article/?id=761

Jan 19 '06 #4
Hi,

"Digital Fart" <pr*********@angelfire.com> wrote in message
news:0t********************************@4ax.com...
I have 3 strings containing "10" , "20", ">="

would it be possible via reflection to generate a dynamic if statement
that return true or false and testing it like this

if ( 10 >= 20 )

so a string becomes a test of 2 integers

and how would i code this?


you could use some eval class, I think I saw a link a time ago here, look in
the archives.

otherwise if you know that you have two string that can be converted to
integer you could do a Convert.ToInt32 with them, using the third string (
the operator) as the expression of a switch clause, after all the operators
are a finite number.

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Jan 19 '06 #5

Digital Fart wrote:
On 19 Jan 2006 06:08:55 -0800, "Larry Lard" <la*******@hotmail.com>
wrote:

3 strings that contain a value that represent an integer
( i read them from a flatfile )
the operator is something the user can enter as a string

so
string1 = "10"
string2 = "20"
operatorstring = ">="

now i need to know if integer_value_of_string1 >=
integer_value_of_string2

does this make more sence?


Sure does!

Use Int32.Parse to convert the strings to ints; then just switch on the
operator and perform the appropriate comparison:

// validation? error handling? not in this sample!
bool Compare(string number1, string number2, string op)
{
int n1 = Int32.Parse(number1);
int n2 = Int32.Parse(number2);

switch(op)
{
case "==":
return (n1 == n2);
case "!=":
return (n1 != n2);
//etc
}
}

--
Larry Lard
Replies to group please

Jan 19 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by jonathan | last post: by
3 posts views Thread by Stephen Gennard | last post: by
4 posts views Thread by Andrew | last post: by
4 posts views Thread by BrianS | last post: by
9 posts views Thread by Kishor | last post: by
3 posts views Thread by Jakob Lithner | last post: by
2 posts views Thread by Luis Arvayo | last post: by
16 posts views Thread by tshad | last post: by
reply views Thread by rosydwin | last post: by

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.