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
 

Get total number of elements, search a value and reverse the array list using “array list” class in vb and C# .net.

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

In this simple article you will learn that how to we get total number of elements, search a value and reverse the array list using “array list” class in vb and C# .net.

 Get the count of array list:

In order to get the count of the array list, array list class provides us a property named as count. It returns an integer which tells us the total number present in 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_count_Click(object sender, EventArgs e)

        {

            int count;

            count=obj.Count;

            MessageBox.Show("total elements present in array list are"+Convert.ToString(count));

 

        }

Vb:

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

        Dim count As Integer

        count = obj.Count

 

        MessageBox.Show("total elements present in array list are" & Convert.ToString(count))

    End Sub

 

Value contained by array list:

In order to check that whether the given value is contained by the array list, array list provide us a function named as contains(). This function takes the value as an argument about which you want to know. Its return type is bool. If the array list contains the given value it will return true and if the list does not contain then it will return false.

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

Now write the following code on Button click event:

C#:                                                                                 

private void btn_clone_Click(object sender, EventArgs e)

        {

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

            if (obj.Contains(val))

            {

                MessageBox.Show("array list contains the given value");

            }

            else

            {

                MessageBox.Show("array list doesn't contains the given value");

 

            }

        }

Vb:

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

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

        If obj.Contains(val) Then

            MessageBox.Show("array list contains the given value")

        Else

 

            MessageBox.Show("array list doesn't contains the given value")

        End If

    End Sub

 

Reverse the list:

In order to reverse the array list, array list provides us a function reverse(). This is an overloaded function and has two overloaded methods/functions. I will discuss both of its overloaded functions.

Reverse all:

To reverse the whole array list , use the function reverse(). It takes no arguments and its return type is void.

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_rev_Click(object sender, EventArgs e)

        {

            obj.Reverse();

            MessageBox.Show("your list is reverse");

           

        }

Vb:

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

        obj.Reverse()

 

        MessageBox.Show("your list is reverse")

    End Sub

 

Reverse some specific index:

In order to reverse some specific indexes, array list provides us an overloaded function reverse(). It takes two arguments as parameters. One is the specific index and the other one is count which tells that how many indexes from the specific indes are going to be reversed. Its return type is also void.

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

Now write the following code on Button click event:

C#:         

private void btn_rev2_Click(object sender, EventArgs e)

        {

            int index = Convert.ToInt16(txt_ind.Text);

            int count = Convert.ToInt16(txt_count.Text);

            obj.Reverse(index, count);

            MessageBox.Show("array list is reverse from index" + Convert.ToString(index) + "and number of reversed items are" + Convert.ToString(count));

        }

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

        Dim index As Integer = Convert.ToInt16(txt_ind.Text)

        Dim count As Integer = Convert.ToInt16(txt_count.Text)

        obj.Reverse(index, count)

        MessageBox.Show(("array list is reverse from index" & Convert.ToString(index) & "and number of reversed items are") + Convert.ToString(count))

    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 will tell you that how you can get the total number of elements present in array list and how to check that specific value is contained by the array list and also how to reverse the array list using “array list” class in vb and C# .net..

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Add and clear values in array list using “array list” class in Vb and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net