473,406 Members | 2,954 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

problems with arrays

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
1 2022
As you probably know that did the trick. Thanks for your help.

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
2
by: -Steve- | last post by:
Okay I have a bunch of code below. Hope it comes across readable. The problem I'm having is that in the lines under main(): cout << a << endl; Is going into the code for IntArray(const...
10
by: Fabian | last post by:
Are there any speed issues in javascript with having really large arrays? I know if the array gets large enough, bandwidth and download time can be an issue, but does it take inordinate amounts of...
3
by: nkparimi | last post by:
Hi, I'm facing issues with using associative arrays in non-IE browsers. In particular, I have code like the following: var IDToAddress = new Array(); IDToAddress = 'apple tree lane, mars';...
4
by: Wayne Wengert | last post by:
I am still stuck trying to create a Class to use for exporting and importing array data to/from XML. The format of the XML that I want to import/export is shown below as is the Class and the code I...
7
by: jmac | last post by:
Greetings fellow programmers, I have created a C program that has a few bugs and would like to get some help with working them out. Here is a list of the problems that I am experiencing: -...
38
by: jdcrief | last post by:
Complier: Visual C++ 2005 Express Edition The program I wrote will compile and execute, but the output is always the same, no matter what number is entered in for the radius of the circle. ...
8
by: John Olbert | last post by:
Subject: Problems with Interop in C# We are having problems using Interop with a Vb6 ActiveX Dll in C# code in Net2 using Vs2005. Below are the signatures of the method that is the problem. It...
3
by: Peter Oliphant | last post by:
Below are the definitions of two classes. VList creates two static integer arrays (v_0, v_1), creates an array of pointers to these arrays (vlist), and has a public method to return a pointer to...
13
by: Just_a_fan | last post by:
I am adding a bunch of controls with the code below. Problem 1: When program flow passes to "UpperChanged" when I click it, the control name is undefined. When I enter: If udUpperLim1.Value 1...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.