Mega Code Archive

 
Categories / VB.Net / Data Types
 

Implements the generic IComparable

Imports System.Collections.Generic Imports System.Numerics Public Structure MapInfo : Implements IComparable(Of MapInfo)    Public Sub New(name As String, lightYears As Double)       Me.Name = name       Me.Distance = CType(Math.Round(lightYears * 5.88e12), BigInteger)    End Sub    Public Sub New(name As String, distance As BigInteger)       Me.Name = name       Me.Distance = distance    End Sub    Public Name As String    Public Distance As BigInteger    Public Overrides Function ToString() As String       Return String.Format("{0,-10} ({1:N0})", Me.Name, Me.Distance)    End Function    Public Function CompareTo(other As MapInfo) As Integer Implements IComparable(Of MapInfo).CompareTo       Return Me.Distance.CompareTo(other.Distance)    End Function                 End Structure Module Example    Public Sub Main()       Dim star As MapInfo       Dim stars As New List(Of MapInfo)       star = New MapInfo("A", 123456.6d)       stars.Add(star)       star = New MapInfo("B", 1d)       stars.Add(star)       star = New MapInfo("C", 31d)       stars.Add(star)       star = New MapInfo("D", 51d)       stars.Add(star)       stars.Sort()       For Each star In stars          Console.WriteLine(star)       Next       End Sub End Module