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
 

Array operations (Reverse, Clear Values, Get Values) using (Static Functions) “ARRAY CLASS” in VB and C# (.net 2.0) (Part 3).

Author: Zunnair
Download Source Code : 856_array.zip

In this simple article you will learn some operations on array using “ARRAY CLASS” in VB and C# .net 2.0.

 

To understand this article read part1 and part2 in which I discussed some basic operations related to arrays.
Static Functions:
Reverse:
In previous two articles we saw that there are some non static functions which array class provides us to perform different operations. Now there are some static functions which array class also provides us.
Array class provides us a facility through a function to reverse array object values.
It provides us one overloaded function for this purpose.
Reverse () function takes array class object as a parameter and reverse the array. It returns nothing.
To demonstrate make a window application. Drag one text box and one button on the form. Press button, reversed array will show in the text box.
Now write the following code on button click event:
 C#
private void btn_reverse_Click(object sender, EventArgs e)
        {
            arr.SetValue(1, 0);
            arr.SetValue(2, 1);
            arr.SetValue(3, 2);
            arr.SetValue(4, 3);
            arr.SetValue(5, 4);
            Array.Reverse(arr);
            MessageBox.Show("ARRAY HAS REVERSED");
            txt_REVERSE.Text = arr.ToString();
        }
VB
Private Sub btn_reverse_Click(ByVal sender As Object, ByVal e As EventArgs)
        arr.SetValue(1, 0)
        arr.SetValue(2, 1)
        arr.SetValue(3, 2)
        arr.SetValue(4, 3)
        arr.SetValue(5, 4)
        Array.Reverse(arr)
        MessageBox.Show("ARRAY HAS REVERSED")
        txt_REVERSE.Text = arr.ToString()
    End Sub
Other overloaded method of Reverse () function takes object of array, an index and length of array.
This function reverses the array from a particular index to the end.
To demonstrate make a window application. Drag two text boxes and one button on the form. Enter index in the text box and press button, reversed array will show in the text box.
Now write the following code on button click event:
 C#
private void btn_rev_Click(object sender, EventArgs e)
        {
            arr.SetValue(1, 0);
            arr.SetValue(2, 1);
            arr.SetValue(3, 2);
            arr.SetValue(4, 3);
            arr.SetValue(5, 4);
            string str= txt_ind.Text;
            int a=Convert.ToInt32(str);
            if (a >= 0 && a < arr.Length)
            {
                Array.Reverse(arr, Convert.ToInt16(txt_ind.Text), 5);
                MessageBox.Show("ARRAY REVERSED");
                txt_REVERSE.Text = arr.ToString();
            }
            else
            {
                MessageBox.Show("INVALID INDEX");
            }
        }
VB
Private Sub btn_rev_Click(ByVal sender As Object, ByVal e As EventArgs)
        arr.SetValue(1, 0)
        arr.SetValue(2, 1)
        arr.SetValue(3, 2)
        arr.SetValue(4, 3)
        arr.SetValue(5, 4)
        Dim str As String = txt_ind.Text
        Dim a As Integer = Convert.ToInt32(str)
        If a >= 0 AndAlso a < arr.Length Then
            Array.Reverse(arr, Convert.ToInt16(txt_ind.Text), 5)
            MessageBox.Show("ARRAY REVERSED")
            txt_REVERSE.Text = arr.ToString()
        Else
            MessageBox.Show("INVALID INDEX")
 
        End If
End Sub
This simple code will reverse the array.
 
Clear Array Values:
Array class provides us a facility to clear the values of array from a particular index. This function sets the null, zero or false on those indexes depends on the type of values in the array.
Clear () function takes object of array class, index and length of the array as a parameter and returns nothing.
To demonstrate make a window application. Drag two text boxes and one button on the form. Enter index in the text box and press button, array after clear will show in the text box.
Now write the following code on button click event:
C#
private void btn_clear_Click(object sender, EventArgs e)
        {
            arr.SetValue(1, 0);
           arr.SetValue(2, 1);
            arr.SetValue(3, 2);
            arr.SetValue(4, 3);
            arr.SetValue(5, 4);
            string str = txt_indexof.Text;
            int a = Convert.ToInt32(str);
            if (a >= 0 && a < arr.Length)
            {
                Array.Clear(arr, Convert.ToInt16(txt_ind.Text), 5);
                MessageBox.Show("ARRAY's VALUES CLEARED");
                txt_clear.Text = arr.ToString();
            }
            else
            {
                MessageBox.Show("INVALID INDEX");
            }
        }
VB
Private Sub btn_clear_Click(ByVal sender As Object, ByVal e As EventArgs)
        arr.SetValue(1, 0)
        arr.SetValue(2, 1)
        arr.SetValue(3, 2)
        arr.SetValue(4, 3)
        arr.SetValue(5, 4)
        Dim str As String = txt_indexof.Text
        Dim a As Integer = Convert.ToInt32(str)
        If a >= 0 AndAlso a < arr.Length Then
            Array.Clear(arr, Convert.ToInt16(txt_ind.Text), 5)
            MessageBox.Show("ARRAY's VALUES CLEARED")
            txt_clear.Text = arr.ToString()
        Else
            MessageBox.Show("INVALID INDEX")
        End If
End Sub
This is the simple code to clear values from the array.
Get Index:
To search the index of a particular value in the array, array class provides the function Index Of (). This function takes object of array class and value. it returns index as a integer.
To demonstrate make a window application. Drag one text box and one button on the form. Enter value in the text box and press button.
Now write the following code on button click event:
C#
private void btn_index_Click(object sender, EventArgs e)
        {
            arr.SetValue(1, 0);
            arr.SetValue(2, 1);
            arr.SetValue(3, 2);
            arr.SetValue(4, 3);
            arr.SetValue(5, 4);
            string str = txt_indexof.Text;
            int a = 0;
            a = Convert.ToInt16(str);
            int b = Array.IndexOf(arr, a);
            if (b == -1)
            {
                MessageBox.Show("value not exist in array");
            }
            else
            {
                MessageBox.Show(b.ToString());
            }
        }
VB
Private Sub btn_index_Click(ByVal sender As Object, ByVal e As EventArgs)
        arr.SetValue(1, 0)
        arr.SetValue(2, 1)
        arr.SetValue(3, 2)
        arr.SetValue(4, 3)
        arr.SetValue(5, 4)
        Dim str As String = txt_indexof.Text
        Dim a As Integer = 0
        a = Convert.ToInt16(str)
        Dim b As Integer = Array.IndexOf(arr, a)
        If b = -1 Then
            MessageBox.Show("value not exist in array")
        Else
            MessageBox.Show(b.ToString())
        End If
End Sub
By write this code you can get index of a value in array.
Now write the following code on FORM LOAD event:
C#
private void Form1_Load(object sender, EventArgs e)
        {
 
            this.Text = "Devasp.net ARRAY Application";
        }
VB
Private Sub Form1_Load(ByVal sender As Object, ByVal e As EventArgs)
 
        Me.Text = "Devasp.net ARRAY Application"
 End Sub
 
This simple article tells some operations on array using “ARRAY CLASS” in VB and C# .net 2.0.
   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to do sorting using “Insertion Sort” algorithm in C# (.net 2.0)

Disclaimer - Privacy
© 2002-2017 DevASP.net