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
 

Comparison of two objects using “CaseInsensitiveComparer” class in VB and C# (.net).

Author: Shehzad Hemani
Download Source Code : 928_code.zip

In this simple article you will learn that how we can compare two objects of same class using “CaseInsensitiveComparer” class in VB and C# (.net).

 Comparison:

In .net from programming point of view comparison means that we compare two objects according to their values or their references. If the objects have same values then the objects will be equal or if the objects have same references or pointing to same address in memory then the objects are also equal.

It compares the strings ignoring the case in strings.

CaseInsensitiveComparer class:

CaseInsensitiveComparer class provides us a facility to compare the two objects of same class. This class tells whether the values of objects of are same, one’s value is greater then second or one’s value is less then second. We have a function named compare () it takes two objects and returns integer value. This integer value tells that objects values are equal, greater or less.

Equal:

If the values of both objects are equal or their references are equal then the compare () function returns zero.

To demonstrate make a window application. Drag one button on form. Declare two objects of same class pass them in compare function as parameter and get the result.

Now write the following code on Button click event:

C#

private void btn_Compare_Click(object sender, EventArgs e)

        {

            System.Collections.CaseInsensitiveComparer ob = new System.Collections.CaseInsensitiveComparer();

            String st1 = "DEVASP";

            String st2 = "DEVASP";

            int c = ob.Compare(st1, st2);

            string str = "RESULT IS " + c.ToString();

            if(c==0)

            MessageBox.Show(str+"\nOBJECTS ARE EQUAL");

            else

            MessageBox.Show("OBJECTS ARE NOT EQUAL");

        }

VB

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

        Dim ob As New System.Collections.CaseInsensitiveComparer()

        Dim st1 As [String] = "DEVASP"

        Dim st2 As [String] = "DEVASP"

        Dim c As Integer = ob.Compare(st1, st2)

        Dim str As String = "RESULT IS " & c.ToString()

        If c = 0 Then

            MessageBox.Show(str & vbLf & "OBJECTS ARE EQUAL")

        Else

            MessageBox.Show("OBJECTS ARE NOT EQUAL")

        End If

    End Sub

This simple code checks the equality of two objects.

Greater:

If the values of both objects are not equal or their references are not equal then the compare () function returns none zero value. But this value can be negative or positive. If the values of first parameter object is greater then the second then compare () function returns positive 1.

To demonstrate make a window application. Drag one button on form. Declare two objects of same class pass them in compare function as parameter and get the result.

Now write the following code on Button click event:

C#

private void btn_Compare_Click(object sender, EventArgs e)

        {

            System.Collections.CaseInsensitiveComparer ob = new System.Collections.CaseInsensitiveComparer();

            String st1 = "DEVAS";

            String st2 = "DEVA";

            int c = ob.Compare(st1, st2);

            string str = "RESULT IS " + c.ToString();

            if(c==0)

            MessageBox.Show(str+"\nOBJECTS ARE EQUAL");

            else

            MessageBox.Show(str +"OBJECTS ARE NOT EQUAL");

        }

VB

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

        Dim ob As New System.Collections.CaseInsensitiveComparer()

        Dim st1 As [String] = "DEVAS"

        Dim st2 As [String] = "DEVA"

        Dim c As Integer = ob.Compare(st1, st2)

        Dim str As String = "RESULT IS " & c.ToString()

        If c = 0 Then

            MessageBox.Show(str & vbLf & "OBJECTS ARE EQUAL")

        Else

            MessageBox.Show(str & "OBJECTS ARE NOT EQUAL")

        End If

    End Sub

 

 

Smaller:

If the values of first parameter object is smaller then the second then compare () function returns negative 1.

To demonstrate make a window application. Drag one button on form. Declare two objects of same class pass them in compare function as parameter and get the result.

Now write the following code on Button click event:

C#

private void btn_Compare_Click(object sender, EventArgs e)

        {

            System.Collections.CaseInsensitiveComparer ob = new System.Collections.CaseInsensitiveComparer();

            String st1 = "DEVAS";

            String st2 = "DEVASP";

            int c = ob.Compare(st1, st2);

            string str = "RESULT IS " + c.ToString();

            if(c==0)

            MessageBox.Show(str+"\nOBJECTS ARE EQUAL");

            else

            MessageBox.Show(str +"OBJECTS ARE NOT EQUAL");

        }

VB

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

        Dim ob As New System.Collections.CaseInsensitiveComparer()

        Dim st1 As [String] = "DEVAS"

        Dim st2 As [String] = "DEVASP"

        Dim c As Integer = ob.Compare(st1, st2)

        Dim str As String = "RESULT IS " & c.ToString()

        If c = 0 Then

            MessageBox.Show(str & vbLf & "OBJECTS ARE EQUAL")

        Else

            MessageBox.Show(str & "OBJECTS ARE NOT EQUAL")

        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 Objects Comparison Application";          

        }

VB

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

        Me.Text = " Devasp Objects Comparison Application "

    End Sub

 

This simple article tells that how we can compare two objects of same class using “CaseInsensitiveComparer” class in VB and C# (.net).

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Sound player (play, play loop, stop) in VB and C# (.net 2.0).Part2

Disclaimer - Privacy
© 2002-2017 DevASP.net