Click here to Skip to main content
15,886,736 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
Hello all

I need help with this code for an assignment, it gives me an error when trying to use the else statement.

C#
public partial class Form1 : Form
   {
       public Form1()
       {
           InitializeComponent();
       }

       //Button click event
       private void btnPredict_Click(object sender, EventArgs e)
       {


           //If statments
           if (cmbMode.SelectedItem.Equals("Flashing") && cmbColour.SelectedItem.Equals ("Blue"))
           {
               lblOutput.Text = "Flashing Blue ; Clouds Due.";
           }
           else if (cmbMode.SelectedItem.Equals("Flashing") && cmbColour.SelectedItem.Equals ("Red"))
           {
               lblOutput.Text = "Flashing Red ; Snow Instead.";
           }
           else if (cmbMode.SelectedItem.Equals("Steady") && cmbColour.SelectedItem.Equals("Blue"))
           {
               lblOutput.Text = "Steady Blue ; Clear View.";
           }
           else if (cmbMode.SelectedItem.Equals("Steady") && cmbColour.SelectedItem.Equals("Red"))
           {
               lblOutput.Text = "Steady Red ; Rain Ahead.";
           }
           else
           {
               lblOutput.Text = "Broken Beacon light";

           }
Posted
Comments
Richard C Bishop 15-May-13 15:13pm    
You are kidding right? Do you think we can read your mind or access your HUD?
SwooshNeorPewPew 15-May-13 15:15pm    
Copy paste into Visual Studio?
SwooshNeorPewPew 15-May-13 15:16pm    
I thought there was just something basic wrong that could be answered from just looking at it, since I'm fairly new at this
Richard C Bishop 15-May-13 15:18pm    
So how do you suppose we tell you what the issue is if you do not supply the error message or even describe a problem?
SwooshNeorPewPew 15-May-13 15:21pm    
Again I thought it was just something basic that was wrong, error happens when i have no item selected in Application, I just want it to display the else lblOutput.Text, gives me "NullReferanceException was unhandled"

if (as you state in the remarks) no item is selected then cmbMode.SelectedItem is a null reference and you are not allowed to call its methods (like for instance cmbMode.SelectedItem.Equals) you may handle such a condition putting this line as the very first statement of your btnPredict_Click method:
C#
if ( cmbMode.SelectedItem == null) return;
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 15-May-13 16:18pm    
Good point, my 5, but it won't save the OP from the complete disaster experienced right now.
Please see my answer where I try to address the root of the problem.
—SA
The answers you received won't really help you to write good supportable code, because the idea is wrong from the very beginning. Like some other beginners, you are doing into a read dead end, which haunts many, these days: you tend to work with string representing objects and data, instead of working objects and data itself. This defeats the purpose of not just the OOP, but, in big part, programming itself.

Instead of
C#
if (cmbMode.SelectedValue.ToString() == "Flashing" && cmbColour.SelectedValue.ToString() == "Blue") //...
it should be something like
C#
enum CmbMode { Flashing, Steady, }
enum SignColor { Red, Blue, }

//...

CmbMode cmbMode = //...
SignColor color = //...

//...

bool snow = cmbMode == CmbMode.Flashing && color == SignColor.Red;


Are you getting the idea?

You may say that you get string from UI. Wrong. You should not. Let's assume this is a ListBox. Do you know that you can store any object as items, not strings? You may say that you need strings to show them properly in UI. Yes, but not by putting strings as items. Instead, create a wrapper class or structure and override System.ToString() to show some value. This is what the UI will actually show.

If you keep doing comparison of string and immediate "if" blocks like you do, forget programming; it's much better to do no programming than to do it the way you do, quite honestly.

—SA
 
Share this answer
 
v5
Comments
CPallini 15-May-13 16:27pm    
5, of course. Though, I guess, it is a bit 'advanced' for the OP, it is definitely the right path.
Sergey Alexandrovich Kryukov 15-May-13 16:39pm    
Thank you, Carlo.

"A bit advanced" is a bit better then nowhere :-)
The alternative is even better: OP should step back, not doing any UI until some more knowledge and confidence is acquired.
—SA
Change your if statements to something like this:
if(cmbMode.SelectedValue.ToString() == "Flashing" && cmbColour.SelectedValue.ToString() == "Blue")
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 15-May-13 16:15pm    
Formally, this is an answer, but... this is nothing good in it, honestly. Why helping to write such trash?..
Please see my answer to see what I mean.
—SA
Richard C Bishop 15-May-13 16:19pm    
I see your point. I do not understand the example you are suggesting though, can you provide a link to a resource that could help explain it? Thank you.
Sergey Alexandrovich Kryukov 15-May-13 16:21pm    
There is no a resource. This is elementary. However, I had a formatting bug I fixed, and then I added a paragraphs to explain further.
Please see. Better now?
—SA
Richard C Bishop 15-May-13 16:30pm    
Yes, thank you. I will study enums and begin to use them when appropriate.
Sergey Alexandrovich Kryukov 15-May-13 16:42pm    
I though you know enough about them. I though so looking at your comments which are usually very reasonable, showing experience, some wisdom.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900