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
 

How to add, clear and getting total number of values in sorted list using “sorted list” class in VB and C# .net.

Author: Maliha Atteeq
Download Source Code : 944_sorted list class.zip

In this simple article you will learn that how we can add and clear values from sorted list and how we can get total number of elements in list using “sorted list” class in VB and c#.net.

 Constructors:

The sorted class has six overloaded constructors. I will explain the most used and common constructors in my article.

First:

First constructor is blank constructor it does not take any value as parameter.

C#:

System.Collections.SortedList obj = new System.Collections.SortedList();

VB:

Dim obj As New System.Collections.SortedList()

Second:

Second constructor is parameterized constructor. It takes the initial capacity of the sorted list as an argument. Initial capacity is an integer type variable which can contain any integral value. Whatever you give the value, the sorted list object contains that much members or indexes. This tells the size or capacity of the list.

C#:

 System.Collections.SortedList obj = new System.Collections.SortedList(10);

VB:

Dim obj As New System.Collections.SortedList(10)

Add Element:

You can simply add objects in the list by using function, named add (). The Add function takes two parameters as an argument. First is the key and second is the original value which you want to enter in list. This function returns nothing.

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

Now write the following code on Button click event:

 

C#:

       private void btn_Add_Click(object sender, EventArgs e)

        {

            int key =Convert.ToInt32(txt_key.Text);

            int value = Convert.ToInt32(txt_value.Text);

            obj.Add(key, value);

            MessageBox.Show("ELEMENT ADDED");

        }

VB:

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

        Dim key As Integer = Convert.ToInt32(txt_key.Text)

        Dim value As Integer = Convert.ToInt32(txt_value.Text)

        obj.Add(key, value)

        MessageBox.Show("ELEMENT ADDED")

    End Sub

This simple code adds the value in sorted list.

Clear All Elements:

To clear the elements of the sorted list, sorted list class provides us a function. The function given by class is clear () and it takes no parameters and also returns nothing. This function clears all the elements of the sorted 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_clear_Click(object sender, EventArgs e)

   {

            obj.Clear();

            MessageBox.Show("ELEMENT REMOVED FROM THE LIST");

   }

VB:

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

        obj.Clear()

        MessageBox.Show("ELEMENT REMOVED FROM THE LIST")

    End Sub

This is simple code to clear all the values from list.

Count:

To get that how many values are in sorted list for this purpose sorted list class provides a property named count. This property returns an integer. This integer tells the number of values added in 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 c = obj.Count;

           string str = "TOTAL NUMBER OF ELEMENTS ARE " + c.ToString();

            MessageBox.Show(str);

   }

VB:

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

        Dim c As Integer = obj.Count

        Dim str As String = "TOTAL NUMBER OF ELEMENTS ARE " & c.ToString()

        MessageBox.Show(str)

    End Sub

This is simple code to count the number of values in list.

Now write the following code on FORM LOAD event:

C#

        private void Form1_Load(object sender, EventArgs e)

        {

            this.Text = "DEVASP SORTED LIST APPLICATION";

            obj.Add(0, 10);

            obj.Add(1, 20);

            obj.Add(2, 30);

            obj.Add(3, 40);

            obj.Add(4, 50);

            obj.Add(5, 60);

 

        }

VB

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

        Me.Text = "DEVASP SORTED LIST APPLICATION"

        obj.Add(0, 10)

        obj.Add(1, 20)

        obj.Add(2, 30)

        obj.Add(3, 40)

        obj.Add(4, 50)

        obj.Add(5, 60)

    End Sub

This simple article tells that we can how we can add and clear values from sorted list and how we can get total number of elements in list using “sorted list” class in Vb and c#.net.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to check object life using “Weak Reference class” in VB and C# .net.

Disclaimer - Privacy
© 2002-2017 DevASP.net