Beginner C# Program Help Please :)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Beginner C# Program Help Please :)

  1. #1
    Join Date
    Feb 2013
    Posts
    7

    Question Beginner C# Program Help Please :)

    Writing a pretty simple cost calculator, and everything is coming out right except the total of everything. Why is the total for everything displaying placeholders instead of numbers, and how can I fix it? I really appreciate any input, thanks!
    Name:  error.png
Views: 286
Size:  29.4 KBName:  framework.png
Views: 216
Size:  14.6 KB

    Code:
    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 StadiumSeating
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void label2_Click(object sender, EventArgs e)
            {
    
            }
    
            private void calculaterevenueButtonrm_Click(object sender, EventArgs e)
            {
                try
                {
    
                    // Declaring for user inputed ticket totals
                    decimal classatickets;
                    decimal classbtickets;
                    decimal classctickets;
                    classatickets = decimal.Parse(classaTextBoxrm.Text);
                    classbtickets = decimal.Parse(classbTextBoxrm.Text);
                    classctickets = decimal.Parse(classcTextBoxrm.Text);
    
    
    
                    // Storing the cost of each ticket class
                    decimal classacost = 15;
                    decimal classbcost = 12;
                    decimal classccost = 9;
    
    
    
    
                    //Declaring names for the results of each class's calculations and how to calculate them
                    decimal classatotal;
                    decimal classbtotal;
                    decimal classctotal;
                    classatotal = classatickets * classacost;
                    classbtotal = classbtickets * classbcost;
                    classctotal = classctickets * classccost;
                    classarevenueTextBoxrm.Text = classatotal.ToString("c");
                    classbrevenueTextBoxrm.Text = classbtotal.ToString("c");
                    classcrevenueTextBoxrm.Text = classctotal.ToString("c");
    
                    // Define how to calculate the total sales
                    decimal totalsales;
                    totalsales = classatotal + classbtotal + classctotal;
    
                    totalrevenueTextBoxrm.Text = totalsales.ToString("c");
                    
                }
                catch (Exception ex)
                {
                    //default error message
                    MessageBox.Show(ex.Message);
    
    
    
    
    
                }
            }
    
            private void clearButtonrm_Click(object sender, EventArgs e)
            {
                classaTextBoxrm.Text = "";
                classbTextBoxrm.Text = "";
                classcTextBoxrm.Text = "";
                classarevenueTextBoxrm.Text = "";
                classbrevenueTextBoxrm.Text = "";
                classcrevenueTextBoxrm.Text = "";
                totalrevenueTextBoxrm.Text = "";
    
            }
        }
    }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: Beginner C# Program Help Please :)

    You'll need to step through and debug your program. If you don't know how to do this, post back.

    Also, can you set the text for the text boxes next to the labels beginning with Class (i.e. Class A, Class B, etc.)?

  3. #3
    Join Date
    Feb 2013
    Posts
    7

    Re: Beginner C# Program Help Please :)

    Thanks for the reply Arjay. I thought I had taken a shot of the output after debugging. I am just selecting "debug" and then "start debugging." This is what happens when I debug(no errors). I input 1 for each field.
    Name:  aQhUxXH.png
Views: 181
Size:  27.3 KB

  4. #4
    DataMiser is online now Super Moderator Power Poster
    Join Date
    Jul 2008
    Location
    WV
    Posts
    4,825

    Re: Beginner C# Program Help Please :)

    Kinda looks like you have the password character set for that field
    Always use [code][/code] tags when posting code.

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,256

    Re: Beginner C# Program Help Please :)

    [I think I know what is the problem, but since I started to explain how to debug, I'm going to continue]

    What debugging allows you to do is to watch the program variables while your program executes. It also allows you to step through each line of code and makes sure variables are getting set as you expect and other logic is working as intended.

    So starting by pressing F5 (debug program) is a good start, but to be really useful, you'll need to set one or more breakpoint so the program will stop so you can inspect the variables.

    To do this, set a breakpoint at the following line inside the calculaterevenue...click handler:
    Code:
    classbtickets = decimal.Parse(classbTextBoxrm.Text);
    Just put the mouse over the line above, click and then press F9. You'll notice a little circle appears to the left of the line of code.
    Next, press F5 to start debugging. Enter the values and click the calculate button.

    You'll notice the program will stop on the break point. To inspect a variable, hold the mouse over the classatickets variable. You should see that the value is the same as what you entered. Next, hold the mouse over the classbtickets variable. You should see either null or "" as the value. This is because this line of code hasn't executed yet.

    Press F10 to execute the next line of code to inspect more variables or press F5 to continue running the program (and hit additional breakpoints if any).

    Okay, so that's a quick debugging primer, now onto your problem.

    I suspect the problem is the style of the Totals text box has been set to Password = true. To check this, open the forms editor, right click on the totals text box and choose properties. Then make sure that the Password entry is set to false.

  6. #6
    Join Date
    Feb 2013
    Posts
    7

    Re: Beginner C# Program Help Please :)

    Thanks guys, that was it. That was pretty silly of me, but now I know a lot more about actual debugging. Cheers

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center