0

I'm creating a form that would store monthly sales amounts. The user would hit a submit button and those amounts would be stored in an Array.

There is then a Total Button that would be clicked, and call a method that would total the amounts from the Array.

So far I have two issues (that I know of): First, I'm still unsure of the proper format to call my method. Right now VS doesn't like my variable "x". I wasn't sure what to use, so I just stuck one in there. Second, the code for summing my Array is not right either. VS doesn't like my "index" variable.

I've tried to note the problem areas in the code. Sorry for the lengthy post and the ignorant questions.

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

        private void submitButton_Click(object sender, EventArgs e)
        {
            //creating an array
            const int SIZE = 12;
            decimal[] amountsArray = new decimal[SIZE];
            amountsArray[0] = decimal.Parse(JaninputBox.Text);
            amountsArray[1] = decimal.Parse(FebinputBox.Text);
            amountsArray[2] = decimal.Parse(MarinputBox.Text);
            amountsArray[3] = decimal.Parse(AprinputBox.Text);
            amountsArray[4] = decimal.Parse(MayinputBox.Text);
            amountsArray[5] = decimal.Parse(JuninputBox.Text);
            amountsArray[6] = decimal.Parse(JulinputBox.Text);
            amountsArray[7] = decimal.Parse(AuginputBox.Text);
            amountsArray[8] = decimal.Parse(SepinputBox.Text);
            amountsArray[9] = decimal.Parse(OctinputBox.Text);
            amountsArray[10] = decimal.Parse(NovinputBox.Text);
            amountsArray[11] = decimal.Parse(DecinputBox.Text);

            TotsalesButton.Enabled = true;

        }

        private void TotsalesButton_Click(object sender, EventArgs e)
        {
            // calling the method to total amounts from array
           TotalIt(decimal x); **//<-- VS doesn't like this**

            AvgsalesButton.Enabled = true; //enabling avg sales button

        }

        private void TotalIt(decimal[] amountsArray)
        {
            decimal sum;  // variable to hold total 

            // method for totaling array data
            for (decimal index = 0; index < amountsArray.Length; index++)
            {
                sum += amountsArray[index]; **//<-- Doesn't like "index"** here.
            }

        }

    }

}
pravprab
  • 2,301
  • 3
  • 26
  • 43
Aron
  • 3
  • 2
  • Make index an integer. Collection indices are integers but this does not mean the values are integers as well. – Jeroen Vannevel Mar 23 '15 at 03:36
  • What is "x"?? also, why don't you make the amountsArray to a global variable so other methods can access?? Also, a suggestion, change the TotalIt method to something like this: http://stackoverflow.com/questions/18824761/c-sharp-how-to-get-sum-of-the-values-from-list – User2012384 Mar 23 '15 at 03:46

2 Answers2

0

Few things. Unless you're handling the input elsewhere you should be using TryParse so to not cause an exception if someone doesn't enter a value that can be parsed to a decimal (ie 1.a03). so

decimal parsedDecimal;
amountsArray[0] = decimal.TryParse(JaninputBox.Text, out parsedDecimal) ? parsedDecimal : 0.00;

for that section.

The TotsalesButton_Click is calling a method (TotalIt) that has a parameter of a decimal array, however you're using a single decimal 'x' which isn't even a variable anywhere.

The easiest way to do what you're trying is to declare the amountsArray a public variable at the top.

public partial class Form1 : Form
{
    private decimal[] _amountsArray;
    public Form1()
    {
        InitializeComponent();
    }
 }

and then in your TotsalesButton you don't need to pass a parameter, just check that the array isn't null (which it won't be because the submitButton has to be called and the array is initialized in there)

private void TotsalesButton_Click(object sender, EventArgs e)
{
     var sum = TotalIt();
     if (sum > 0.0) AvgsalesButton.Enabled = true; 
}

finally the the total can be called like this

private decimal TotalIt()
{
    if (_amountsArray == null) return;
    decimal sum;  // variable to hold total 

    //since you're iterating through an array, an index is always an int
    for (int index = 0; index < amountsArray.Length; index++)
    {
         sum += amountsArray[index];
    }
    return sum;
}
Ministry
  • 116
  • 7
0

The fundamental problem you have right now has to do with scope. Some of your variables have a scope that is limited to a single method, but need to be available as members for the entire class:

public partial class Form1 : Form
{
   //creating an array
    const int SIZE = 12;
    decimal[] amountsArray = new decimal[SIZE];

    public Form1()
    {
        InitializeComponent();
    }

   //...

}

Then you will be able to call the TotalIt() method. Additionally, there would be no need to pass the array to the method at all, since it is already available as a member of the class:

    private void TotsalesButton_Click(object sender, EventArgs e)
    {
       TotalIt();
       AvgsalesButton.Enabled = true; //enabling avg sales button
    }

Here is what the TotalIt() method would look like. Again, since the method can already use the array, you don't need to declare as a parameter:

    private void TotalIt()
    {
        amountsArray.Sum(); 
    }

I'm not sure what you plan to do with that result... it wasn't in your original code. However, you can see that the Sum() method makes the whole thing easier. However, your original code was almost right. All you had to do was declare the index variable as an int instead of a decimal. You'll find out very quickly that using exactly the right data type for things matters a great deal when you are working with code.

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794