9 Replies - 1797 Views - Last Post: 16 October 2014 - 01:02 PM Rate Topic: -----

#1 liverpooty   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-September 14

Help finding flaw in my "if" logic

Posted 15 October 2014 - 04:10 PM

Hey guys, here is problem. My code has no errors, but nothing comes out. Pretty sure I have something wrong somewhere.


When you mix red and blue, you get purple.
When you mix red and yellow, you get orange.
When you mix blue and yellow, you get green.
Design a program that prompts the user to enter the names of two primary colors to mix. If the user enters anything other than red, blue, or yellow, the program should display an error message. Otherwise, the program should display the name of the secondary color that results. You need to turn in Data Dictionary, Flow Chart or Storyboard.


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Calculate_Click(object sender, EventArgs e)
        {
            if (ColorBox1.Text != "red" || ColorBox1.Text != "blue" || ColorBox1.Text != "yellow"
                || ColorBox2.Text != "red" || ColorBox2.Text != "yellow" || ColorBox2.Text != "blue")
            {
                
            }
            else
            {

                //Calculating colors and seeing what color results


                if (ColorBox1.Text == "red" && ColorBox2.Text == "yellow") { ColorBox3.Text = "Orange"; }
                else if (ColorBox1.Text == "yellow" && ColorBox2.Text == "red") { ColorBox3.Text = "Orange"; }
                else if (ColorBox1.Text == "red" && ColorBox2.Text == "blue") { ColorBox3.Text = "Purple"; }
                else if (ColorBox1.Text == "blue" && ColorBox2.Text == "red") { ColorBox3.Text = "Purple"; }
                else if (ColorBox1.Text == "blue" && ColorBox2.Text == "yellow") { ColorBox3.Text = "Green"; }
                else if (ColorBox1.Text == "yellow" && ColorBox2.Text == "blue") { ColorBox3.Text = "Green"; }

            }

        }

        private void Exit_Click(object sender, EventArgs e)
        {
            this.Close();// Exits program
        }

    }

}




Thanks guys.

Is This A Good Question/Topic? 0
  • +

Replies To: Help finding flaw in my "if" logic

#2 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Help finding flaw in my "if" logic

Posted 15 October 2014 - 04:44 PM

I think debugging would help you here. Have you tried putting a messagebox in here?

if (ColorBox1.Text != "red" || ColorBox1.Text != "blue" || ColorBox1.Text != "yellow"

                || ColorBox2.Text != "red" || ColorBox2.Text != "yellow" || ColorBox2.Text != "blue")

            {

                 

            }


Was This Post Helpful? 0
  • +
  • -

#3 liverpooty   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-September 14

Re: Help finding flaw in my "if" logic

Posted 15 October 2014 - 04:52 PM

Yeah. Still couldn't get it to work. Just displays just that message box and doesn't display anything else when you mix the two colors.
Was This Post Helpful? 0
  • +
  • -

#4 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Help finding flaw in my "if" logic

Posted 15 October 2014 - 04:57 PM

Yup but what that helps you identify is that there is a logic error with your if statement.

Seriously cannot emphasize how important debugging is for programming.

Now that you've narrowed down where the issue is you can focus on what you're doing.

If you have to, get a pen and paper and draw your statement, it helps sometimes seeing it represented like that

Hint, you need brackets in your "if"
Was This Post Helpful? 0
  • +
  • -

#5 liverpooty   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 16-September 14

Re: Help finding flaw in my "if" logic

Posted 15 October 2014 - 05:08 PM

Im not sure. Its due in less than an hour. Ill figure something out.
Was This Post Helpful? 0
  • +
  • -

#6 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Help finding flaw in my "if" logic

Posted 15 October 2014 - 05:12 PM

Dude, read your if statement

You're saying if its not red or it's not blue or yellow

So your program goes

Hey it's not red, success execute whatever's in the block, hence your message box popping up.

You want to check that its not red and its not blue and it's not yellow
Was This Post Helpful? 1
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Help finding flaw in my "if" logic

Posted 16 October 2014 - 04:41 AM

Wait wait wait... "Its due in an hour"... Meaning this is course homework.

What school is this for? Please tell me its coding somewhere between 8th and 10th grade.
If this is for a university course that you are PAYING FOR then you need to drop it and get your money back. Nobody should be paying to go to a course where they are teaching you to do this kind of stuff that any $30 "Learn C# in a month" book would be better at. There are so many things conceptually wrong with this, that we try to tell students not to do.

Its not your fault, you're just doing what you're taught. But no teacher should be teaching you to do most of this stuff.

Like I said, I'm not getting on you. You only know what they're telling you. I just hate seeing people getting ripped off. This is all stuff I try to stop rookies from doing in the first week.

tlhIn`toq said:

Looking at your code you have some bad practices in play that need your immediate attention.
These will form bad habits quickly and should be worked on. This is part of what I mean by "learn foundation concepts first".

Problem 1: Use of GUI controls as your variables.
You have numerous places where you are doing things like: SomeMethodCall(TextBox43.Text);
Your textboxes and other GUI elements are not meant to be your variables to hold values. They exist only to be a way for the user to interact with the data held in the logic code *behind* the GUI. You should have properties bound to the GUI. When the user changes something in a textbox it updates the property in your code. When the property gets a new value, the GUI is updated. But your logic always uses the property not the GUI element.
There is a tutorial on properties linked in my signature block.

Problem 2: This bit about button1 and textbox14 is just horrible. If you are working in old 1980's WinForms for Win98 and WinXP programs then name your control as soon as you make it before doing anything else with it. txtbxFirstName is so much easier to maintain than textbox32. Same with "frmConnectionSettings" instead of "form2" or "wndCustomerDetails" than "window4". If you are making programs for current OSes like Win7/8 then you should be working in WPF where the controls don't need to be named 90% of the time, but the same practice of giving meaningful names should still be followed in the few cases where they are needed.

Problem 3: Don't put meaningful operational code in GUI control handlers. You have a lot of places where the work is being done in the button event handler instead if in a dedicated method. Instead you want that handler to just direct action to the first methods, not actually perform the actions.
btnSave_Click(object sender, eventargs e)
{
   SaveSettings(); // Call the save method
   SavePlayers();
   SaveWindowGeometry();
   // Do NOT put all the save code here
}

This way you can call the same save methods independently form code without having to either duplicate code (evil practice) or without calling the button handler. If you are using WinForms then you have a .Click event handler. If you are writting in WPF then don't call the .Click event but instead create Commands and activate those. The .Click handler in WPF is kind of a carry-over to give WinForms coders a comfortable migration path but is not considered the way a modern profession WPF coder works.

Problem 4:
Eye sore code.
Take the extra 2 minutes to clean up your code to make it easy to read, easy to follow, and most important, easy to manage.
  • Kill all those dead blank lines at the ends of methods.
  • Wrap groups of methods that go together in #region blocks so you can find all your sending code and receiving code easily for example.
  • Get your lines aligned. Control-K, Control-D will do that for you (Think of it as Klean Document)


Problem 5: Decent naming
Methods like private void ClientSession(object sender, eventargs e); don't follow established naming style guides.
A method performs an action. By convention for decades methods are named as verbs to show what they do. Nouns like ClientSession would be presumed to be a property by any other coder looking at your code. private void CreateClientSession(object sender, eventargs e); on the other hand shows that it does something (a method) not that it is something (a variable).

I'm not going to go into a long critique of your code. And I don't want you to feel like I'm just out to pick on you. I'm just pointing out a couple of many foundation concepts you didn't pickup up in the early stages of learning C#. You can't leap-frog the learning process by trying to build a program in your first month. Nobody can. Its not just you. Nobody in the first month of a new discipline has enough knowledge to jump in and start designing finished solutions: Not carpentry, not automotive design, not software engineering. C# is a foreign language with its own syntax. You aren't going to say "I know no Russian. I'm going to write a Russian mystery novel as my vehicle to learn the language." Same here with programming.

I urge you to take a step back. Pick up a "Learn C# in 30 days" type book and work your way through it cover to cover; don't use it a like a reference dictionary when you get stuck. There are several in a thread pinned to head of the C# forum. Plus there are countless C# beginner tutorial series around just like the C# Learning Series - tutorials here on DIC. There are also numerous "My First C# Program" tutorials like this one that will help you with the design and OOP concepts that are vital to good programming.

Was This Post Helpful? 1
  • +
  • -

#8 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Help finding flaw in my "if" logic

Posted 16 October 2014 - 12:04 PM

View PosttlhIn`toq, on 16 October 2014 - 11:41 PM, said:

If you are making programs for current OSes like Win7/8 then you should be working in WPF where the controls don't need to be named 90% of the time, but the same practice of giving meaningful names should still be followed in the few cases where they are needed.


I've seen this said before (I've started going though one of those $30 books and haven't quite got to the explanation yet), why do WPF controls not need to be as explicitly named?
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Help finding flaw in my "if" logic

Posted 16 October 2014 - 12:19 PM

The short answer for most of the reasons. There are others of course.

99% of the time they are displaying their data through binding to an object.
They aren't tightly coupled to the dataobject, and the code behind isn't directly updating them.
In other words, you don't have this in your code

txtbxFirstName.Text = "Fred";
txtboxLastName.Text = "Flintstone";


Same with their events. You don't tightly bind your logic code to the .Click event of a button. The button is wired to a COMMAND. The logic code has a command handler, but doesn't know where the command was raised from: Could have been a button, a menu, a TCP/IP call, a touchscreen gesture....

The entire point is to uncouple the GUI from the logic.
Was This Post Helpful? 1
  • +
  • -

#10 Damage   User is offline

  • Lord of Schwing
  • member icon

Reputation: 300
  • View blog
  • Posts: 1,989
  • Joined: 05-June 08

Re: Help finding flaw in my "if" logic

Posted 16 October 2014 - 01:02 PM

Ahh okay, gotcha. Thanks
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1