Solved

if condition

Posted on 2014-10-14
4
71 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

630 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