473,654 Members | 3,096 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7482

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*******@hotm ail.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_o f_string1 >=
integer_value_o f_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*********@an gelfire.com> wrote in message
news:0t******** *************** *********@4ax.c om...
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*******@hotm ail.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_o f_string1 >=
integer_value_o f_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(num ber1);
int n2 = Int32.Parse(num ber2);

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
1886
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 24-28, 200) Organization committee: Roel Wuyts (primary contact - roel.wuyts@ulb.ac.be), Gilad Bracha, Wolfgang De Meuter, Stéphane Ducasse and Oscar Nierstrasz.
3
2010
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 with the application. When a new component is shipped, the application 'picks up' that component and drops it in to the interface (in this case a GUI button as well as an associated class with that gui buttion). The application then uses the...
3
1312
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 below... --
4
4641
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 stop by in here. Crystal's knowledgebase has made available a bunch of examples for both ASP.Net and VB.Net applications for download and review (I added the links at the end of this post). One of the examples is how to pass in a parameter to...
4
2480
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 desirable. I have a web service that, based on an input parameter, dynamically loads A, B or C library. I also understand that an separate AppDomain is needed to explicitly unload the Assembly. No other apps will use these assemblies. Do I need...
9
3735
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 execute this function dynamically. So I have a function list in database written as a string. I am now looking for function or mechanism which will execute function dynamically. I am here Giving a example.
3
2197
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 SQL-Select-string and a type as parameters. It will then return a collection of objects of the specified type. (I have ~30 object types that will be fetched in exactly the same way from one table each). Dynamic parsing works fine using the Reflection...
2
3632
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
2356
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 or another (could also be 3) different web servers to use these Web Services. The Web Services are identical on all the machines. I tried just changing the URL of the Web Services and cannot make it work. I then decided to create 2 identical web...
0
8376
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8815
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8708
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8489
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
5622
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2716
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
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.