Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy is my C# code coming up with zero's in message box upon clicking exit?
    primarykey
    data
    text
    <p>I am trying to get my message box to show the invoice subtotals stored in my array...5 of them to show in a message box using the foreach method. I am supposed to input a wage and it then does some calculation and stores the subtotal value into the array. I declared an array and index called decArray and intIndex. Can anyone tell me what I'm missing or doing wrong? Thank you in advance!</p> <pre><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[] decArray = new decimal[5]; int intIndex = 0; 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 &gt; 0 &amp;&amp; subtotal &lt; 10000) { decimal discountPercent = 0m; if (subtotal &gt;= 500) discountPercent = .2m; else if (subtotal &gt;= 250 &amp; subtotal &lt; 500) discountPercent = .15m; else if (subtotal &gt;= 100 &amp; subtotal &lt; 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 (intIndex = 0; intIndex &lt;= decArray.Length - 1; intIndex++) { DecArray[intIndex] = InvoiceTotal } } 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 totalstring = ""; foreach (decimal value in decArray) { totalstring += value + "\n"; MessageBox.Show(totalstring + "\n", "Order Totals"); } this.Close(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload