CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Oct 2013
    Posts
    2

    Save user input in array

    I am learning arrays. I have no idea what I am doing. This code is supposed to save the calculated totals and when the user clicks the exit button a message box pops up displaying the totals.

    I'm trying to follow the examples in the book but it's not working. I have a build error down at the bottom and I underlined it.

    "Error 1 A local variable named 'totals' cannot be declared in this scope because it would give a different meaning to 'totals', which is already used in a 'parent or current' scope to denote something else"

    Can someone help me through this?


    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace InvoiceTotal
    {
    
        public partial class frmInvoiceTotal : Form
    	{
    		public frmInvoiceTotal()
    		{
    			InitializeComponent();
    		}
    
            // TODO: declare class variables for array and list here
    
            decimal[] totals = new decimal[4];
    
    
            private void btnCalculate_Click(object sender, EventArgs e)
    		{
                try
                {
    
                    if (txtSubtotal.Text == "")
                    {
                        MessageBox.Show(
                            "Subtotal is a required field.", "Entry Error");
                    }
                    else
                    {
    			        decimal subtotal = Decimal.Parse(txtSubtotal.Text);
                        if (subtotal > 0 && subtotal < 10000)
                        {
                            decimal discountPercent = 0m;
                            if (subtotal >= 500)
                                discountPercent = .2m;
                            else if (subtotal >= 250 & subtotal < 500)
                                discountPercent = .15m;
                            else if (subtotal >= 100 & subtotal < 250)
                                discountPercent = .1m;
                            decimal discountAmount = subtotal * discountPercent;
    			            decimal invoiceTotal = subtotal - discountAmount;
    
                            discountAmount = Math.Round(discountAmount, 2);
                            invoiceTotal = Math.Round(invoiceTotal, 2);
    
                            txtDiscountPercent.Text = discountPercent.ToString("p1");
                            txtDiscountAmount.Text = discountAmount.ToString();
                            txtTotal.Text = invoiceTotal.ToString();
    
                            txtSubtotal.Focus();
    
                            for (int i = 0; i < totals.Length; i++)
                            {
                                totals[i] = i;
                            }
    
    
                        }
                        else
                        {
                            MessageBox.Show(
                                "Subtotal must be greater than 0 and less than 10,000.", 
                                "Entry Error");
                        }
                    }
                }
                catch (FormatException)
                {
                    MessageBox.Show(
                        "Please enter a valid number for the Subtotal field.", 
                        "Entry Error");
                }
    
                txtSubtotal.Focus();
            }
    
    		private void btnExit_Click(object sender, EventArgs e)
    		{
                // TODO: add code that displays dialog boxes here
    
                String[] totals = {};
                Array.Sort(totals);
                string message = "";
                foreach (string totals in totals)
                    message += totals + "\n";
                MessageBox.Show(message, "Sorted Totals");
    
                this.Close();
    		}
    
    	}
    }

  2. #2
    Join Date
    Oct 2013
    Posts
    2

    Re: Save user input in array

    Well I have fixed some things up an added a list feature but I'm still having problems. My code is not catching the fact that I have exceeded the array count but it should. I know one of the reasons is the way I have it coded for loading information into the array but I don't know another way of doing it.

    Code:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace InvoiceTotal
    {
        public partial class frmInvoiceTotal : Form
    	{
    		public frmInvoiceTotal()
    		{
    			InitializeComponent();
    		}
    
            // TODO: declare class variables for array and list here
    
            decimal[] totals = new decimal[5];
            List<decimal> totalsList = new List<decimal>(5);
    
    
            private void btnCalculate_Click(object sender, EventArgs e)
    		{
                try
                {
    
                    if (txtSubtotal.Text == "")
                    {
                        MessageBox.Show(
                            "Subtotal is a required field.", "Entry Error");
                    }
                    else
                    {
    			        decimal subtotal = Decimal.Parse(txtSubtotal.Text);
                        if (subtotal > 0 && subtotal < 10000)
                        {
                            decimal discountPercent = 0m;
                            if (subtotal >= 500)
                                discountPercent = .2m;
                            else if (subtotal >= 250 & subtotal < 500)
                                discountPercent = .15m;
                            else if (subtotal >= 100 & subtotal < 250)
                                discountPercent = .1m;
                            decimal discountAmount = subtotal * discountPercent;
    			            decimal invoiceTotal = subtotal - discountAmount;
    
                            discountAmount = Math.Round(discountAmount, 2);
                            invoiceTotal = Math.Round(invoiceTotal, 2);
    
                            txtDiscountPercent.Text = discountPercent.ToString("p1");
                            txtDiscountAmount.Text = discountAmount.ToString();
                            txtTotal.Text = invoiceTotal.ToString();
    
                            for (int i = totals.Length - 1; i > 0; i--)
                            {
                                totals[i] = totals[i - 1];
                            }
    
                            for (int d = totalsList.Count - 1; d > 0; d--)
                            {
                                totalsList[1] = totalsList[d - 1];
                            }
    
                            totals[0] = invoiceTotal;
    
                            txtSubtotal.Focus();
    
                        }
                        else
                        {
                            MessageBox.Show(
                                "Subtotal must be greater than 0 and less than 10,000.", 
                                "Entry Error");
                        }
                    }
                }
                catch (FormatException)
                {
                    MessageBox.Show(
                        "Please enter a valid number for the Subtotal field.", 
                        "Entry Error");
                }
    
                txtSubtotal.Focus();
            }
    
    		private void btnExit_Click(object sender, EventArgs e)
    		{
                // TODO: add code that displays dialog boxes here
                Array.Sort(totals);
                String totalsString = "";
                for (int i = 0; i < totals.Length; i++)
                    totalsString += totals[i].ToString("c") + "\n";
                MessageBox.Show(totalsString, "Totals");
    
                foreach (decimal d in totalsList)
                    totalsString += d + "\n";
                MessageBox.Show(totalsString, "Totals List");
    
                this.Close();
    		}
    
    	}
    }

  3. #3
    Join Date
    Oct 2004
    Location
    Rocket City
    Posts
    220

    Re: Save user input in array

    decimal[] totals = new decimal[5];
    List<decimal> totalsList = new List<decimal>(5);
    You might read up again on the List<T> Constructor. Unlike the totals array, the 5 does not actually add any items to totalsList, and I do not see where you have used the totalsList.Add method to do so.

    BTW, when discussing an error you are getting it is sometimes helpful to quote the error message exactly.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Save user input in array

    Quote Originally Posted by arnatuile View Post
    Code:
    namespace InvoiceTotal
    {
        public partial class frmInvoiceTotal : Form
        {
            decimal[] totals = new decimal[4];    // variable at class scope  
    
            private void btnExit_Click(object sender, EventArgs e)
            {
     
                String[] totals = {};    // variable at method scope  
                Array.Sort(totals);
                string message = "";
                foreach (string totals in totals)
                    message += totals + "\n";
                MessageBox.Show(message, "Sorted Totals");
    
                this.Close();
            }
    
        }
    }
    The problem is you've declared a class variable (i.e. class Field) with the name of 'totals' and method variable of the same name. The class totals variables is an array of decimals while the method totals variable is an array of strings. These conflict and that's why you get the error. Tip: use the convention for class scope variables of prepending an '_' to the variable. Thus totals becomes _totals and you can see at a glance that _totals refers to variable with class scope.


    Change your code to refer to the _totals class variable and remove the unneeded local totals variable.

    Code:
    private void btnExit_Click(object sender, EventArgs e)
    {
      Array.Sort(_totals);
      string message = "";
    
      foreach (var total in _total)
      {
        message += total + "\n";
      }
    
      MessageBox.Show(message, "Sorted Totals");
    
      this.Close();
    }
    Last edited by Arjay; October 4th, 2013 at 02:54 PM.

Tags for this Thread

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured