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

Author: Zunnair
Download Source Code : 848_array.zip

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

Array class provides us both static and non static functions.

Static Functions:

Static functions provides us facility to to do some operations on array of any type without making array class object.

Non Static Functions:

Array class also provides us some non static functions to perform some basic array operations. To use these functions we have to make object of array class. To make object we have to specify that which type of array we are going to make.

Making object of Array class:

As I told that if we want to make an object we have to tell array type. Here we will deal with array of integer type.

To declare an object for integer array write the following code.

C#

Array arr = new int[10];

VB

Dim arr As Array = New Integer(9) {}

 

This is a simple code to declare an object of array class for integer array.

Initializing Array by Default Constructor:

We can also initialize whole array through a default constructor. For this purpose array class provides us a function Initialize().

This function takes nothing as a parameter and returns nothing.

To initialize write the following code.

C#

Array arr = new int[10];

arr.Initialize();

 

VB

Dim arr As Array = New Integer(9) {}

arr.Initialize()

This simple code initializes array by default constructor.

Set and get value in an array using Array class:

Array class provides us functions to set a value in an array or to get a value from an array.

To set a value we use SetValue() function. It takes value and array index as a parameter and sets the value on that index. If index is out of range than this functions throws an exception.

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

Now write the following code on button click event:

C#

private void btn_set_Click(object sender, EventArgs e)

        {

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

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

            {

                arr.SetValue(Convert.ToInt16((txt_setv.Text)), a);

                MessageBox.Show("VALUE SET");

            }

            else

            {

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

 

            }

        }

VB

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

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

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

            arr.SetValue(Convert.ToInt16((txt_setv.Text)), a)

            MessageBox.Show("VALUE SET")

        Else

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

 

        End If

    End Sub

This simple code sets a value on given index.

To get a value we use GetValue() function. It takes array index as a parameter and returns the value. If index is out of range than this functions throws an exception.

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

        {

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

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

            {               

                txt_getv.Text =arr.GetValue(a).ToString();               

            }

            else

            {

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

 

            }

        }

VB

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

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

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

            txt_getv.Text = arr.GetValue(a).ToString()

        Else

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

 

        End If

    End Sub

This simple code gets value from array.

Getting Upper and Lower Bound of an Array:

Array class provides us functions to get the upper and lower bound or minimum and maximum index value of an array.

To get the lower bound of an array we use GetLowerBound() funtion. It takes dimention as a parameter and returns the lower bound of array.

To demonstrate 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 bn_lower_Click(object sender, EventArgs e)

        {

            txt_lower.Text=arr.GetLowerBound(0).ToString();

        }

VB

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

        txt_lower.Text = arr.GetLowerBound(0).ToString()

    End Sub

 

This is a simple code to get lower bound.

To get the upper bound of an array we use GetUpperBound() funtion. It takes dimention as a parameter and returns the upper bound of array.

To demonstrate 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_upper_Click(object sender, EventArgs e)

        {

            txt_upper.Text = arr.GetUpperBound(0).ToString();

        }

VB

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

        txt_upper.Text = arr.GetUpperBound(0).ToString()

    End Sub


This is a simple code to get upper bound.

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 Get and Set file Last Time in which data Written to file system.io namespace c# (.net 2.0).

Disclaimer - Privacy
© 2002-2017 DevASP.net