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
 

Getting index of specific value in array list using “array list” class in vb and C# .net.

Author: Maliha Atteeq
Download Source Code : 978_arrayList4.zip

In this simple article you will learn that how we can get the index of the specified value in array list using “array list” class in Vb and c#.net.

 Get index of specific value:

In order to get the index of the specific value in list, array list class provides us a function named as indexof(). It is an over loaded function. It depends on your requirement that which type of overloaded function you need in your program.

First overloaded function:

This function takes one argument which is the value as a parameter. Its return type is int. it returns the index of the specific value.

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

        {

            int value;

            int index;

            value = Convert.ToInt16(txt_val.Text);

            index=obj.IndexOf(value);

            MessageBox.Show("index of specific value"+Convert.ToString(index));

        }

VB:

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

        Dim value As Integer

        Dim index As Integer

        value = Convert.ToInt16(txt_val.Text)

        index = obj.IndexOf(value)

        MessageBox.Show("index of specific value" & Convert.ToString(index))

    End Sub

Second overloaded function:

This function takes two arguments which are value and starting index as a parameter. Its return type is int. it returns the index of the specific value.

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

Now write the following code on Button click event:

C#:

private void button1_Click_1(object sender, EventArgs e)

        {

            int value;

            int index;

            int temp;

            value = Convert.ToInt16(txt_valu.Text);

            index = Convert.ToInt16(txt_ind.Text);

            if (index < 0 || index > 4)

            {

                MessageBox.Show("invalid index");

            }

            else

            {

                temp = obj.IndexOf(value, index);

               

            }

            if (temp == -1)

            {

                MessageBox.Show("invalid index");

            }

            else

            {

                MessageBox.Show("index of specific value" + Convert.ToString(temp));

            }

        }

    VB:

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

        Dim value As Integer

        Dim index As Integer

        Dim temp As Integer

        value = Convert.ToInt16(txt_valu.Text)

        index = Convert.ToInt16(txt_ind.Text)

        If index < 0 OrElse index > 4 Then

            MessageBox.Show("invalid index")

        Else

 

            temp = obj.IndexOf(value, index)

        End If

        If temp = -1 Then

            MessageBox.Show("invalid index")

        Else

            MessageBox.Show("index of specific value" & Convert.ToString(temp))

        End If

    End Sub

Third overloaded function:

This function takes three arguments which are value, starting index and number of elements in the list to be search as a parameter. Its return type is int. it returns the index of the specific value.

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

Now write the following code on Button click event:

C#:

private void button1_Click_2(object sender, EventArgs e)

        {

            int value;

            int index;

            int count;

            int temp;

            value = Convert.ToInt16(txt_value.Text);

            index = Convert.ToInt16(txt_index.Text);

            count = Convert.ToInt16(txt_count.Text);

            if (index < 0 || index > 4)

            {

                MessageBox.Show("invalid index");

            }

            else

            {

                temp = obj.IndexOf(value, index, count);

    

                MessageBox.Show("index of specific value" + Convert.ToString(temp));

            }

            

        }

VB:

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

        Dim value As Integer

        Dim index As Integer

        Dim count As Integer

        Dim temp As Integer

        value = Convert.ToInt16(txt_value.Text)

        index = Convert.ToInt16(txt_index.Text)

        count = Convert.ToInt16(txt_count.Text)

        If index < 0 OrElse index > 4 Then

            MessageBox.Show("invalid index")

        Else

            temp = obj.IndexOf(value, index, count)

            MessageBox.Show("index of specific value" & Convert.ToString(temp))

        End If

    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";

         obj.Add(10)

        obj.Add( 20)

        obj.Add( 30)

        obj.Add( 40)

        obj.Add( 50)

        obj.Add( 60)

        }

VB

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

        Me.Text = "DEVASP ARRAY LIST APPLICATION"

        obj.Add(10)

        obj.Add( 20)

        obj.Add( 30)

        obj.Add( 40)

        obj.Add( 50)

        obj.Add( 60)

    End Sub

This simple article tells that how we can get the index of a specific value in array list using ”Array List” class in Vb and c#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Binary search, sorting and insertion in array list using “Array List” class in VB and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net