473,396 Members | 1,764 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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 7454

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Roel Wuyts | last post by:
CALL FOR CONTRIBUTIONS International Workshop on Revival of Dynamic Languages http://pico.vub.ac.be/~wdmeuter/RDL04/index.html (at OOPSLA2004, Vancouver, British Columbia, Canada, October...
3
by: jonathan | last post by:
hey all, I'd like to implement an application that is truly dynamic: the components are not only just known at runtime, but also the components are unknown at runtime: ie: some don't even ship...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
4
by: Andrew | last post by:
Hey all, Been working with the Crystal Report Viewer, and have run into a situation I am hoping someone can help me get past. This may be more of a CR question, but hoping for some CR gurus to...
4
by: BrianS | last post by:
What is the best strategy for dynamic loading private assemblies in asp.net? I understand, and have confirmed, that any dll placed in the app's /bin dir will get loaded on startup. This is not...
9
by: Kishor | last post by:
Hi all, I am Using VB.Net for developing my application. I am now needed help. In this project I have to execute some function, but I cannot call them directly using function name, I wanted to...
3
by: Jakob Lithner | last post by:
I have searched the news groups on similar subjects, but haven't found anything adequate for my need .... To save much duplication of code I would like to create a baseclass that takes a...
2
by: Luis Arvayo | last post by:
Hi, In c#, I need to dynamically create types at runtime that will consist of the following: - inherits from a given interface - will have a constructor with an int argument
16
by: tshad | last post by:
This is a little complicated to explain but I have some web services on a machine that work great. The problem is that I have run into a situation where I need to set up my program to access one...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
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...
0
BarryA
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.