Solved

Creating a case statement that will stop printing the same value

Posted on 2004-08-20
25
184 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
Comment Utility
where are you setting newTotalPremium?

0
 

Author Comment

by:dmullis
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
can you post the entire code

-Ian
0
 

Author Comment

by:dmullis
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 1

Expert Comment

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

Author Comment

by:dmullis
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

771 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