TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET IComparable Example

Implement the IComparable generic interface. Use the List Sort subroutine.
IComparable. Two objects are not equal. But how can we compare them? With the IComparable interface, we implement CompareTo and return an Integer. This indicates which object comes before the other.Integer
Example. We introduce a class called Box. We specify that this class Implements IComparable and we specify Box as the generic type parameter. I added a New method, which initializes the size field.

Size: The Size property is also shown. It has public Get and Set parts. We use Size to sort the Box objects.

CompareTo: Next we implement CompareTo. Please notice its syntax. I added an underscore to break the lines.

And: In CompareTo, we simply use the Size property of the Box object to return an Integer value.

Result: The value one means more than (after). The number zero means equal. And minus one means less than (before).

VB.NET program that uses IComparable, CompareTo Class Box Implements IComparable(Of Box) Public Sub New(ByVal size As Integer) sizeValue = size End Sub Private sizeValue As Integer Public Property Size() As Integer Get Return sizeValue End Get Set(ByVal value As Integer) sizeValue = value End Set End Property Public Function CompareTo(other As Box) As Integer _ Implements IComparable(Of Box).CompareTo ' Compare sizes. Return Me.Size().CompareTo(other.Size()) End Function End Class Module Module1 Sub Main() ' Create list of Box objects. Dim boxes As List(Of Box) = New List(Of Box) boxes.Add(New Box(100)) boxes.Add(New Box(90)) boxes.Add(New Box(110)) boxes.Add(New Box(80)) ' Sort with IComparable implementation. boxes.Sort() For Each element As Box In boxes Console.WriteLine(element.Size()) Next End Sub End Module Output 80 90 100 110
In Main, we create a List with four Box objects in it. We use the New subroutine (a constructor) to specify the sizes of the boxes in the creation expression. We then invoke the Sort Sub.ListSub

And: Sort internally calls the CompareTo method we implemented. It operates upon the IComparable interface.

Interface

Thus: The Box class can be sorted through its IComparable interface. The Sort method itself has no special knowledge of the Box.

In the results, our Box objects are sorted by size, from lowest to highest. This is called an ascending sort. For a descending sort, try reversing the order you compare variables in the CompareTo function.
Summary. Some classes are not often sorted. But for classes that are stored in groups, as in collections like Lists, consider adding the IComparable interface. This makes a lambda expression unneeded when sorting the objects.Lambda
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf