Solved

Creating a case statement that will stop printing the same value

Posted on 2004-08-20
25
185 Views
Last Modified: 2010-04-15
I am new to programming and the C# language. I thought I knew how to make a simple switch/case statement but, I could not get anything I tried to work. I think I am confused in that I feel I need the if statement inside each case in order to get correct reults, and I am not sure what to call what, and what is  = to what now .

The if statements below loop through different companys looking at the total premium for each , once it finds a match for the company the user selected on the website, it displays the total on the web page. It works right now except, when it matches the first company, "Company A" on the list and prints it to the page, that printed amount never changes again no matter what company it matches after that, so I think I need a break  after each statement. Shouldn't a correctly coded switch help my problem? Could someone show me how you would code these if statements as a switch? I'm guessing my switch() would be (company) but I couldn't get it to work. I tried the case statement below and it still  printed then premium for the first company on the list and not for the others.

The code does loop through each company. Even my case statement breaks then loops through each company
anyway? It may print the premium for a different company the first time around but the second time, as soon as the first company, (Company A) in the case or in the if statements is hit and printed, it never changes again regardless of which company is hit.  The switch and case statements that I tried are below the if statement code that follows:


                                                                         if (company == "Company A")

                              {
                                    
                                    quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                                     
                              
                              }

                              if (company == "Company B.")
                              {
                                    quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                              }
                   
                              if (company == "Company C")
                              {
                                    quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                              }
                   

                              if (company == "Company D")
                              {
                                    quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                              }
                   
                              if (company == "Company E")
                              {
                                    quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                              }
                              
                              if (company == "Company F")
                              {
                                    quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                              }
                               
                        }

                                                                  My switch code below - that doesn't help:


                                                                      switch(company)
                              {

                                    case"Company A":
                                                      
                                    quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                                          
                                    break;
                              

                                    case"Company B":                              
                                                                                                                       
                                    quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";

                                    break;  
           
                                    case"Company C":
                              
                                    quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                                          
                                    break;                

                                    case "Company D":

                                            quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";

                                    break;
                   
                                    case "Company E":

                                            quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                                    
                                    break;

                                    case "Company F":

                                             quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                                          
                                    break;
                               
                              }
0
Comment
Question by:dmullis
  • 12
  • 5
  • 4
  • +1
25 Comments
 
LVL 10

Expert Comment

by:ibost
ID: 11853276
where are you setting newTotalPremium?

0
 

Author Comment

by:dmullis
ID: 11853398
This is the first half of thate code above - on the same page:

                                                           foreach (XmlNode xNode in xmlNode)
                  {
                        //dmullis- The commented out variables below will be used later.
                        string broker;
                        string company;                        
                        string totalPremium;
                        double itotalPremium = 0.0;
                        double newTotalPremium;
                        //double premium = 0.0;
                        double ibroker;                        
                        //Set Total Premium
                        company = (xNode.Attributes.GetNamedItem("companyname").Value);
                        broker = xNode.Attributes.GetNamedItem("BrokerFee").Value;
                        ibroker = System.Convert.ToDouble(broker);
                        totalPremium = (xNode.Attributes.GetNamedItem("totalpremium").Value);
                        itotalPremium = System.Convert.ToDouble(totalPremium);
                        newTotalPremium = itotalPremium + ibroker;      
                        newTotalPremium = System.Math.Round(newTotalPremium);
                        // Is this needed?
                        string infoid = xNode.Attributes.GetNamedItem("id").Value;

                              
                        if (user.CurrentQuote == "auto")

                        {
                              
                              switch(company)
0
 
LVL 10

Expert Comment

by:ibost
ID: 11853406
the body of each if as well as in the case is exactly the same.

It looks the equivalent of saying something like:
If (a) then print 'hello'
else if (b) then print 'hello'
else if (c) then print 'hello'
etc etc

Assuming newTotalPremium is something like a float or int, you need to set it to whatever company's premium you want.  In fact if you are querying somewhere to get the company, just get their premium at the same time and you don't need an if/else at all.



0
 
LVL 10

Expert Comment

by:ibost
ID: 11853493
Are you sure you need the if/else or the switch at all?

Did the user enter some value they are searching for?

How about just say

if (txtUserEnteredCompany.Text == company) {
   quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
}
0
 

Author Comment

by:dmullis
ID: 11853534
The user is presented with a web page of total premiums. He then clicks a detail button  beside the one Market/Company he wants a breakdown on.  A new "detail" page opens up with a break down that he can print, but right now that page does not display a total so the code above goes through to find out what company he clicked and match it to that companies total premium then displays it, it but it keeps displaying Company A , the first company listed.
0
 

Author Comment

by:dmullis
ID: 11853587
It might display the chosen companies premium the first time around, but once it hits company A, it never displays another companies premium again so I need to do something with the if statements or the case statement to overcome this..clear that value out or something.. I guess.
0
 
LVL 10

Expert Comment

by:ibost
ID: 11853655
can you post the entire code

-Ian
0
 

Author Comment

by:dmullis
ID: 11853863
Here it is:

foreach (XmlNode xNode in xmlNode)
                  {
                        //dmullis- The commented out variables below will be used later.
                        string broker;
                        string company;                        
                        string totalPremium;
                        double itotalPremium = 0.0;
                        double newTotalPremium;
                        //double premium = 0.0;
                        double ibroker;                        
                        //Set Total Premium
                        company = (xNode.Attributes.GetNamedItem("companyname").Value);
                        broker = xNode.Attributes.GetNamedItem("BrokerFee").Value;
                        ibroker = System.Convert.ToDouble(broker);
                        totalPremium = (xNode.Attributes.GetNamedItem("totalpremium").Value);
                        itotalPremium = System.Convert.ToDouble(totalPremium);
                        newTotalPremium = itotalPremium + ibroker;      
                        newTotalPremium = System.Math.Round(newTotalPremium);
                        // Is this needed?
                        string infoid = xNode.Attributes.GetNamedItem("id").Value;

                              
                        if (user.CurrentQuote == "auto")

                        {
                              
                               if (company == "Company A")

                         {
                             
                              quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                               
                         
                         }

                         if (company == "Company B.")
                         {
                              quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                         }
                   
                         if (company == "Company C")
                         {
                              quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                         }
                   

                         if (company == "Company D")
                         {
                              quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                         }
                   
                         if (company == "Company E")
                         {
                              quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                         }
                         
                         if (company == "Company F")
                         {
                              quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                         }
                         
                    }

                        }

                  }
                        
                  //end TT#516, TT#526      
                        
0
 
LVL 10

Expert Comment

by:ibost
ID: 11853907
instead of comparing (company == "Company X")

Don't you want to compare what the user selected?

if (userSelection == "Company X") ...


0
 

Author Comment

by:dmullis
ID: 11854097
The code works far as matching the correct company premium to the correct company and displaying it on the page. I can see that in debugging and as I stated above, it will print the correct premium for the correct company on the page unless it hits Company A..the first of my if statements..which is either when the user chooses Company A or just the second time around . ..then it displays the premium for company A here on out regardless of what company is chosen after it hits Company A. The value for company A is never cleared out . So it may land on the correct company premium and only execute that if statement while debugging the code but if Company A already displayed a premium on this "detail" page, it never changes to the correct company 's premium after that..even though I see the statement executing on the company the user chose, the correct company's value, the value that the if statement was correctly execurted upon, will not be displayed on the web page..Company A's value is displayed.
0
 

Author Comment

by:dmullis
ID: 11854332
Just to clarify, it the actual code Company A-F are real company names, each a different company name. I am not literally using Company A-F but the actual company names in the if and case statements, I just didn't think I should put the real company names used in my code out on the web, so I named them Company A-F.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Expert Comment

by:chmohan
ID: 11854534
try this  
string company = (xNode.Attributes.GetNamedItem("companyname").Value).ToString();
0
 

Author Comment

by:dmullis
ID: 11854923
I tried string company = (xNode.Attributes.GetNamedItem("companyname").Value).ToString();. It works just like my original code. I choose company  on the website. My code goes through each case or if statement until it correctly matches the company I chose, then it executes the statement in the case or the if statement correctly. This works. There is no problem matching companies. What doesn't work is once it executes or even passes by my very first if statement(Company A), that will now and forever be the only premium that displays on the page .

 If I chose company B, the code loops through and finds an if statement or case that matches company b, then it ONLY executes that if statement for company B which is all correct. It will then display the "newTotalPremium" on the web page for only Company B. That is correct. I then shut down the web page and bring it back up. I go through every page then go through this process again. This time I choose company A. It loops through my code and it correctly displays the "newTotalPremium" value for company A . I then shut down the web page and bring it back up. I go through every page then go through this process again. This time I chose company C. It loops through my code it and I can see in debugging that  it is only executing the correct statements for Company C  and the "newTotalPremium" value is indeed the correct value for company C . This works...but when I look on the web page, there is the value for company A because I chose Company A before I chose Company C so now if I go through shutting down the webpage and bringing back up and choosing any other company B-F, I never see anything displayed on the web page but the value for Company A. I can't get rid of it.  I need a way to clear it out or something. I thing it is odd that it is only Company A which stops the show and it is the first of my if statements.
0
 
LVL 1

Expert Comment

by:chmohan
ID: 11855763
you have to refresh the page right? else how will the page know that it has to change the data after selecting a?
0
 

Author Comment

by:dmullis
ID: 11855789
Maybe that is the problem. I will have to research how to do that. Is there a pretty standard C# way to do that? I know it is probably something easy that I just haven't had to use yet.
0
 

Author Comment

by:dmullis
ID: 11855986
On second thought, I don't think that is it because the user clicks a button called " detail" then is taken to a new page, the page I have my statements on,  so it is automatically a new page.
0
 
LVL 2

Expert Comment

by:Delani
ID: 11856756
Is the line
>> quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";<<
a dummy line? I can't believe you have exactly the same statement for each company there, you probably intend to put special processing dependent on company there. Otherwise, it's just silly. Regarding the detail page: maybe the browser or a proxy is inadvertently caching it. Maybe you could shed a little more light on the context of your code?
0
 

Author Comment

by:dmullis
ID: 11863718
The statement "quoteCell.InnerText =  newTotalPremium.ToString()+ ".00"; gets and prints only the totalpremium value for the particular company that the user chooses. The user chooses a company button( actually a 'quote detail" button for a company), clicks on it and a new page opens with a breakdown of charges. This page needs to have te total printed to it as well. The code maybe repetitive and could probably be written better but I am a beginner in programming and that code works as it is now. In debugging it gets only the correct totalpremium for only the company selected and I have seen it print other correct values to the page as long I as I don't hit company A. I feel the problem is I don't know how in C# to put an additional statement there to clear out the Company A's value once it hits that company and prints it to the page. I cleared out my cookies, files and history and I am still stuck with the value printed on the page for Comapany A regardless of what the next company chosen may be. I feel the problem is in the case of if statement in general or I need a statement of some sort to clear each value out after they have been hit, specifically the first value in my if or switch. The code inside each does what it is supposed to far as matching the correct company totalpremium to the company the user chooses even though it may be written more repetitvly than necessary.
0
 
LVL 2

Expert Comment

by:Delani
ID: 11864352
If that's true, we can reduce the code you have to the following:

foreach (XmlNode xNode in xmlNode)
               {
                    //dmullis- The commented out variables below will be used later.
                    string broker;
                    string company;                    
                    string totalPremium;
                    double itotalPremium = 0.0;
                    double newTotalPremium;
                    //double premium = 0.0;
                    double ibroker;                    
                    //Set Total Premium
                    company = (xNode.Attributes.GetNamedItem("companyname").Value);
                    broker = xNode.Attributes.GetNamedItem("BrokerFee").Value;
                    ibroker = System.Convert.ToDouble(broker);
                    totalPremium = (xNode.Attributes.GetNamedItem("totalpremium").Value);
                    itotalPremium = System.Convert.ToDouble(totalPremium);
                    newTotalPremium = itotalPremium + ibroker;    
                    newTotalPremium = System.Math.Round(newTotalPremium);
                    // Is this needed?
                    string infoid = xNode.Attributes.GetNamedItem("id").Value;

                         
                    if (user.CurrentQuote == "auto")
                    {
                              quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";                                                  
                    }


               }

Now we are getting somewhere. I think what you want now, is only to assign to the quoteCell when you hit the selected company. But where is the variable that specifies which company is selected? That would make the if-statement:

                    if (user.CurrentQuote == "auto" && company == companyRequestedByUser)
                    {
                              quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";                                                  
                    }

