1

I apologize if this is a really simple solution...

So I'm trying to get an Ending Balance from 4 inputs, those being

  • Starting Balance
  • Number of months that have elapsed
  • User Specified yearly interest rate
  • and a optional monthly contribution that the user can put in. This is what I would imagine the equation to be

balance = contribution + balance + (INTEREST_RATE * balance) + (yearly * balance);

Everything is fine until the compiler states that Use of unassigned local variable 'contribution' This really confuses me because at the comment at the top of the code I have stated that contribution will be an int.

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

    private void calculateButton_Click(object sender, EventArgs e)
    {
        // Constant for the monthly interest rate.
        const decimal INTEREST_RATE = 0.005m;

        // Local variables
        decimal balance; // The account balance
        int months; // The number of months
        int contribution;
        int count = 1; // Loop counter, initialized with 1
        decimal yearly;

        // Get the starting balance.
        if (decimal.TryParse(txtStartBalance.Text, out balance))
        {
            // Get the number of months.
            if (int.TryParse(txtMonths.Text, out months))
            {
                // Get the yearly interest rate
                if (decimal.TryParse(txtYearly.Text, out yearly))
                {
                    //Get monthly contribution
                    if (int.TryParse (txtMonthly.Text, out contribution));
                }
                // The following loop calculates the ending balance.
                while (count <= months)
                {
                    // Add this month's interest to the balance.
                    balance = contribution + balance + (INTEREST_RATE * balance) + (yearly * balance);

                    // Display this month's ending balance.
                    if (rdoEnglish.Checked == false)
                        lstDetails.Items.Add("ʻO ka pale hope " + "no ka mahina " + count + " ʻO " + balance.ToString("c"));
                    else
                        lstDetails.Items.Add("The ending balance for " + "month " + count + " is " + balance.ToString("c"));

                    // Add one to the loop counter.
                    count = count + 1;
                }

                // Display the ending balance.
                txtEnding.Text = balance.ToString("c");

Again thank you for taking the time to help me.

2
  • 1
    Consider the paths taken through the code that leads to the line that gives the error, and see if there is one that does not assign a value to contribution. What if the value in either txtYearly.Text or txtMonthly.Text does not successfully parse as a decimal or int (respectively)? If that happens, and count <= months is still true, then you skip the assignment of contribution, and then try to use it. Commented Oct 12, 2018 at 18:34
  • @St.Pat contribution will be set to 0 if txtMonthly.Text fails to parse, since out parameters must be assigned. The problem is just with txtYearly.Text. Commented Oct 12, 2018 at 19:13

2 Answers 2

2

This really confuses me because at the comment at the top of the code I have stated that contribution will be an int.

Yes but you have not assigned a value to the variable in at least one path your code can take. (Specifically if int.TryParse (txtMonthly.Text, out contribution)); fails) And then you try to use contribution in the line:

balance = contribution + balance + (INTEREST_RATE * balance) + (yearly * balance);

You need to assign a value to contribution before you use it. Think about what contribution is supposed to represent and assign it to a value accordingly

Sign up to request clarification or add additional context in comments.

5 Comments

I think I get it? In the lines above where I state all my variables I tried to assign int contribution = int.TryParse (txtMonthly.Text); but I get an error about tryParse taking 1 argument.
@BrandonLau Yes that is because tryParse takes two arguments. If you are only trying to pass only one argument you could use int contribution = int.Parse(txtMonthly.Text);
@BrandonLau I would rework your code to handle if any of your parses fail
Awww jeez, I feel so dumb. I had the solution up a long time ago. It gave me a incorrect string format error. I should've put that into my question but I disregarded it. I think that happened because when I tried to only calculate the starting balance and months only (without putting in a number for contribution in the box) it just closed the window and brought me to the exception. I had to make an "else" error message to prevent it from crashing. As you can tell I'm EXTREMELY new to this...
so yes int contribution = int.Parse(txtMonthly.Text); IS the solution as GBlodgett said.
0

Try to declare your variables Like below:

decimal.TryParse(txtStartBalance.Text, out decimal balance);  

it is better to make a checkbox for the contribution field (txtMonthly) and set its visibility to Checkbox.checked property so if the user wants to set the optional contribution value, has to check the checkbox to write it down.
Also using that ifs in your code is useless until you throw Exceptions when those TryParse methods retrun false and warn the user to try again with right inputs.

2 Comments

Duly noted! Your checkbox idea is actually what helped me achieve Eureka!!! Thank you! Now I just have to find out how to code that error message in...
@BrandonLau simply you can control the flow of your code with else statement so if the TryParse returned false use messagebox.show(…) to warn the user and your app does not evaluate the balance and clears the textboxes.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.