8 Replies - 1311 Views - Last Post: 06 March 2015 - 07:53 PM Rate Topic: -----

#1 renzore101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 15

Problems with c# program being created for school.

Posted 03 March 2015 - 08:42 PM

Hello folks, I am new here, and to programming in general. This semester I am taking an introductory c# course and this is my first ever exposure to c# so I am definitely a noob! With that being said, I have a program (shown below) that I am having a couple issues with that are absolutely driving me crazy. First of all, within my program there are two check boxes that are not working correctly. I want them to individually adjust a given salary by 10%, and if they are both checked, by 15%. This however is not working exactly right. If I click the first button and then the second button, it works correctly. If I click the second button and then the first button it does not work correctly. In addition to that I am trying to compare the salaries to update a lead highest salary and it is not working correctly. The idea behind the if statement if (HitCountDecVariableComparison >= HitCountDecVariable)
is to compare the variables to continually update the lead salary text box with the highest salary. If anyone could shed some light on possible errors in my code that would be awesome! Thank you in advance for your time.


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;
using System.Globalization;

namespace TieredPlayerSalaryVialF___Robert_Renz
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        const decimal Tier1 = 7500;
        const decimal Tier2 = 10000;
        const decimal Tier3 = 12500;
        const decimal Tier4 = 15000;
        private decimal BeginningSalary = 0;
        private void CalcBtn_Click(object sender, EventArgs e)
        {
            bool TryParseHitCountBoolVariable;
            decimal HitCountDecVariable = 0;
            string PlayerName = "";
            string Tier = "";
            //decimal HitCountVar = 0;
            decimal Tier1VarAns = 0;
            decimal Tier2VarAns = 0;
            decimal Tier3VarAns = 0;
            decimal Tier4VarAns = 0;
            decimal HitCountDecVariableComparison = 0;
            decimal TierVariableComparison = 0;
            //decimal.TryParse(NumberOfHitsTxtBox.Text, out HitCountVar);
            
            TryParseHitCountBoolVariable = decimal.TryParse(NumberOfHitsTxtBox.Text, out HitCountDecVariable);
            
            if (string.IsNullOrWhiteSpace(PlayerNameTxtBox.Text))

            {
                MessageBox.Show("Error, please enter a valid player name!");
                //return;
            }
            else
            {
                PlayerName = PlayerNameTxtBox.Text;
            }
            
            

            if (TryParseHitCountBoolVariable)
            {
                
            }
            else
            {
                MessageBox.Show("Error, please enter a valid number of hits!");
                //return; -- I noticed that these returns were breaking the if statement below, the reason of which I do not yet fully understand.
            }




            if (HitCountDecVariable <= 49)
            {
                Tier1VarAns = (HitCountDecVariable * Tier1);
                LastEnteredPlayerSalaryTxtBox.Text = Tier1VarAns.ToString("c");
                LastEnteredPlayerSalaryTxtBox.Enabled = true;
                LastEnteredPlayerNameTxtBox.Text = PlayerName.ToString();
                LastEnteredPlayerNameTxtBox.Enabled = true;
                LastEnteredPlayerHitsTxtBox.Text = HitCountDecVariable.ToString();
                LastEnteredPlayerHitsTxtBox.Enabled = true;
                BeginningSalary = Tier1VarAns;
                HitCountDecVariableComparison = HitCountDecVariable;
                TierVariableComparison = Tier1VarAns;
                Tier = "Tier 1";
                /*LeadPlayerHitsTxtBox.Text = HitCountDecVariable.ToString();
                LeadPlayerSalaryTxtBox.Text = Tier1VarAns.ToString("c");
                LeadPlayerNameTxtBox.Text = PlayerName.ToString();*/
                
            }
            else if (HitCountDecVariable <= 99)
            {
                Tier2VarAns = (HitCountDecVariable * Tier2);
                LastEnteredPlayerSalaryTxtBox.Text = Tier2VarAns.ToString("c");
                LastEnteredPlayerSalaryTxtBox.Enabled = true;
                LastEnteredPlayerNameTxtBox.Text = PlayerName.ToString();
                LastEnteredPlayerNameTxtBox.Enabled = true;
                LastEnteredPlayerHitsTxtBox.Text = HitCountDecVariable.ToString();
                LastEnteredPlayerHitsTxtBox.Enabled = true;
                BeginningSalary = Tier2VarAns;
                HitCountDecVariableComparison = HitCountDecVariable;
                TierVariableComparison = Tier2VarAns;
                Tier = "Tier 2";
                /*LeadPlayerHitsTxtBox.Text = HitCountDecVariable.ToString();
                LeadPlayerSalaryTxtBox.Text = Tier2VarAns.ToString("c");
                LeadPlayerNameTxtBox.Text = PlayerName.ToString();*/
            }
            else if (HitCountDecVariable <= 149)
            {
                Tier3VarAns = (HitCountDecVariable * Tier3);
                LastEnteredPlayerSalaryTxtBox.Text = Tier3VarAns.ToString("c");
                LastEnteredPlayerSalaryTxtBox.Enabled = true;
                LastEnteredPlayerNameTxtBox.Text = PlayerName.ToString();
                LastEnteredPlayerNameTxtBox.Enabled = true;
                LastEnteredPlayerHitsTxtBox.Text = HitCountDecVariable.ToString();
                LastEnteredPlayerHitsTxtBox.Enabled = true;
                BeginningSalary = Tier3VarAns;
                HitCountDecVariableComparison = HitCountDecVariable;
                TierVariableComparison = Tier3VarAns;
                Tier = "Tier 3";
                /*LeadPlayerHitsTxtBox.Text = HitCountDecVariable.ToString();
                LeadPlayerSalaryTxtBox.Text = Tier3VarAns.ToString("c");
                LeadPlayerNameTxtBox.Text = PlayerName.ToString();*/
            }
            else if (HitCountDecVariable >= 150)
            {
                Tier4VarAns = (HitCountDecVariable * Tier4);
                LastEnteredPlayerSalaryTxtBox.Text = Tier4VarAns.ToString("c");
                LastEnteredPlayerSalaryTxtBox.Enabled = true;
                LastEnteredPlayerNameTxtBox.Text = PlayerName.ToString();
                LastEnteredPlayerNameTxtBox.Enabled = true;
                LastEnteredPlayerHitsTxtBox.Text = HitCountDecVariable.ToString();
                LastEnteredPlayerHitsTxtBox.Enabled = true;
                BeginningSalary = Tier4VarAns;
                HitCountDecVariableComparison = HitCountDecVariable;
                TierVariableComparison = Tier4VarAns;
                Tier = "Tier 4";
                /*LeadPlayerHitsTxtBox.Text = HitCountDecVariable.ToString();
                LeadPlayerSalaryTxtBox.Text = Tier4VarAns.ToString("c");
                LeadPlayerNameTxtBox.Text = PlayerName.ToString();*/
            }

            if (HitCountDecVariableComparison >= HitCountDecVariable)
            {
                LeadPlayerHitsTxtBox.Text = HitCountDecVariable.ToString();
                LeadPlayerSalaryTxtBox.Text = TierVariableComparison.ToString("c");
                LeadPlayerNameTxtBox.Text = PlayerName.ToString();
                LeadPlayerTierNumTxtBox.Text = Tier;
                LeadPlayerHitsTxtBox.Enabled = true;
                LeadPlayerSalaryTxtBox.Enabled = true;
                LeadPlayerNameTxtBox.Enabled = true;
                LeadPlayerTierNumTxtBox.Enabled = true;
            }
            
        }

        private void AllStarChkBox_CheckedChanged(object sender, EventArgs e)
        {
            decimal BeforeAllStar = 0;
            decimal AllStarApplied = 0;
            decimal AllStarAdjust = 0.1m;
            decimal TotalAllStarPlusOriginalSalary = 0;
            if (AllStarChkBox.Checked == true)
            {
                BeforeAllStar = BeginningSalary;
                AllStarApplied = (BeforeAllStar * AllStarAdjust);
                TotalAllStarPlusOriginalSalary = (AllStarApplied + BeforeAllStar);
                LastEnteredPlayerSalaryTxtBox.Text = TotalAllStarPlusOriginalSalary.ToString("c");
                return;
            }
            
            else if (AllStarChkBox.Checked == true && MVPChkBox.Checked == true)
            {
                decimal BeforeMVPAllStar = 0;
                decimal MVPAllStarAdjust = 0.15m;
                decimal MVPAllStarApplied = 0;
                decimal TotalMVPAllStarSalary = 0;
                BeforeMVPAllStar = BeginningSalary;
                MVPAllStarApplied = (BeforeMVPAllStar * MVPAllStarAdjust);
                TotalMVPAllStarSalary = (BeforeMVPAllStar + MVPAllStarApplied);
                LastEnteredPlayerSalaryTxtBox.Text = TotalMVPAllStarSalary.ToString("c");
            }
            
            if (AllStarChkBox.Checked == false)
            {
                LastEnteredPlayerSalaryTxtBox.Text = BeginningSalary.ToString("c");
                
            }
          
        }
                
        private void MVPChkBox_CheckedChanged(object sender, EventArgs e)
        {
            decimal BeforeMVP = 0;
            decimal MVPApplied = 0;
            decimal MVPAdjust = 0.1m;
            decimal TotalMVPPlusOriginalSalary = 0;

            if (AllStarChkBox.Checked == true && MVPChkBox.Checked == true)
            {
                decimal BeforeMVPAllStar = 0;
                decimal MVPAllStarAdjust = 0.15m;
                decimal MVPAllStarApplied = 0;
                decimal TotalMVPAllStarSalary = 0;
                BeforeMVPAllStar = BeginningSalary;
                MVPAllStarApplied = (BeforeMVPAllStar * MVPAllStarAdjust);
                TotalMVPAllStarSalary = (BeforeMVPAllStar + MVPAllStarApplied);
                LastEnteredPlayerSalaryTxtBox.Text = TotalMVPAllStarSalary.ToString("c");
            }

            else if (MVPChkBox.Checked == true)
            {
                BeforeMVP = BeginningSalary;
                MVPApplied = (BeforeMVP * MVPAdjust);
                TotalMVPPlusOriginalSalary = (MVPApplied + BeforeMVP);
                LastEnteredPlayerSalaryTxtBox.Text = TotalMVPPlusOriginalSalary.ToString("c");
            }
            

            /*else if (MVPChkBox.Checked == false)
            {
                LastEnteredPlayerSalaryTxtBox.Text = BeginningSalary.ToString("c");

            }*/

            /*else if (AllStarChkBox.Checked == true && MVPChkBox.Checked == true)
            {
                decimal BeforeMVPAllStar = 0;
                decimal MVPAllStarAdjust = 0.15m;
                decimal MVPAllStarApplied = 0;
                decimal TotalMVPAllStarSalary = 0;
                BeforeMVPAllStar = BeginningSalary;
                MVPAllStarApplied = (BeforeMVPAllStar * MVPAllStarAdjust);
                TotalMVPAllStarSalary = (BeforeMVPAllStar + MVPAllStarApplied);
                LastEnteredPlayerSalaryTxtBox.Text = TotalMVPAllStarSalary.ToString("c");
            }*/

            else if (AllStarChkBox.Checked == false && MVPChkBox.Checked == false)
            {
                LastEnteredPlayerSalaryTxtBox.Text = BeginningSalary.ToString("c");
            }

            else if (AllStarChkBox.Checked == false && MVPChkBox.Checked == true)
            {
                BeforeMVP = BeginningSalary;
                MVPApplied = (BeforeMVP * MVPAdjust);
                TotalMVPPlusOriginalSalary = (MVPApplied + BeforeMVP);
                LastEnteredPlayerSalaryTxtBox.Text = TotalMVPPlusOriginalSalary.ToString("c");
            }
            
            
           
        }

        private void ClearBtn_Click(object sender, EventArgs e)
        {
            PlayerNameTxtBox.Text = "";
            NumberOfHitsTxtBox.Text = "";
            AllStarChkBox.Checked = false;
            MVPChkBox.Checked = false;
            LastEnteredPlayerNameTxtBox.Text = "";
            LastEnteredPlayerSalaryTxtBox.Text = "";
            LastEnteredPlayerHitsTxtBox.Text = "";
            LeadPlayerNameTxtBox.Text = "";
            LeadPlayerSalaryTxtBox.Text = "";
            LeadPlayerTierNumTxtBox.Text = "";
            LeadPlayerHitsTxtBox.Text = "";
            LeadPlayerHitsTxtBox.Enabled = false;
            LeadPlayerSalaryTxtBox.Enabled = false;
            LeadPlayerNameTxtBox.Enabled = false;
            LeadPlayerTierNumTxtBox.Enabled = false;
            LastEnteredPlayerHitsTxtBox.Enabled = false;
            LastEnteredPlayerNameTxtBox.Enabled = false;
            LastEnteredPlayerSalaryTxtBox.Enabled = false;
        }
    }
}


This post has been edited by Skydiver: 03 March 2015 - 09:11 PM
Reason for edit:: Fixed botched code tag.


Is This A Good Question/Topic? 0
  • +

Replies To: Problems with c# program being created for school.

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: Problems with c# program being created for school.

Posted 04 March 2015 - 06:26 AM

"Doesn't work right" is pretty much meaningless to us. "Doubles the pay instead of adding 15%" tells us something. "Crashes the program" tells us what it does do. But "Its broke" doesn't tell us much.

I know you're new to this. You're in class so you have no choice but to do what the teacher tells you. But I want you to know that you're being taught WinForms which is out-of-date and not what employers are looking for. Go ahead and learn this in class because you have to, but be aware that you could also pick up a "Learn WPF in 30 days" book to learn how people code in THIS DECADE. I other words, your course curriculum is way out of date and it wouldn't hurt to ask why. Schools are businesses like any other: They aren't going to upgrade as long as they're making money. But what you're being taught isn't preparing you for a job developing software without a time machine to go back to 1995.

There are also some bad practices at work here. Again, you're just doing what you're taught but you should try to do better. You're not limited to JUST the school textbook.

A part of where your code is going wrong is due to duplicate code. You have a lot of the same code in both of the checkbox handlers. When you have duplicate code like that you have a bad design. Instead of doing all the work in the checkchanged handler, make a single method to do the calculation. Subscribe both checkboxes to a single hanlder that calls a single recalculate method. now you can call that recalculate method from anyplace much easier.
void checkboxes_checkchanged(object sender, eventargs e)
{
    RecalculatePay();
}


