Solved

if condition

Posted on 2014-10-14
4
61 Views
Last Modified: 2014-10-18
please code below, i need to use code below less if statements, is look not properway or not very professional to use this much If conditions, i belive may be another way .

            if (ComplainType == "Total")
            {


                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyTots";
            }


            if (ComplainType == "ManualKit")
            {


                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyMan
            }


            if (ComplainType == "iComplaints")
            {


                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyIcom
            }


            if (ComplainType == "InsComplaints")
            {


                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyINS
            }
0
Comment
Question by:ukerandi
4 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 167 total points
Comment Utility
try like this:

switch (ComplainType )
{
    case "Total":
            SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyTots";
        break;
    case "ManualKit":
        SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyMan";
        break;
    case "iComplaints":
           SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyIcom";
        break;
    case "InsComplaints":
                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyINS";
        break;
    default:
   
        break;
}

Open in new window

0
 
LVL 34

Assisted Solution

by:Beverley Portlock
Beverley Portlock earned 167 total points
Comment Utility
I am not a C# programmer, but I think you need to be looking at switch statement

switch ( ComplainType ) {

     case "Total":
          SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyTots";
          break;

     case "ManualKit":
          SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyMan
          break;

   ... and so on
}

Open in new window


See http://msdn.microsoft.com/en-GB/library/06tc147t.aspx for examples
0
 
LVL 4

Expert Comment

by:javaftper
Comment Utility
You should use a case statement for faster execution-

// ... Switch on the string.
	switch (ComplainType)
	{
	    case "Total":
                SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyTots";
		break;
	    case "ManualKit":
		  SqlTotalComplaints = "select count(*) as YearlyTotalComplaint from NumberOfComplaints_YearlyMan";
		break;
	    case "etc":
		etc;
		break;
	}

Open in new window

0
 
LVL 42

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 166 total points
Comment Utility
As your SQL statements are all pretty much the same, here's a completely different approach to using switch statements:

Dictionary<string, string> ComplaintTables = new Dictionary<string, string>();

ComplaintTables.Add("Total", "YearlyTots");
ComplaintTables.Add("ManualKit", "YearlyMan");
ComplaintTables.Add("iComplaints", "YearlyIcom");
ComplaintTables.Add("InsComplaints", "YearlyINS");

SqlTotalComplaints = String.Format("select count(*) as YearlyTotalComplaint from NumberOfComplaints_{0}", ComplaintTables[ComplainType]);

Open in new window

Always good to have options ;)
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

772 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

10 Experts available now in Live!

Get 1:1 Help Now