Using arrays to hold values for calculations in C#

Often we learn a programming language to accomplish a task that was assigned to us at work or in class.   Although we find the experience gratifying, we don't have an opportunity to continue to use the new skill we learned until another occasion arises when we need to use it.  

 

Being in a similar boat where I really enjoy programming in C# but due to a change in careers I haven't utilized it as much as I would like, I endeavor to create a section in my website that provides mini C# lessons by example.  These lessons are for anyone from new C# programmers to people who find themselves in the situation described above, so without any more hesitation, here is the technical part of the post.

 

Program Summary:  The program below prompts allows the user to enter 5 lengths at one time, followed by a series of 5 widths. Once the 5 lengths and widths are entered, the user can click on the "calculate" button so that the area values are displayed in the text box.  Since the display area is a text box and not a menu or list menu, we cannot make use of the Add.Item feature of the control. 

 

The program uses three arrays.  All three arrays are of type integer.  Since the user can enter decimal or character values, a try catch block is incorporated to catch these instances, display a message and wait for another value to be entered. 

 

 

The C# methods and concepts that this program demonstrates are:

 

  • Text Box and Button Enabling based on conditions (btn.Enabled = true)
  • New method creation 
  • Try Catch Blocks
  • For loops
  • Use of Arrays
  • Assigning values to a string and concatenating to it via a for loop

 

Attached is the project.  It was created in Visual Studio Express 2008

AttachmentSize
Array Example.zip62.3 KB