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 (copy, length, read only, fixed size, convert to string) using “ARRAY CLASS” (Non Static functions) in C# (.net 2.0) (Part 2)

Author: Zunnair
Download Source Code : 849_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, in which I discussed some basic operations related to arrays.

Copy Values of One Array into another Array.

Array class provides us a facility through a function to copy values of one array to another array. Arrays should be of same type.

We use CopyTo() function. This function takes an object of array type and index of array. It copies the value on given index from an array to other array on same index.

To demonstrate make a window application. Drag one text box and one button on the form. Write index number in text box and press button.

Now write the following code on button click event:

 C#

private void btn_copy_Click(object sender, EventArgs e)

        {

            int a = Convert.ToInt16(txt_copy.Text);

            Array cpy = new int[20];

            if (a >= 0 && a < arr.Length)

            {

                if (a < cpy.Length)

                {                   

                    arr.CopyTo(cpy, a);

                    MessageBox.Show("COPIED");

                }

                else

                {

                    MessageBox.Show("DESTINATION ARRAY SIZE IS LESS THAN INDEX");

                }

            }

            else

            {

                MessageBox.Show("SOURCE ARRAY HAS LOWER SIZE THAN INDEX");

 

            }

        }

VB

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

        Dim a As Integer = Convert.ToInt16(txt_copy.Text)

        Dim cpy As Array = New Integer(19) {}

        If a >= 0 AndAlso a < arr.Length Then

            If a < cpy.Length Then

                arr.CopyTo(cpy, a)

                MessageBox.Show("COPIED")

            Else

                MessageBox.Show("DESTINATION ARRAY SIZE IS LESS THAN INDEX")

            End If

        Else

            MessageBox.Show("SOURCE ARRAY HAS LOWER SIZE THAN INDEX")

 

        End If

    End Sub

This simple code will copy values from one array to another array on same index.

Arrays Object Length:

Array class provides us a facility to check the length of the array. For this purpose we c# provides us two ways one is Get Length () function and second is Length.

First discuss Get Length () function this functions takes dimension as a parameter and returns length as an integer.

To demonstrate make a window application. Drag one text box and one button on the form. Write dimension in text box and press button.

Now write the following code on button click event:

C#

private void btn_length_Click(object sender, EventArgs e)

        {

            int a = arr.GetLength(Convert.ToInt16(txt_length.Text));

            MessageBox.Show(a.ToString());

        }     

VB

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

        Dim a As Integer = arr.GetLength(Convert.ToInt16(txt_length.Text))

        MessageBox.Show(a.ToString())

    End Sub

This is the first way to get size of array.

To demonstrate second ways make a window application. Drag one text box and one button on the form, press button.

Now write the following code on button click event:

C#

 

 

private void btn_length_Click(object sender, EventArgs e)

        {

            int a = arr.Length;

            MessageBox.Show(a.ToString());

        }    

VB

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

        Dim a As Integer = arr.Length

        MessageBox.Show(a.ToString())

    End Sub

By write this code you can get length of an array.

Array Is Read Only:

You can also check that array is read only or you have a access to write in array. For this purpose array class provides us non static function.

To demonstrate second ways make a window application. Drag one text box and one button on the form, press button.

Now write the following code on button click event:

C#

private void btn_read_Click(object sender, EventArgs e)

        {

            bool b = arr.IsReadOnly;

            txt_read.Text = b.ToString();

        }    

VB

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

        Dim b As Boolean = arr.IsReadOnly

        txt_read.Text = b.ToString()

    End Sub

Check Size Is Fixed Or Not:

Array class also provides us a facility to check that array has fixed size or not.

To demonstrate second ways make a window application. Drag one text box and one button on the form, press button.

Now write the following code on button click event:

 

C#

private void btn_fixed_Click(object sender, EventArgs e)

        {

            bool b = arr.IsFixedSize;

            txt_fixed.Text = b.ToString();

        }    

VB

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

        Dim b As Boolean = arr.IsFixedSize

        txt_fixed.Text = b.ToString()

    End Sub

Convert Array Object In String:

You can convert array object in string by using To String () function of array class.

To demonstrate second ways make a window application. Drag one text box and one button on the form, press button.

Now write the following code on button click event:

C#

private void btn_string_Click(object sender, EventArgs e)

        {

            string str = string.Empty;

            str = arr.ToString();

            txt_string.Text = str;

        }   

VB

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

        Dim str As String = String.Empty

        str = arr.ToString()

        txt_string.Text = str

    End Sub

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 :
 
   
<< Array operations (Making, Constructor, Initializing, set get values, lower and upper bound) using “ARRAY CLASS” (Non Static functions) in C# (.net 2.0) (Part 1)

Disclaimer - Privacy
© 2002-2017 DevASP.net