Solved

if condition

Posted on 2014-10-14
4
70 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 15

Accepted Solution

by:
Haris Djulic earned 167 total points
ID: 40379846
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
ID: 40379849
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
ID: 40379857
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 43

Assisted Solution

by:Chris Stanyon
Chris Stanyon earned 166 total points
ID: 40380565
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

Major Incident Management Communications

Major incidents and IT service outages cost companies millions. Often the solution to minimizing damage is automated communication. Find out more in our Major Incident Management Communications infographic.

Question has a verified solution.

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

Suggested Solutions

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

738 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