Sunday, November 29, 2009

how to use arraylist - arraylist example

here i am going to write a program which shows example of arraylist.
think in this case that i am using arraylist to associate each
student with courses taken.

arraylists are used for collection type purposes.so it is used
widely.

program codes are following-
public class Course
    {
        private string name;
        public string Name
        {
            get { return name; }
            //set { name = value; }
        }
        public Course(string name) {
            this.name = name;
        }
        public string Output()
        {
             return "Course Name : "+ name + " ";
        }
    }
public class Student
    {
        private string name;
        public string Name
        {
            get { return name; }
        }
        private ArrayList courseTaken = new ArrayList();
        public ArrayList CourseTaken
        {
            get { return courseTaken; }
            //set { courseTaken = value; }
        }      
        public Student(string name) {
            this.name = name;      
        }
        public string Output() {
            return "Student Name : "+ name + "\n";
        }
    }
here is the entry form::
public partial class MridulExampleUi : Form
    {
        public MridulExampleUi()
        {
            InitializeComponent();
        }      
        private void MridulExampleUi_Load(object sender, EventArgs e)
        {}
        private void ShowArraylistButton_Click(object sender, EventArgs e)
        {       
            Student studentObj = new Student("mridul");
            Student studentObj3 = new Student("someone");
            Course courseObj1 = new Course("Programming");
            Course courseObj2 = new Course("Web designing");                
            studentObj.CourseTaken.Add(courseObj1);
            studentObj.CourseTaken.Add(courseObj2);
            studentObj3.CourseTaken.Add(courseObj1);         
            ArrayList arrayObj = new ArrayList();
            arrayObj.Add(studentObj);
            arrayObj.Add(studentObj3);          

            string message="";
            foreach (Student studentObj2 in arrayObj)
            {
                message+=studentObj2.Output();
                foreach(Course courseObj3 in studentObj2.CourseTaken)
                {
                  message+=courseObj3.Output();\                     
                }
                message += "\n\n";
            }
            MessageBox.Show(message);          
        }}
here is the program entry class::
static class Program
    {
        ///


        /// The main entry point for the application.
        ///

        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new MridulExampleUi());
        }
    }

2 comments:

Unknown said...

hi
i saw your querie in adsense forum, you want to activate adsense to your mil? i can get it done for you, contact my email. thanks
parc1978@gmail.com

Mridul said...

hey rahul
thank u very much for your comment.I saw your site its very good also.do you write articles?
check my site www.LearnEarnShare.com for c# resources.if u want to see your original articles with your name in my site then send me article.we will be helpfull to each other.keep commenting...

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Powered by Blogger