Solved

Build a Conditional If structure  dynamically!

Posted on 2014-02-18
2
262 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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

In our personal lives, we have well-designed consumer apps to delight us and make even the most complex transactions simple. Many enterprise applications, however, are a bit behind the times. For an enterprise app to be successful in today's tech wo…
Skype is a P2P (Peer to Peer) instant messaging and VOIP (Voice over IP) service – as well as a whole lot more.
The viewer will learn how to set up a document for the web and print and the recommended PPI for printing.
Viewers will learn how to use the Hootsuite Dashboard.

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now