By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,452 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

problems with arrays

P: n/a
Hello,
Beginner here reading through murach's c# book and come to a point
where the book doesn't give a good example of what they want you to do
in the exercise.

I've created an array with 5 elements that all start with their default
value 0. I need to replace those 0's with values i enter into the
application. When trying to solve this problem i've run into two
outcomes. First is when I enter a value and click calcualte it stores
that value in all 5 elements, and when I enter another value they're
all replaced again. Second when I added a break; statement and type in
a value in the application it only stores that value in the first
element of the array and if I type another value it just replaces the
first value and doesn't store that value in the second part of the
array.

I know with collections you can just use arraylist.add but i would like
how to do this with an array. Any suggestions would be apprecaited.
Part i'm working on is in the click event down towards the bottom.

see code below: Any suggestions would be greatly appreciated.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication1
{
/// <summary>
/// Main form
/// </summary>
/// <remarks>
/// Murach's C# by Joel Murach & Doug Lowe.
/// Exercise 8-1 Start.
/// (c) 2004 by Mike Murach & Associates, Inc.
/// www.murach.com
/// </remarks>

public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Button btnCalculate;
private System.Windows.Forms.Button btnExit;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.TextBox txtSubtotal;
private System.Windows.Forms.Label lblDiscountPercent;
private System.Windows.Forms.Label lblDiscountAmount;
private System.Windows.Forms.Label lblTotal;

private System.ComponentModel.Container components = null;

public Form1()
{
InitializeComponent();
}

protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code

private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.txtSubtotal = new System.Windows.Forms.TextBox();
this.btnCalculate = new System.Windows.Forms.Button();
this.btnExit = new System.Windows.Forms.Button();
this.label4 = new System.Windows.Forms.Label();
this.lblDiscountPercent = new System.Windows.Forms.Label();
this.lblDiscountAmount = new System.Windows.Forms.Label();
this.lblTotal = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(13, 14);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(94, 20);
this.label1.TabIndex = 0;
this.label1.Text = "Subtotal:";
this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label2
//
this.label2.Location = new System.Drawing.Point(13, 69);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(94, 20);
this.label2.TabIndex = 0;
this.label2.Text = "Discount amount:";
this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label3
//
this.label3.Location = new System.Drawing.Point(13, 97);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(94, 20);
this.label3.TabIndex = 0;
this.label3.Text = "Total:";
this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// txtSubtotal
//
this.txtSubtotal.Location = new System.Drawing.Point(113, 14);
this.txtSubtotal.Name = "txtSubtotal";
this.txtSubtotal.Size = new System.Drawing.Size(84, 20);
this.txtSubtotal.TabIndex = 0;
this.txtSubtotal.Text = "";
//
// btnCalculate
//
this.btnCalculate.Location = new System.Drawing.Point(27, 132);
this.btnCalculate.Name = "btnCalculate";
this.btnCalculate.Size = new System.Drawing.Size(73, 27);
this.btnCalculate.TabIndex = 1;
this.btnCalculate.Text = "&Calculate";
this.btnCalculate.Click += new
System.EventHandler(this.btnCalculate_Click);
//
// btnExit
//
this.btnExit.DialogResult = System.Windows.Forms.DialogResult.Cancel;
this.btnExit.Location = new System.Drawing.Point(113, 132);
this.btnExit.Name = "btnExit";
this.btnExit.Size = new System.Drawing.Size(80, 27);
this.btnExit.TabIndex = 2;
this.btnExit.Text = "E&xit";
this.btnExit.Click += new System.EventHandler(this.btnExit_Click);
//
// label4
//
this.label4.Location = new System.Drawing.Point(13, 42);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(94, 20);
this.label4.TabIndex = 5;
this.label4.Text = "Discount percent:";
this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// lblDiscountPercent
//
this.lblDiscountPercent.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblDiscountPercent.Location = new System.Drawing.Point(113, 42);
this.lblDiscountPercent.Name = "lblDiscountPercent";
this.lblDiscountPercent.Size = new System.Drawing.Size(84, 20);
this.lblDiscountPercent.TabIndex = 6;
this.lblDiscountPercent.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// lblDiscountAmount
//
this.lblDiscountAmount.BorderStyle =
System.Windows.Forms.BorderStyle.Fixed3D;
this.lblDiscountAmount.Location = new System.Drawing.Point(113, 69);
this.lblDiscountAmount.Name = "lblDiscountAmount";
this.lblDiscountAmount.Size = new System.Drawing.Size(84, 20);
this.lblDiscountAmount.TabIndex = 7;
this.lblDiscountAmount.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// lblTotal
//
this.lblTotal.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.lblTotal.Location = new System.Drawing.Point(113, 97);
this.lblTotal.Name = "lblTotal";
this.lblTotal.Size = new System.Drawing.Size(84, 20);
this.lblTotal.TabIndex = 8;
this.lblTotal.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// Form1
//
this.AcceptButton = this.btnCalculate;
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.CancelButton = this.btnExit;
this.ClientSize = new System.Drawing.Size(213, 173);
this.Controls.Add(this.lblTotal);
this.Controls.Add(this.lblDiscountAmount);
this.Controls.Add(this.lblDiscountPercent);
this.Controls.Add(this.label4);
this.Controls.Add(this.btnExit);
this.Controls.Add(this.btnCalculate);
this.Controls.Add(this.txtSubtotal);
this.Controls.Add(this.label3);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Invoice Total";
this.ResumeLayout(false);

}
#endregion

[STAThread]
static void Main()
{
Application.Run(new Form1());
}

//ArrayList invoiceTotalArray = new ArrayList(5);
decimal[] invoiceTotalArray = new decimal[5];

private void btnCalculate_Click(object sender, System.EventArgs e)
{
try
{
decimal subtotal = Convert.ToDecimal(txtSubtotal.Text);
decimal discountPercent = .25m;
decimal discountAmount = subtotal * discountPercent;
decimal invoiceTotal = subtotal - discountAmount;

lblDiscountPercent.Text = discountPercent.ToString("p1");
lblDiscountAmount.Text = discountAmount.ToString("c");
lblTotal.Text = invoiceTotal.ToString("c");

for (int i = 0; i < invoiceTotalArray.Length; i++)
{
invoiceTotalArray[i] = invoiceTotal;
//break;
}

txtSubtotal.Focus();
}
catch (FormatException)
{
MessageBox.Show(
"Please enter a valid number in the Subtotal field.",
"Entry Error");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message + "\n\n" +
ex.GetType().ToString() + "\n" +
ex.StackTrace, "Exception");
}
}

private void btnExit_Click(object sender, System.EventArgs e)
{
String totalsString = "";
for (int i = 0; i < invoiceTotalArray.Length; i++)
totalsString += invoiceTotalArray[i].ToString("c") + "\n";
MessageBox.Show(totalsString, "Order Totals");

this.Close();
}
}
}

Nov 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
As you probably know that did the trick. Thanks for your help.

Nov 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.