Solved

Build a Conditional If structure  dynamically!

Posted on 2014-02-18
2
265 Views
Last Modified: 2014-02-18
I want to allow users a  simple search  with  2 textboxes and one combo box

[                   ]  or/and [              ]

My conditional If  would be something like this:

string sele = "";
if   (operator.Text == "or")
                            {
                            sele = "text.Contains(textbox1) == true || text.Contains(textbox2)";
                            }
                        else
                            {
                            sele = "text.Contains(textbox1) == true && text.Contains(textbox2)";
                            }

                                 // now I would like to use      
                            if (sele)                   ----------> not allowed in c#
                               {
                                   lot a code here
                               }

Any help for a Proof of Concept programmer?
0
Comment
Question by:novreisb
2 Comments
 
LVL 35

Accepted Solution

by:
Robert Schutt earned 500 total points
ID: 39867579
I see 2 options:

1 - make sele a boolean and assign without the quotes
bool sele = false;
if   (operator.Text == "or")
                            {
                            sele = text.Contains(textbox1.Text) || text.Contains(textbox2.Text);
                            }
                        else
                            {
                            sele = text.Contains(textbox1.Text) && text.Contains(textbox2.Text);
                            }

                            if (sele)
                               { 
                                   //lot a code here
                               }

Open in new window

2 - use boolean logic to make 1 big if statement (of which only 1 part will be executed so no waste of resources)
if (
    ( operator.Text == "or"  && (text.Contains(textbox1.Text) || text.Contains(textbox2.Text) ) )
    ||
    ( operator.Text == "and" && (text.Contains(textbox1.Text) && text.Contains(textbox2.Text) ) )
)
                               { 
                                   //lot a code here
                               }

Open in new window

0
 

Author Closing Comment

by:novreisb
ID: 39867878
Perfect!
Sometimes we put an idea in your head and forget other options
thks Robert
0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Citrix XenApp, Internet Explorer 11 set to Enterprise Mode and using central hosted sites.xml file.
I use more than 1 computer in my office for various reasons. Multiple keyboards and mice take up more than just extra space, they make working a little more complicated. Using one mouse and keyboard for all of my computers makes life easier. This co…
The viewer will learn how to successfully download and install the SARDU utility on Windows 8, without downloading adware.
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question