Using your GUI textboxes like they are your variables is also a terrible way to go. Please tell me your teacher didn't tell you to do this.

Here are some very common issues that come up all the time from rookies. They may not all apply, but it looks like a few of them do.


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 from 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 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.



As I said, you're aren't going to get in trouble for learning more and from sources in addition to your text book. Tutorials I recommend:



I'm going to assume your class teacher does what most do and expect you to code - but (s)he hasn't bothered to first show you how to debug your programs in Visual Studio. I don't really know how they get their credentials with this thinking. Please see the debugging tutorials listed below: They will put you head and shoulders above your competition in class.

See FAQ # 2 "How do I debug my program?"

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 6: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated February 2015
Spoiler

Was This Post Helpful? 0
  • +
  • -

#3 renzore101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 15

Re: Problems with c# program being created for school.

Posted 04 March 2015 - 10:57 AM

tlhIn`toq,

Thank you so much for that extremely detailed response. I am going to review everything you have included in depth and and take it all into consideration moving forward. Although I plan on going the networking route versus the software development route, this intro c# course has sparked an interest in coding in general for me. However, I wonder if my professor is just going about it the wrong way.
Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: Problems with c# program being created for school.

Posted 04 March 2015 - 11:13 AM

Not so much the 'wrong' way as the 'old' way - and not starting from the small things and working up to entire programs.
You should have learned about the parts that make up a program, and good practices, and the techniques for debugging, before being told to make something that works.
Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5101
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: Problems with c# program being created for school.

Posted 05 March 2015 - 12:00 PM

I can kinda explain why debugging skills aren't usually taught early on: their focus isn't actually teaching you how to program; it's to teach you programming concepts.

They skip a lot of skills that you will need in the job market in the early classes. For instance, using databases. The point is that it's academic rather than practical.

I agree that they probably should spend more time on the debugger, but since the goal of the class isn't usually "to learn C# programming", but more "to learn about programming using C#". If they covered the debugger too specifically, then all you know is how to debug in Visual Studio instead of the general concepts of debugging.

Academia is just so separated from real-world programming.
Was This Post Helpful? 0
  • +
  • -

#6 click_here  Icon User is offline

  • D.I.C Regular

Reputation: 46
  • View blog
  • Posts: 300
  • Joined: 25-November 13

Re: Problems with c# program being created for school.

Posted 05 March 2015 - 04:53 PM

Without running your code, I'd think that your problem is the order of your if/else if statements in AllStarChkBox_CheckedChanged...

Look at it this way...

// If A && B are true, then A is true...
if (A)
{
  Code will go here if A or A&&B are true
}
else if (A && B)/>
{
  Code will never get here...
}



Solution: Check to see if A&&B are true before just A.
Was This Post Helpful? 0
  • +
  • -

#7 renzore101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 15

Re: Problems with c# program being created for school.

Posted 05 March 2015 - 08:13 PM

Thanks for the feedback click_here, I understand what you are saying and that would make logical sense. I sat down with my professor tonight to look it over and we came to the conclusion that I made it more complicated than it needed to be. Essentially he was looking for the code to check the Checkbox.Checked property within the code on the calculate button, versus how I did it by associating duplicate code with a click event on both of the check boxes. He also mentioned what tlhIn`toq was essentially referring to in regards to dedicated methods instead of the event handlers. However, he is exposing us to this concept in a couple weeks.
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,377
  • Joined: 02-June 10

Re: Problems with c# program being created for school.

Posted 06 March 2015 - 05:38 AM

Quote

However, he is exposing us to this concept in a couple weeks.

It sounds like you could read AND WORK THROUGH a "Learn C# in 30 days" book and put yourself months ahead of everyone else. Generally one of those '30 day' books will cover about a year to a year and a half of what the college course teaches you.
Was This Post Helpful? 0
  • +
  • -

#9 renzore101  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 03-March 15

Re: Problems with c# program being created for school.

Posted 06 March 2015 - 07:53 PM

Quote

It sounds like you could read AND WORK THROUGH a "Learn C# in 30 days" book and put yourself months ahead of everyone else. Generally one of those '30 day' books will cover about a year to a year and a half of what the college course teaches you.


I will look into acquiring such a book. At times I am quite busy with my networking courses on top of this and my internship, which does not necessarily give me loads of time to study additional extracurricular material however. Perhaps over spring break!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1