Where I introduce the variable companyRequestedByUser which should hold the name of the company the user is interested in. (You can replace this with the correct variable that holds the selected company).
0
 

Author Comment

by:dmullis
ID: 11869259
In my if or switch, I have each company listed out. When the variable "companyname" ( =  company) which is what the user chooses hit s the company named in one of the if satements, it executes  quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";  for only the matching company name. See below- this code works.


company = (xNode.Attributes.GetNamedItem("companyname").Value); //"company" is what the users chooses

switch(company)
{
                                    
case"Explorer Ins. - Preferred":
                                          
quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                                          
break;

case"ABC Insurance Co.":
                              
quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
break;        
     
case"Bobs Crazy Insurance Co.":
                              
quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                                          
break;                

case "Insurance R US Casualty Co.":
                                          
                                          
quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                                          
break;
                   
case "West Coast National":
                               
quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                                          
break;

case "Navagator Ins.Co.":

quoteCell.InnerText =  newTotalPremium.ToString()+ ".00";
                                          
break;

This code works in that it matches the correct value that the user chooses, gets the correct total and prints it to the page. This is what I want to happen, however after it hits the first company: case"Explorer Ins. - Preferred":, the value(total) for only this company stays displayed on the web page  no matter what I do or what company is selected, after I close and re-open the web page and go through the process of choosing a different  company.The other companys work fine until I hit the first in my case statement.
0
 
LVL 2

Accepted Solution

by:
Delani earned 500 total points
ID: 11873249
Hello Dmullis,

Why have a switch or if-construction when all branches give you the same result? Whatever company is selected, clearly you want to do the same thing - assigning the newTotalPremium to the quoteCell. You say the company variable already holds the user's selection, however, you extract it every time from the current xmlnode, which doesn't make sense to me at this point.

These examples are equivalent:

// example 1:
doFunction();

// example 2:
if (variable == true) {
  doFunction();
}
if (variable == false) {
  doFunction();
}

Either way, independent of the value of the variable, the same action is taken. Such an if or case statement then is unnecessary and can be removed.
Please take another look at my previous post - I think you misunderstand the mechanics of case/if.

Kind regards,

Delani
0
 

Author Comment

by:dmullis
ID: 11874261
Yes, that code is more consise and works as well but I still have my original problem and have discovered it is not the "first company in a case or if statement " that prints to the web page then never changes, it is the first company I choose regardless.

I have to get new code to start over and test it because now this displayed value of the company I chose first will never change to the next and different company I choose while testing. Basically, I have the same problem, just a little less code.
0
 
LVL 2

Expert Comment

by:Delani
ID: 11874564
What you are doing in your code is this:

you have a bunch of xmlnodes, and with those, you do the following:
- extract all kinds of interesting values from a node and put them into variables
- if user.CurrentQuote == "Auto", you put the newTotalPremium into quotecell.

What you need in that if-statement (previous comment), is a test whether the xml-node currently under examination in the foreach statement, is the right company. The company variable gets it's value from the current xml-node, so that's not right. You have to have the company the user selected, and then compare that to the company value in the xmlnode.

Kind regards,

Delani
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

943 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

11 Experts available now in Live!

Get 1:1 Help Now