Home

Arrays of Objects

 

Introduction

Like a primitive array, a class can be declared and used as an array. There is nothing significant to do when declaring the field. Make sure you give it an appropriate name and add its square brackets. The approach is the same used for a primitive data type.

Practical LearningPractical Learning: Introducing Array Objects in the Main Class

  1. To start a new project, on the main menu, click File -> New Project
  2. In the Template section, click Class Library
  3. Set the Name to ItemManager and click OK
  4. In the Solution Explorer, right-click Class1.cs and click Rename
  5. Change the name to StoreItem.cs and press Enter.
    If a dialog box displays, read the text and click Yes
  6. Change the file as follows:
     
    using System;
    
    namespace Business
    {
        interface IStoreInventory
        {
            long ItemNumber { get; set; }
            string ItemName { get; set; }
            decimal UnitPrice { get; set; }
        }
    }
    
    namespace ItemManager
    {
        public sealed class StoreItem : Business.IStoreInventory
        {
            private long itemNo;
            private string name;
            private decimal price;
    
            public long ItemNumber
            {
                get { return itemNo; }
                set { itemNo = value; }
            }
    
            public string ItemName
            {
                get { return name; }
                set { name = value; }
            }
    
            public decimal UnitPrice
            {
                get { return (price < 0) ? 0.00M : price; }
                set { price = value; }
            }
    
            public StoreItem()
            {
                this.itemNo = 0;
                this.name = "Unknown";
                this.price = 0.00M;
            }
    
            public StoreItem(long Nbr, string nm, decimal pr)
            {
                this.itemNo = Nbr;
                this.name = nm;
                this.price = pr;
            }
        }
    }
  7. To save the project, on the Standard toolbar, click the Save All button Save All
  8. Accept the Name as ItemManager and click Save
  9. To create the library, on the main menu, click Build -> Build Solution
  10. To create a new application, in the Solution Explorer, right-click Solution 'ItemManager' -> Add -> New Project...
  11. In the Templates section, click Console Application
  12. Set the Name to DepartmentStore6 and press Enter
  13. To save the project, on the Standard toolbar, click the Save All button Save All
  14. To specify the default project, in the Solution Explorer, right-click DepartmentStore6 and click Set As StartUp Project
  15. To import the previously created assembly, in the Solution Explorer, right-click the References node under DepartmentStore6 and click Add Reference...
  16. Click Browse
  17. Locate the ItemManager folder and display it in the Look In combo box
  18. Locate the ItemManager.dll file (it should be in the Release sub-folder inside the bin folder of ItemManager) and click it
  19. Click OK
  20. Change the Program.cs file as follows:
     
    using System;
    
    namespace DepartmentStore6
    {
        class Program
        {
            static int Main(string[] args)
            {
                ItemManager.StoreItem dptStore = new ItemManager.StoreItem();
    
                Console.WriteLine("Department Store");
                Console.WriteLine("Item #:      {0}", dptStore.ItemNumber);
                Console.WriteLine("Description: {0}", dptStore.ItemName);
                Console.WriteLine("Unit Price:  {0:C}\n", dptStore.UnitPrice);
    
                return 0;
            }
        }
    }
  21. Execute the application to see the result. This would produce:
     
    Department Store
    Item #:      0
    Description: Unknown
    Unit Price:  $0.00
    
    Press any key to continue . . .
  22. Close the DOS window

 

 

A Class as an Array Member Variable

To use a class as an array member variable of the main class, declare the array as we have used them so far but make it static. For example, imagine you have created a class as follows:

using System;

namespace CSharpLessons
{
    public class Square
    {
	private double _side;

	public Square() { _side = 0; }
	public double Side
	{
	    get { return _side; }
	    set { _side = value; }
	}
	public Double Perimeter
	{
	    get { return _side * 4; }
	}
	public double Area
	{
	    get { return _side * _side; }
	}
    }
}

Here is how you can declare an array variable from the above class:

using System;

namespace CSharpLessons
{
	class Exercise
	{
		static Square[] Sq = new Square[2];

		static void Main(string[] args)
		{
		}
	}
}

After declaring the array, you can initialize it using the Main() function. You should first allocate memory using the new operator for each member of the array. Each member of the array is accessed using its index. Once the array exists, you can access each member of the class indirectly from the index of the variable. Here is an example:

using System;

namespace CSharpLessons
{
	class Exercise
	{
		static Square[] Sq = new Square[2];

		static void Main(string[] args)
		{
			Sq[0] = new Square();
			Sq[0].Side = 18.84;

			Sq[1] = new Square();
			Sq[1].Side = 25.62;

			Console.WriteLine("Squares Characteristics");
			Console.WriteLine("\nFirst Square");
			Console.WriteLine("Side:      {0}", Sq[0].Side);
			Console.WriteLine("Perimeter: {0}", Sq[0].Perimeter);
			Console.WriteLine("Area:      {0}", Sq[0].Area);
			
			Console.WriteLine("\nSecond Square");
			Console.WriteLine("Side:      {0}", Sq[1].Side);
			Console.WriteLine("Perimeter: {0}", Sq[1].Perimeter);
			Console.WriteLine("Area:      {0}", Sq[1].Area);
		}
	}
}

This would produce:

Squares Characteristics

First Square
Side:      18.84
Perimeter: 75.36
Area:      354.9456

Second Square
Side:      25.62
Perimeter: 102.48
Area:      656.3844
 

Practical LearningPractical Learning: Declaring an Array Variable of a Class

  1. To declare and use an array variable of StoreItem values, change the contents of the Main() method of the Exercise.cs as follows:
     
    using System;
    
    namespace DepartmentStore6
    {
        class Program
        {
            static int Main(string[] args)
            {
                ItemManager.StoreItem[] anItem = new ItemManager.StoreItem[20];
    
                // We will use a string to categorize each item
                string Category;
    
                // Create the list of sales items
                anItem[0] = new ItemManager.StoreItem();
                anItem[0].ItemNumber = 947783;
                anItem[0].ItemName = "Double-faced wool coat    ";
                anItem[0].UnitPrice = 275.25M;
    
                anItem[1] = new ItemManager.StoreItem();
                anItem[1].ItemNumber = 934687;
                anItem[1].ItemName = "Floral Silk Tank Blouse   ";
                anItem[1].UnitPrice = 180.00M;
    
                anItem[2] = new ItemManager.StoreItem();
                anItem[2].ItemNumber = 973947;
                anItem[2].ItemName = "Push Up Bra               ";
                anItem[2].UnitPrice = 50.00M;
    
                anItem[3] = new ItemManager.StoreItem();
                anItem[3].ItemNumber = 987598;
                anItem[3].ItemName = "Chiffon Blouse            ";
                anItem[3].UnitPrice = 265.00M;
    
                anItem[4] = new ItemManager.StoreItem();
                anItem[4].ItemNumber = 974937;
                anItem[4].ItemName = "Bow Belt Skirtsuit        ";
                anItem[4].UnitPrice = 245.55M;
    
                anItem[5] = new ItemManager.StoreItem();
                anItem[5].ItemNumber = 743765;
                anItem[5].ItemName = "Cable-knit Sweater        ";
                anItem[5].UnitPrice = 45.55M;
    
                anItem[6] = new ItemManager.StoreItem();
                anItem[6].ItemNumber = 747635;
                anItem[6].ItemName = "Jeans with Heart Belt     ";
                anItem[6].UnitPrice = 25.65M;
    
                anItem[7] = new ItemManager.StoreItem();
                anItem[7].ItemNumber = 765473;
                anItem[7].ItemName = "Fashionable mini skirt    ";
                anItem[7].UnitPrice = 34.55M;
    
                anItem[8] = new ItemManager.StoreItem();
                anItem[8].ItemNumber = 754026;
                anItem[8].ItemName = "Double Dry Pants          ";
                anItem[8].UnitPrice = 28.55M;
    
                anItem[9] = new ItemManager.StoreItem();
                anItem[9].ItemNumber = 730302;
                anItem[9].ItemName = "Romantic Flower Dress     ";
                anItem[9].UnitPrice = 24.95M;
    
                anItem[10] = new ItemManager.StoreItem();
                anItem[10].ItemNumber = 209579;
                anItem[10].ItemName = "Cotton Polo Shirt         ";
                anItem[10].UnitPrice = 45.75M;
    
                anItem[11] = new ItemManager.StoreItem();
                anItem[11].ItemNumber = 267583;
                anItem[11].ItemName = "Pure Wool Cap             ";
                anItem[11].UnitPrice = 25.00M;
    
                anItem[12] = new ItemManager.StoreItem();
                anItem[12].ItemNumber = 248937;
                anItem[12].ItemName = "Striped Cotton Shirt      ";
                anItem[12].UnitPrice = 65.55M;
    
                anItem[13] = new ItemManager.StoreItem();
                anItem[13].ItemNumber = 276057;
                anItem[13].ItemName = "Two-Toned Ribbed Crewneck ";
                anItem[13].UnitPrice = 9.75M;
    
                anItem[14] = new ItemManager.StoreItem();
                anItem[14].ItemNumber = 267945;
                anItem[14].ItemName = "Chestnut Italian Shoes    ";
                anItem[14].UnitPrice = 165.75M;
    
                anItem[15] = new ItemManager.StoreItem();
                anItem[15].ItemNumber = 409579;
                anItem[15].ItemName = "Collar and Placket Jacket ";
                anItem[15].UnitPrice = 265.15M;
    
                anItem[16] = new ItemManager.StoreItem();
                anItem[16].ItemNumber = 467583;
                anItem[16].ItemName = "Country Coat Rugged Wear  ";
                anItem[16].UnitPrice = 35.55M;
    
                anItem[17] = new ItemManager.StoreItem();
                anItem[17].ItemNumber = 448937;
                anItem[17].ItemName = "Carpenter Jeans           ";
                anItem[17].UnitPrice = 24.95M;
    
                anItem[18] = new ItemManager.StoreItem();
                anItem[18].ItemNumber = 476057;
                anItem[18].ItemName = "Dbl-Cushion Tennis Shoes  ";
                anItem[18].UnitPrice = 48.75M;
    
                anItem[19] = new ItemManager.StoreItem();
                anItem[19].ItemNumber = 467945;
                anItem[19].ItemName = "Stitched Center-Bar Belt  ";
                anItem[19].UnitPrice = 32.50M;
    
                // Display the inventory
                Console.WriteLine("Store Inventory");
    Console.WriteLine("=======+==========+===========================+=========");
    Console.WriteLine("Item # | Category | Description               | Price");
                for (int i = 0; i < 20; i++)
                {
                 // Any item whose number starts with 9 is categorized as "Women"
                    if (anItem[i].ItemNumber >= 900000)
                        Category = " Women";
                    // Any item whose number starts with 7 is for teenage girls
                    else if (anItem[i].ItemNumber >= 700000)
                        Category = " Girls";
                    // Items whose number start with 4 are for teenage boyes
                    else if (anItem[i].ItemNumber >= 400000)
                        Category = " Boys ";
                    else
                        Category = " Men  ";
    
    Console.WriteLine("-------+----------+---------------------------+---------");
                    Console.WriteLine("{0} | {1}   | {2}| {3}",
                                      anItem[i].ItemNumber, Category,
                        		  anItem[i].ItemName, anItem[i].UnitPrice);
                }
    
    Console.WriteLine("=======+==========+===========================+=========\n");
                return 0;
            }
        }
    }
  2. Execute the application. This would produce:
     
    Store Inventory
    =======+==========+===========================+=========
    Item # | Category | Description               | Price
    -------+----------+---------------------------+---------
    947783 |  Women   | Double-faced wool coat    | 275.25
    -------+----------+---------------------------+---------
    934687 |  Women   | Floral Silk Tank Blouse   | 180.00
    -------+----------+---------------------------+---------
    973947 |  Women   | Push Up Bra               | 50.00
    -------+----------+---------------------------+---------
    987598 |  Women   | Chiffon Blouse            | 265.00
    -------+----------+---------------------------+---------
    974937 |  Women   | Bow Belt Skirtsuit        | 245.55
    -------+----------+---------------------------+---------
    743765 |  Girls   | Cable-knit Sweater        | 45.55
    -------+----------+---------------------------+---------
    747635 |  Girls   | Jeans with Heart Belt     | 25.65
    -------+----------+---------------------------+---------
    765473 |  Girls   | Fashionable mini skirt    | 34.55
    -------+----------+---------------------------+---------
    754026 |  Girls   | Double Dry Pants          | 28.55
    -------+----------+---------------------------+---------
    730302 |  Girls   | Romantic Flower Dress     | 24.95
    -------+----------+---------------------------+---------
    209579 |  Men     | Cotton Polo Shirt         | 45.75
    -------+----------+---------------------------+---------
    267583 |  Men     | Pure Wool Cap             | 25.00
    -------+----------+---------------------------+---------
    248937 |  Men     | Striped Cotton Shirt      | 65.55
    -------+----------+---------------------------+---------
    276057 |  Men     | Two-Toned Ribbed Crewneck | 9.75
    -------+----------+---------------------------+---------
    267945 |  Men     | Chestnut Italian Shoes    | 165.75
    -------+----------+---------------------------+---------
    409579 |  Boys    | Collar and Placket Jacket | 265.15
    -------+----------+---------------------------+---------
    467583 |  Boys    | Country Coat Rugged Wear  | 35.55
    -------+----------+---------------------------+---------
    448937 |  Boys    | Carpenter Jeans           | 24.95
    -------+----------+---------------------------+---------
    476057 |  Boys    | Dbl-Cushion Tennis Shoes  | 48.75
    -------+----------+---------------------------+---------
    467945 |  Boys    | Stitched Center-Bar Belt  | 32.50
    =======+==========+===========================+=========
    
    Press any key to continue . . .
  3. Close the DOS window
  4. To be able to process an order, change the file as follows:
     
    using System;
    
    namespace DepartmentStore6
    {
        class Program
        {
            static int Main(string[] args)
            {
                ItemManager.StoreItem[] anItem = new ItemManager.StoreItem[20];
    
                // We will use a string to categorize each item
                string Category = "Unknown";
                long ItemID = 0;
                string Description = "Unknown";
                decimal Price = 0.00M;
    
                // Create the list of sales items
                anItem[0] = new ItemManager.StoreItem();
                anItem[0].ItemNumber = 947783;
                anItem[0].ItemName = "Double-faced wool coat    ";
                anItem[0].UnitPrice = 275.25M;
    
                anItem[1] = new ItemManager.StoreItem();
                anItem[1].ItemNumber = 934687;
                anItem[1].ItemName = "Floral Silk Tank Blouse   ";
                anItem[1].UnitPrice = 180.00M;
    
                anItem[2] = new ItemManager.StoreItem();
                anItem[2].ItemNumber = 973947;
                anItem[2].ItemName = "Push Up Bra               ";
                anItem[2].UnitPrice = 50.00M;
    
                anItem[3] = new ItemManager.StoreItem();
                anItem[3].ItemNumber = 987598;
                anItem[3].ItemName = "Chiffon Blouse            ";
                anItem[3].UnitPrice = 265.00M;
    
                anItem[4] = new ItemManager.StoreItem();
                anItem[4].ItemNumber = 974937;
                anItem[4].ItemName = "Bow Belt Skirtsuit        ";
                anItem[4].UnitPrice = 245.55M;
    
                anItem[5] = new ItemManager.StoreItem();
                anItem[5].ItemNumber = 743765;
                anItem[5].ItemName = "Cable-knit Sweater        ";
                anItem[5].UnitPrice = 45.55M;
    
                anItem[6] = new ItemManager.StoreItem();
                anItem[6].ItemNumber = 747635;
                anItem[6].ItemName = "Jeans with Heart Belt     ";
                anItem[6].UnitPrice = 25.65M;
    
                anItem[7] = new ItemManager.StoreItem();
                anItem[7].ItemNumber = 765473;
                anItem[7].ItemName = "Fashionable mini skirt    ";
                anItem[7].UnitPrice = 34.55M;
    
                anItem[8] = new ItemManager.StoreItem();
                anItem[8].ItemNumber = 754026;
                anItem[8].ItemName = "Double Dry Pants          ";
                anItem[8].UnitPrice = 28.55M;
    
                anItem[9] = new ItemManager.StoreItem();
                anItem[9].ItemNumber = 730302;
                anItem[9].ItemName = "Romantic Flower Dress     ";
                anItem[9].UnitPrice = 24.95M;
    
                anItem[10] = new ItemManager.StoreItem();
                anItem[10].ItemNumber = 209579;
                anItem[10].ItemName = "Cotton Polo Shirt         ";
                anItem[10].UnitPrice = 45.75M;
    
                anItem[11] = new ItemManager.StoreItem();
                anItem[11].ItemNumber = 267583;
                anItem[11].ItemName = "Pure Wool Cap             ";
                anItem[11].UnitPrice = 25.00M;
    
                anItem[12] = new ItemManager.StoreItem();
                anItem[12].ItemNumber = 248937;
                anItem[12].ItemName = "Striped Cotton Shirt      ";
                anItem[12].UnitPrice = 65.55M;
    
                anItem[13] = new ItemManager.StoreItem();
                anItem[13].ItemNumber = 276057;
                anItem[13].ItemName = "Two-Toned Ribbed Crewneck ";
                anItem[13].UnitPrice = 9.75M;
    
                anItem[14] = new ItemManager.StoreItem();
                anItem[14].ItemNumber = 267945;
                anItem[14].ItemName = "Chestnut Italian Shoes    ";
                anItem[14].UnitPrice = 165.75M;
    
                anItem[15] = new ItemManager.StoreItem();
                anItem[15].ItemNumber = 409579;
                anItem[15].ItemName = "Collar and Placket Jacket ";
                anItem[15].UnitPrice = 265.15M;
    
                anItem[16] = new ItemManager.StoreItem();
                anItem[16].ItemNumber = 467583;
                anItem[16].ItemName = "Country Coat Rugged Wear  ";
                anItem[16].UnitPrice = 35.55M;
    
                anItem[17] = new ItemManager.StoreItem();
                anItem[17].ItemNumber = 448937;
                anItem[17].ItemName = "Carpenter Jeans           ";
                anItem[17].UnitPrice = 24.95M;
    
                anItem[18] = new ItemManager.StoreItem();
                anItem[18].ItemNumber = 476057;
                anItem[18].ItemName = "Dbl-Cushion Tennis Shoes  ";
                anItem[18].UnitPrice = 48.75M;
    
                anItem[19] = new ItemManager.StoreItem();
                anItem[19].ItemNumber = 467945;
                anItem[19].ItemName = "Stitched Center-Bar Belt  ";
                anItem[19].UnitPrice = 32.50M;
    
                // Order Processing
                // Request an item number from the user
                try
                {
                    Console.Write("Enter Item Number: ");
                    ItemID = long.Parse(Console.ReadLine());
                }
                catch (FormatException)
                {
            Console.WriteLine("Invalid Number - The program will terminate\n");
                }
    
                // Scan the list
                for (int i = 0; i < 20; i++)
                {
                    // Find out if the number typed exists in the list
                    if (ItemID == anItem[i].ItemNumber)
                    {
                        // If the number matches one of the numbers in the list
                        // Create a StoreItem value from it
                        Description = anItem[i].ItemName;
                        Price = anItem[i].UnitPrice;
    
                        // Identify the category of the item
                        if (ItemID >= 900000)
                            Category = "Women";
                        else if (ItemID >= 700000)
                            Category = "Girls";
                        else if (ItemID >= 400000)
                            Category = "Boys";
                        else
                            Category = "Men";
                    }
                }
    
                // Display the receipt
                Console.WriteLine("Receipt");
                Console.WriteLine("Item Number: {0}", ItemID);
            Console.WriteLine("Description: {0} - {1}", Category, Description);
                Console.WriteLine("Unit Price:  {0:C}\n", Price);
                return 0;
            }
        }
    }
  5. Execute the application and test it. Here is an example:
     
    Enter Item Number: 987598
    Receipt
    Item Number: 987598
    Description: Women - Chiffon Blouse
    Unit Price:  $265.00
    
    Press any key to continue . . .
  6. Close the DOS window

A Class Array Passed as Argument

Like a primitive type, a class can be passed to a method as argument. Everything is primarily done the same except that, since the argument is an array, each member of the array is a variable in its own right, with a value for each member variable of the class. Based on this, when treating the argument, you can access each of its members individually using its index.

Practical LearningPractical Learning: Passing a Class as Argument

  1. To pass an array of a class to a method as argument, change the file as follows:
     
    using System;
    using ItemManager;
    
    namespace DepartmentStore6
    {
        class Program
        {
            static int Main(string[] args)
            {   
    	    StoreItem[] SaleItem = new StoreItem[20];
    
    	    CreateInventory(SaleItem);
    
    	    int Choice = 1;
    
    	    Console.WriteLine("Welcome to Emilie Department Store");
    	    // Present a short menu to the user
    	    do
    	    {
    		// Let the user specify the action to take
    		try 
    		{
    			Console.WriteLine("What action do you want to take?");
    		Console.WriteLine("1 - I want to see the store inventory");
    		Console.WriteLine("2 - I want to process a customer order");
    			Console.Write("Your Choice? ");
    			Choice = int.Parse(Console.ReadLine());
    		}
    		catch(FormatException)
    		{
    Console.WriteLine("Invalid choice - The character you typed is excluded!");
    		}
    					
    		if( Choice != 1 && Choice != 2 )
    		Console.WriteLine("Invalid Number - Please Try Again!\n");
    		} while( Choice != 1 && Choice != 2 );
    
    		// Take action based on the user's choice
    		if( Choice == 1 )
    			ShowInventory(SaleItem);
    		else
    			ProcessOrder(SaleItem);
    
                return 0;
    	}
    
    	// This method is used to process an order
    	// It fills a StoreItem array argument passed to it
    	static void ProcessOrder(StoreItem[] Order)
    	{
    	    string  Category = "Category";
    	    long    ItemID = 0;
    	    string  Description = "Unknown";
    	    decimal Price = 0.00M;
    
    	    // Order Processing
     	    // Request an item number from the user
    	    try 
    	    {
    		Console.Write("Enter Item Number: ");
    		ItemID = long.Parse(Console.ReadLine());
    	    }
    	    catch(FormatException)
    	    {
    	Console.WriteLine("Invalid Number - The program will terminate\n");
    	    }
    
    	    // Scan the list
    	    for(int i = 0; i < 20; i++)
    	    {
    		// Find out if the number typed exists in the list
    		if( ItemID == Order[i].ItemNumber )
    		{
    		    // If the number matches one of the itemsin the list
    		    // get the characteristics of that item
    		    Description = Order[i].ItemName;
    		    Price = Order[i].UnitPrice;
                        
    		    // Identify the category of the item
    		    if( ItemID >= 900000 )
    			Category = "Women";
    		    else if( ItemID >= 700000 )
    			Category = "Girls";
    		    else if( ItemID >= 400000 )
    			Category = "Boys";
    		    else
    			Category = "Men";
    		}
    	    }
    
                Console.WriteLine("\n=========================================");
    	    Console.WriteLine("Receipt");
                Console.WriteLine("-----------------------------------------");
    	    Console.WriteLine("Item Number: {0}", ItemID);
    	    Console.WriteLine("Description: {0} - {1}", Category, Description);
                Console.WriteLine("Unit Price:  {0:C}", Price);
                Console.WriteLine("=========================================\n");
    
    	    Console.WriteLine();
    	}
    		
    // This method can be called to display the current inventory of the store
    	static void ShowInventory(StoreItem[] Items)
    	{
    	    // Display the inventory
    	    string  Category = "Category";
    	    Console.WriteLine("Store Inventory");
    Console.WriteLine("=======+==========+===========================+=========");
    Console.WriteLine("Item # | Category | Description               | Price");
    	    for(int i = 0; i < 20; i++)
    	    {
    		if( Items[i].ItemNumber >= 900000 )
    			Category = " Women";
    		else if( Items[i].ItemNumber >= 700000 )
    			Category = " Girls";
    		else if( Items[i].ItemNumber >= 400000 )
    			Category = " Boys ";
    		else
    			Category = " Men  ";
                    
    Console.WriteLine("-------+----------+---------------------------+---------");
    		Console.WriteLine("{0} | {1}   | {2}| {3}",
    		Items[i].ItemNumber, Category,
    		Items[i].ItemName, Items[i].UnitPrice);
    	    }
    
    Console.WriteLine("=======+==========+===========================+=========\n");
    	}
    
    	// This method creates an inventory of the items available in the store
    	// It receives an empty list as a StoreItem array 
    	// but fills it with the necessary item
    	static void CreateInventory(StoreItem[] anItem)
    	{
    	    // Create the list of sales items
    	    anItem[0] = new StoreItem();
    	    anItem[0].ItemNumber = 947783;
    	    anItem[0].ItemName   = "Double-faced wool coat    ";
    	    anItem[0].UnitPrice  = 275.25M;
    				
    			anItem[1] = new StoreItem();
    			anItem[1].ItemNumber = 934687;
    			anItem[1].ItemName   = "Floral Silk Tank Blouse   ";
    			anItem[1].UnitPrice  = 180.00M;
    				
    			anItem[2] = new StoreItem();
    			anItem[2].ItemNumber = 973947;
    			anItem[2].ItemName   = "Push Up Bra               ";
    			anItem[2].UnitPrice  = 50.00M;
    				
    			anItem[3] = new StoreItem();
    			anItem[3].ItemNumber = 987598;
    			anItem[3].ItemName   = "Chiffon Blouse            ";
    			anItem[3].UnitPrice  = 265.00M;
    				
    			anItem[4] = new StoreItem();
    			anItem[4].ItemNumber = 974937;
    			anItem[4].ItemName   = "Bow Belt Skirtsuit        ";
    			anItem[4].UnitPrice  = 245.55M;
    
    			anItem[5] = new StoreItem();
    			anItem[5].ItemNumber = 743765;
    			anItem[5].ItemName   = "Cable-knit Sweater        ";
    			anItem[5].UnitPrice  = 45.55M;
    
    			anItem[6] = new StoreItem();
    			anItem[6].ItemNumber = 747635;
    			anItem[6].ItemName   = "Jeans with Heart Belt     ";
    			anItem[6].UnitPrice  = 25.65M;
    			
    			anItem[7] = new StoreItem();
    			anItem[7].ItemNumber = 765473;
    			anItem[7].ItemName   = "Fashionable mini skirt    ";
    			anItem[7].UnitPrice  = 34.55M;
    			
    			anItem[8] = new StoreItem();
    			anItem[8].ItemNumber = 754026;
    			anItem[8].ItemName   = "Double Dry Pants          ";
    			anItem[8].UnitPrice  = 28.55M;
    			
    			anItem[9] = new StoreItem();
    			anItem[9].ItemNumber = 730302;
    			anItem[9].ItemName   = "Romantic Flower Dress     ";
    			anItem[9].UnitPrice  = 24.95M;
    			
    			anItem[10] = new StoreItem();
    			anItem[10].ItemNumber = 209579;
    			anItem[10].ItemName   = "Cotton Polo Shirt         ";
    			anItem[10].UnitPrice  = 45.75M;
    			
    			anItem[11] = new StoreItem();
    			anItem[11].ItemNumber = 267583;
    			anItem[11].ItemName   = "Pure Wool Cap             ";
    			anItem[11].UnitPrice  = 25.00M;
    			
    			anItem[12] = new StoreItem();
    			anItem[12].ItemNumber = 248937;
    			anItem[12].ItemName   = "Striped Cotton Shirt      ";
    			anItem[12].UnitPrice  = 65.55M;
    			
    			anItem[13] = new StoreItem();
    			anItem[13].ItemNumber = 276057;
    			anItem[13].ItemName   = "Two-Toned Ribbed Crewneck ";
    			anItem[13].UnitPrice  = 9.75M;
    			
    			anItem[14] = new StoreItem();
    			anItem[14].ItemNumber = 267945;
    			anItem[14].ItemName   = "Chestnut Italian Shoes    ";
    			anItem[14].UnitPrice  = 165.75M;
    			
    			anItem[15] = new StoreItem();
    			anItem[15].ItemNumber = 409579;
    			anItem[15].ItemName   = "Collar and Placket Jacket ";
    			anItem[15].UnitPrice  = 265.15M;
    			
    			anItem[16] = new StoreItem();
    			anItem[16].ItemNumber = 467583;
    			anItem[16].ItemName   = "Country Coat Rugged Wear  ";
    			anItem[16].UnitPrice  = 35.55M;
    			
    			anItem[17] = new StoreItem();
    			anItem[17].ItemNumber = 448937;
    			anItem[17].ItemName   = "Carpenter Jeans           ";
    			anItem[17].UnitPrice  = 24.95M;
    			
    			anItem[18] = new StoreItem();
    			anItem[18].ItemNumber = 476057;
    			anItem[18].ItemName   = "Dbl-Cushion Tennis Shoes  ";
    			anItem[18].UnitPrice  = 48.75M;
    			
    			anItem[19] = new StoreItem();
    			anItem[19].ItemNumber = 467945;
    			anItem[19].ItemName   = "Stitched Center-Bar Belt  ";
    			anItem[19].UnitPrice  = 32.50M;
            }
        }
    }
  2. Execute the application. Here is an example:
     
    Welcome to Emilie Department Store
    What action do you want to take?
    1 - I want to see the store inventory
    2 - I want to process a customer order
    Your Choice? 2
    Enter Item Number: 743765
    
    =========================================
    Receipt
    -----------------------------------------
    Item Number: 743765
    Description: Girls - Cable-knit Sweater
    Unit Price:  $45.55
    =========================================
    
    Press any key to continue . . .
  3. Close the DOS window

Returning a Class Array From a Method

Once again, like a regular variable, an array can be returned from a function. The syntax also follows that of a primitive type. When the function ends, you must make sure it returns an array and not a regular variable.

Practical LearningPractical Learning: Return a Class Array From a Function

  1. To return an array of a class type from a method, change the file as follows:
     
    using System;
    using ItemManager;
    
    namespace DepartmentStore6
    {
        class Program
        {
            static int Main(string[] args)
            {   
    	    StoreItem[] SaleItem = CreateInventory();
    
    	    int Choice = 1;
    
    	    . . . No Change
    
                return 0;
    	}
    
    	. . . No Change
    		
    // This method creates an inventory of the items available in the store
    	// It receives an empty list as a StoreItem array
    	// but fills it with the necessary item
    	static StoreItem[] CreateInventory()
    	{
                StoreItem[] anItem = new StoreItem[20];
    
    	    // Create the list of sales items
    	    anItem[0] = new StoreItem();
    	    anItem[0].ItemNumber = 947783;
    	    anItem[0].ItemName   = "Double-faced wool coat    ";
    	    anItem[0].UnitPrice  = 275.25M;
    				
    	    . . . No Change
    			
    	    anItem[19] = new StoreItem();
    	    anItem[19].ItemNumber = 467945;
    	    anItem[19].ItemName   = "Stitched Center-Bar Belt  ";
    	    anItem[19].UnitPrice  = 32.50M;
    
                return anItem;
            }
        }
    }
  2. Execute the application and test it
  3. Close the DOS window
 

Previous Copyright © 2006 FunctionX, Inc.