Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Add and clear values in array list using “array list” class in Vb and C# .net.

Author: Maliha Atteeq
Download Source Code : 975_arrayList.zip

In this simple article you will learn that how we can declare an object of array list and how we can add and clear values from array list using “array list” class in Vb and c#.net.

 Constructors:

The array list class has 3 overloaded constructors. I will explain the most used and common constructors in my article.

First Constructor:

First constructor is blank constructor it does not take any value as parameter.

To demonstrate make a new window application. Drag one button on from.

Now write the following code on Button click event:

C#:

private void btn_bild_Click(object sender, EventArgs e)

        {

            System.Collections.ArrayList ob = new System.Collections.ArrayList();

        }

Vb:

     Private Sub btn_bild_Click(ByVal sender As Object, ByVal e As EventArgs)

         Dim ob As New System.Collections.ArrayList()

    End Sub

Second Constructor:

Second constructor is parameterized constructor. It takes the initial capacity of the array list as an argument. Initial capacity is an integer type variable which can contain any integral value. Whatever you give the value, the array list object contains that much members or indexes. This tells the size or capacity of the list.

To demonstrate make a new window application. Drag one button and a text box on from.

Now write the following code on Button click event:

C#:

private void build_btn_Click(object sender, EventArgs e)

        {

            int size = Convert.ToInt32(txt_size.Text);

            ob = new System.Collections.ArrayList(size);

            MessageBox.Show("array list is build of size"             +Convert.ToString(size));      

        }

 

Vb:

Private Sub build_btn_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim size As Integer = Convert.ToInt32(txt_size.Text)

        ob = New System.Collections.ArrayList(size)

        MessageBox.Show("array list is build of size" & Convert.ToString(size))

    End Sub

Add an element in array list:

In order to add an element in array list, array list provide us a function named as add(). This function take only one argument as a parameter. It takes the value as a parameter which you want to add into the array list.

To demonstrate make a new window application. Drag one text boxes and one button on form.

Now write the following code on Button click event:

C#:

private void add_btn_Click(object sender, EventArgs e)

        {

            int val = Convert.ToInt16(txt_val.Text);

            ob.Add(val);

            MessageBox.Show("value is added in your array list");

        }

Vb:

Private Sub add_btn_Click(ByVal sender As Object, ByVal e As EventArgs)

        Dim val As Integer = Convert.ToInt16(txt_val.Text)

        ob.Add(val)

        MessageBox.Show("value is added in your array list")

    End Sub

Clear All Elements:

To clear the elements of the array list, array list class provides us a function. The function given by class is clear () and it takes no parameters and also returns nothing. This function clears all the elements of the array list.

To demonstrate make a new window application. Drag one button on from.

Now write the following code on Button click event:

C#:

private void btn_clr_Click(object sender, EventArgs e)

        {

            ob.Clear();

            MessageBox.Show("your array list is clear");

        }

Vb:

Private Sub btn_clr_Click(ByVal sender As Object, ByVal e As EventArgs)

        ob.Clear()

        MessageBox.Show("your array list is clear")

    End Sub

Now write the following code on FORM LOAD event:

C#

        private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP ARRAY LIST APPLICATION";

            ob.Add(10);

            ob.Add( 20);

            ob.Add( 30);

            ob.Add( 40);

            ob.Add( 50);

            ob.Add( 60);

 

        }

VB

Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)

        Me.Text = "DEVASP ARRAY LIST APPLICATION"

            ob.Add(10);

            ob.Add( 20);

            ob.Add( 30);

            ob.Add( 40);

            ob.Add( 50);

            ob.Add( 60);

 

    End Sub

This simple article tells that how we can declare an object of array list and how we can  add and clear values from array list using “array list” class in Vb and c#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Power round Remainder Math operations using Math class VB and C#.net 2.0

Disclaimer - Privacy
© 2002-2017 DevASP.net