Note that there are some explanatory texts on larger screens.

plurals
  1. POVB.NET (Of T) Comparison Operator
    text
    copied!<p>In the sample code, the line with the 'error comment' gives the following the error -</p> <ul> <li>Operator '&lt;' is not defined for types 'T' and 'T'.</li> </ul> <p>Why wouldn't VB automatically call the appropriate T operator? (i.e. If T is an integer then call the integer comparison functions.)</p> <p>Is it possible to make this work in an elegant fashion?</p> <p>This is for .NET 2.0.</p> <p><strong>Edit</strong> - Updated code for anyone interested.</p> <pre><code>Public Class TreeNode(Of T) Public Left As TreeNode(Of T) Public Right As TreeNode(Of T) Public Value As IComparable(Of T) Public Sub New(ByVal _value As T) Value = _value End Sub End Class Public Class Tree(Of T) Private _Root As TreeNode(Of T) Public ReadOnly Property Root() Get Return _Root End Get End Property Public Sub New() _Root = Nothing End Sub Public Function Add(ByVal value As IComparable(Of T)) As TreeNode(Of T) If _Root Is Nothing Then _Root = New TreeNode(Of T)(value) Else Dim node As TreeNode(Of T) = _Root While node IsNot Nothing If value.CompareTo(node.Value) &lt; 0 Then If node.Left IsNot Nothing Then node = node.Left Else node.Left = New TreeNode(Of T)(value) Return node.Left End If Else If node.Right IsNot Nothing Then node = node.Right Else node.Right = New TreeNode(Of T)(value) Return node.Right End If End If End While End If Return _Root End Function Public Sub Print(ByVal node As TreeNode(Of T)) If node IsNot Nothing Then Print(node.Left) Console.WriteLine(node.Value) Print(node.Right) End If End Sub End Class </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload