Array of Objects

by Jagadish Pulakhandam on 8/25/2011 10:13:15 AM
Rated 0 from 0 votes
Brief: Demonstrates Object Array (array of Objects) using C#
Posted to: Basics of Object Oriented Programming in .NET
Add to DiggAdd to del.icio.usAdd to FURLAdd to RedditAdd to YahooAdd to BlinklistAdd to GoogleAdd to ma.gnoliaAdd to ShadowsAdd to Technorati

Following is a simple class:

01.using System;
02.using System.Collections.Generic;
03.using System.Linq;
04.using System.Text;
05.using System.Windows.Forms;
06. 
07.namespace SampleObjectArray
08.{
09.    class Rectangle
10.    {
11.        public int length;
12.        public int breadth;
13. 
14.        public void DispArea()
15.        {
16.            MessageBox.Show("Area = " + (length * breadth));
17.        }
18. 
19.        public void DispPerimeter()
20.        {
21.            MessageBox.Show("Perimeter = " + (2 * (length + breadth)));
22.        }
23.    }
24.}

Following is the code which works with array of objects:

01.using System;
02.using System.Collections.Generic;
03.using System.ComponentModel;
04.using System.Data;
05.using System.Drawing;
06.using System.Linq;
07.using System.Text;
08.using System.Windows.Forms;
09. 
10.namespace SampleObjectArray
11.{
12.    public partial class Form1 : Form
13.    {
14.        public Form1()
15.        {
16.            InitializeComponent();
17.        }
18. 
19.        private void button1_Click(object sender, EventArgs e)
20.        {
21.            //object array, "r" can contain five "Rectangle" objects
22.            Rectangle[] r = new Rectangle[5];
23. 
24.            r[0] = new Rectangle() {
25.                length = 10,
26.                breadth = 20
27.            };
28. 
29.            r[1] = new Rectangle()
30.            {
31.                length = 15,
32.                breadth = 25
33.            };
34. 
35.            r[2] = new Rectangle()
36.            {
37.                length = 12,
38.                breadth = 22
39.            };
40. 
41.            r[3] = new Rectangle()
42.            {
43.                length = 15,
44.                breadth = 26
45.            };
46. 
47.            r[4] = new Rectangle()
48.            {
49.                length = 17,
50.                breadth = 28
51.            };
52. 
53.            //Access every item in an array using for loop
54.            for (int i = 0; i < r.Length; i++)
55.            {
56.                r[i].DispArea(); //access a method in an object in an array
57.            }
58. 
59.            //Access every item in an array using for foreach
60.            foreach (Rectangle o in r) //
61.            {
62.                o.DispPerimeter();
63.            }
64. 
65.        }
66.    }
67.}



Join the .NET Code Central Community and join the discussion!
Signing-up is FREE and quick. Do it now, we want to hear your opinion
0

Rated 0 from 0 votes ( login  to rate)
DotnetKicks DotnetKicksDe DotNetShoutout

Attachments / Source Code
You need to Login or Join for FREE to download the following