?
Solved

Delegate

Posted on 2006-03-21
10
Medium Priority
?
315 Views
Last Modified: 2010-04-23
How can I display it in textboxes or lables instead of in Console. I got this sampe from
http://msdn.microsoft.com/msdnmag/issues/03/02/Delegates/default.aspx 



Option Strict On

Imports System

Namespace DelegatesAndEvents

    ' a simple collection to hold two items
    Public Class Pair

        ' enumeration to indicate which value is smaller
        Public Enum comparison
            theFirst = 1
            theSecond = 2
        End Enum

        ' private array to hold the two objects
        Private thePair(2) As Object

        ' the delegate declaration
        Public Delegate Function WhichIsSmaller( _
          ByVal obj1 As Object, ByVal obj2 As Object) As comparison

        ' constructor take two objects,
        ' added to array in order received
        Public Sub New( _
           ByVal firstObject As Object, _
           ByVal secondObject As Object)
            thePair(0) = firstObject
            thePair(1) = secondObject
        End Sub

        ' public method that orders the two objects
        ' by whatever criteria the objects like
        Public Sub Sort(ByVal theDelegatedFunc As WhichIsSmaller)
            If theDelegatedFunc(thePair(0), thePair(1)) = _
               comparison.theSecond Then
                Dim temp As Object = thePair(0)
                thePair(0) = thePair(1)
                thePair(1) = temp
            End If
        End Sub

        ' public method that orders the two objects
        ' by the reverse of whatever criteria the objects like
        Public Sub ReverseSort(ByVal theDelegatedFunc As WhichIsSmaller)
            If theDelegatedFunc(thePair(0), thePair(1)) = _  
                    comparison.theFirst Then
                Dim temp As Object = thePair(0)
                thePair(0) = thePair(1)
                thePair(1) = temp
            End If
        End Sub

        ' ask the two objects to give their string value
        Public Overrides Function ToString() As String
            Return thePair(0).ToString() & ", " & thePair(1).ToString()
        End Function
    End Class

    Public Class Dog

        Private weight As Integer

        Public Sub New(ByVal weight As Integer)
            Me.weight = weight
        End Sub

        ' dogs are ordered by weight
        Public Shared Function WhichDogIsSmaller( _
          ByVal o1 As Object, ByVal o2 As Object) As Pair.comparison
            Dim d1 As Dog = DirectCast(o1, Dog)
            Dim d2 As Dog = DirectCast(o2, Dog)
            If d1.weight > d2.weight Then
                Return Pair.comparison.theSecond
            Else
                Return Pair.comparison.theFirst
            End If
        End Function

        Public Overrides Function ToString() As String
            Return weight.ToString()
        End Function
    End Class

    Public Class Student

        Private name As String

        Public Sub New(ByVal name As String)
            Me.name = name
        End Sub

        ' students are ordered alphabetically
        Public Shared Function WhichStudentIsSmaller( _
          ByVal o1 As Object, ByVal o2 As Object) As Pair.comparison
            Dim s1 As Student = DirectCast(o1, Student)
            Dim s2 As Student = DirectCast(o2, Student)
            If String.Compare(s1.name, s2.name) < 0 Then
                Return Pair.comparison.theFirst
            Else
                Return Pair.comparison.theSecond
            End If
        End Function

        Public Overrides Function ToString() As String
            Return name
        End Function
    End Class

    Class Tester

        Public Sub Run()
            ' create two students and two dogs
            ' and add them to Pair objects
            Dim Jesse As New Student("Jesse")
            Dim Stacey As New Student("Stacey")
            Dim Milo As New Dog(65)
            Dim Fred As New Dog(12)

            Dim studentPair As New Pair(Jesse, Stacey)
            Dim dogPair As New Pair(Milo, Fred)
            Console.WriteLine("studentPair: {0}", _
                studentPair.ToString())
            Console.WriteLine("dogPair: {0}", _
                dogPair.ToString())

            ' Instantiate  the delegates
            Dim theStudentDelegate As New _
              Pair.WhichIsSmaller(AddressOf Student.WhichStudentIsSmaller)
            Dim theDogDelegate As New _
              Pair.WhichIsSmaller(AddressOf Dog.WhichDogIsSmaller)

            ' sort using the delegates
            studentPair.Sort(theStudentDelegate)
            Console.WriteLine("After Sort studentPair: {0}", _
                studentPair.ToString())
            studentPair.ReverseSort(theStudentDelegate)
            Console.WriteLine("After ReverseSort studentPair: {0}", _
                studentPair.ToString())

            dogPair.Sort(theDogDelegate)
            Console.WriteLine("After Sort dogPair: {0}", _
                dogPair.ToString())
            dogPair.ReverseSort(theDogDelegate)
            Console.WriteLine("After ReverseSort dogPair: {0}", _
                dogPair.ToString())
        End Sub

        Public Shared Sub Main()
            Dim t As New Tester()
            t.Run()
        End Sub
    End Class
End Namespace
0
Comment
Question by:VBdotnet2005
  • 5
  • 5
10 Comments
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16249886
Hi VBdotnet2005;

The following are three different files. A Windows Form File to display the results, a Global module to hold the reference to the Windows Form and a modified Classes you posted which will display the info into text boxes.

'==========================================
' Form1 Start
'==========================================

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents txtStudentBefore As System.Windows.Forms.TextBox
    Friend WithEvents txtDogBefore As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtStudentAfter As System.Windows.Forms.TextBox
    Friend WithEvents txtDogAfter As System.Windows.Forms.TextBox
    Friend WithEvents txtDogResort As System.Windows.Forms.TextBox
    Friend WithEvents txtStudentResort As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.txtStudentBefore = New System.Windows.Forms.TextBox
Me.txtDogBefore = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.txtStudentAfter = New System.Windows.Forms.TextBox
Me.txtDogAfter = New System.Windows.Forms.TextBox
Me.txtDogResort = New System.Windows.Forms.TextBox
Me.txtStudentResort = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 224)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'txtStudentBefore
'
Me.txtStudentBefore.Location = New System.Drawing.Point(16, 40)
Me.txtStudentBefore.Name = "txtStudentBefore"
Me.txtStudentBefore.TabIndex = 1
Me.txtStudentBefore.Text = ""
'
'txtDogBefore
'
Me.txtDogBefore.Location = New System.Drawing.Point(144, 40)
Me.txtDogBefore.Name = "txtDogBefore"
Me.txtDogBefore.TabIndex = 2
Me.txtDogBefore.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(240, 23)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Student              Before      Dog"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 80)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(240, 23)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Student                After         Dog"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'txtStudentAfter
'
Me.txtStudentAfter.Location = New System.Drawing.Point(16, 112)
Me.txtStudentAfter.Name = "txtStudentAfter"
Me.txtStudentAfter.TabIndex = 5
Me.txtStudentAfter.Text = ""
'
'txtDogAfter
'
Me.txtDogAfter.Location = New System.Drawing.Point(144, 112)
Me.txtDogAfter.Name = "txtDogAfter"
Me.txtDogAfter.TabIndex = 6
Me.txtDogAfter.Text = ""
'
'txtDogResort
'
Me.txtDogResort.Location = New System.Drawing.Point(144, 184)
Me.txtDogResort.Name = "txtDogResort"
Me.txtDogResort.TabIndex = 9
Me.txtDogResort.Text = ""
'
'txtStudentResort
'
Me.txtStudentResort.Location = New System.Drawing.Point(16, 184)
Me.txtStudentResort.Name = "txtStudentResort"
Me.txtStudentResort.TabIndex = 8
Me.txtStudentResort.Text = ""
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(16, 152)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(240, 23)
Me.Label3.TabIndex = 7
Me.Label3.Text = "Student             After Resort         Dog"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 270)
Me.Controls.Add(Me.txtDogResort)
Me.Controls.Add(Me.txtStudentResort)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.txtDogAfter)
Me.Controls.Add(Me.txtStudentAfter)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtDogBefore)
Me.Controls.Add(Me.txtStudentBefore)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        frm1 = Me

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click

        Dim TestRun As New DelegatesAndEvents.Tester

        TestRun.Run()


    End Sub
End Class

'==========================================
' Form1 End
'==========================================

'==========================================
' Global Module Start
'==========================================

Module Global

    Public frm1 As Form1

End Module

'==========================================
' Global Module End
'==========================================

'==========================================
' Classes Modified Start
'==========================================

Option Strict On

Imports System

Namespace DelegatesAndEvents

    ' a simple collection to hold two items
    Public Class Pair

        ' enumeration to indicate which value is smaller
        Public Enum comparison
            theFirst = 1
            theSecond = 2
        End Enum

        ' private array to hold the two objects
        Private thePair(2) As Object

        ' the delegate declaration
        Public Delegate Function WhichIsSmaller( _
          ByVal obj1 As Object, ByVal obj2 As Object) As comparison

        ' constructor take two objects,
        ' added to array in order received
        Public Sub New( _
           ByVal firstObject As Object, _
           ByVal secondObject As Object)
            thePair(0) = firstObject
            thePair(1) = secondObject
        End Sub

        ' public method that orders the two objects
        ' by whatever criteria the objects like
        Public Sub Sort(ByVal theDelegatedFunc As WhichIsSmaller)
            If theDelegatedFunc(thePair(0), thePair(1)) = _
               comparison.theSecond Then
                Dim temp As Object = thePair(0)
                thePair(0) = thePair(1)
                thePair(1) = temp
            End If
        End Sub

        ' public method that orders the two objects
        ' by the reverse of whatever criteria the objects like
        Public Sub ReverseSort(ByVal theDelegatedFunc As WhichIsSmaller)
            If theDelegatedFunc(thePair(0), thePair(1)) = _
                    comparison.theFirst Then
                Dim temp As Object = thePair(0)
                thePair(0) = thePair(1)
                thePair(1) = temp
            End If
        End Sub

        ' ask the two objects to give their string value
        Public Overrides Function ToString() As String
            Return thePair(0).ToString() & ", " & thePair(1).ToString()
        End Function
    End Class

    Public Class Dog

        Private weight As Integer

        Public Sub New(ByVal weight As Integer)
            Me.weight = weight
        End Sub

        ' dogs are ordered by weight
        Public Shared Function WhichDogIsSmaller( _
          ByVal o1 As Object, ByVal o2 As Object) As Pair.comparison
            Dim d1 As Dog = DirectCast(o1, Dog)
            Dim d2 As Dog = DirectCast(o2, Dog)
            If d1.weight > d2.weight Then
                Return Pair.comparison.theSecond
            Else
                Return Pair.comparison.theFirst
            End If
        End Function

        Public Overrides Function ToString() As String
            Return weight.ToString()
        End Function
    End Class

    Public Class Student

        Private name As String

        Public Sub New(ByVal name As String)
            Me.name = name
        End Sub

        ' students are ordered alphabetically
        Public Shared Function WhichStudentIsSmaller( _
          ByVal o1 As Object, ByVal o2 As Object) As Pair.comparison
            Dim s1 As Student = DirectCast(o1, Student)
            Dim s2 As Student = DirectCast(o2, Student)
            If String.Compare(s1.name, s2.name) < 0 Then
                Return Pair.comparison.theFirst
            Else
                Return Pair.comparison.theSecond
            End If
        End Function

        Public Overrides Function ToString() As String
            Return name
        End Function
    End Class

    Class Tester

        Public Sub Run()
            ' create two students and two dogs
            ' and add them to Pair objects
            Dim Jesse As New Student("Jesse")
            Dim Stacey As New Student("Stacey")
            Dim Milo As New Dog(65)
            Dim Fred As New Dog(12)

            Dim studentPair As New Pair(Jesse, Stacey)
            Dim dogPair As New Pair(Milo, Fred)
            frm1.txtStudentBefore.Text = studentPair.ToString()
            frm1.txtDogBefore.Text = dogPair.ToString()

            ' Instantiate  the delegates
            Dim theStudentDelegate As New _
              Pair.WhichIsSmaller(AddressOf Student.WhichStudentIsSmaller)
            Dim theDogDelegate As New _
              Pair.WhichIsSmaller(AddressOf Dog.WhichDogIsSmaller)

            ' sort using the delegates
            studentPair.Sort(theStudentDelegate)
            frm1.txtStudentAfter.Text = studentPair.ToString()
            studentPair.ReverseSort(theStudentDelegate)
            frm1.txtStudentResort.Text = studentPair.ToString()

            dogPair.Sort(theDogDelegate)
            frm1.txtDogAfter.Text = dogPair.ToString()
            dogPair.ReverseSort(theDogDelegate)
            frm1.txtDogResort.Text = dogPair.ToString()
        End Sub

    End Class
End Namespace

'==========================================
' Classes Modified End
'==========================================

I hope that this was of some help.

Fernando
0
 

Author Comment

by:VBdotnet2005
ID: 16250396
Can I instantiate the delegates inside Class Dog and Class Student? Would that eliminate Class Tester?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16250465
Class Tester is just testing the other classes so what is your objective / what do you want to do?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:VBdotnet2005
ID: 16251841
I want to  instantiate the delegates inside Class Dog and Class Student.
0
 

Author Comment

by:VBdotnet2005
ID: 16251893



Public Class Dog

        Private weight As Integer

        Public Shared ReadOnly OderDogs As New Pair.WhichIsSmaller(AddressOf Dog.WhichDogIsSmaller)  --------------------------------------like this :


        Public Sub New(ByVal weight As Integer)
            Me.weight = weight
        End Sub


        Public Shared Function WhichDogIsSmaller(ByVal o1 As Object, ByVal o2 As Object) As Comparison

            Dim D1 As Dog = DirectCast(o1, Dog)
            Dim D2 As Dog = DirectCast(o2, Dog)

            If D1.weight > D2.weight Then
                Return Comparison.theSecond
            Else
                Return Comparison.theFirst

            End If
        End Function



        Public Overrides Function Tostring() As String

            Return weight.ToString

        End Function



    End Class

    Public Class Student


        Private name As String

        Dim theStudentDelegate As New Pair.WhichIsSmaller(AddressOf Student.WhichStudentIsSmaller)  --------------------------------------like this :

        Public Sub New(ByVal name As String)
            Me.name = name


        End Sub

        Public Shared ReadOnly OrderStudents As New Pair.WhichIsSmaller(AddressOf Student.WhichStudentIsSmaller)




        Public Shared Function WhichStudentIsSmaller(ByVal o1 As Object, ByVal o2 As Object) As Comparison


            Dim s1 As Student = DirectCast(o1, Student)
            Dim s2 As Student = DirectCast(o2, Student)

            If String.Compare(s1.name, s2.name) < 0 Then
                Return Comparison.theSecond
            Else
                Return Comparison.theFirst

            End If
        End Function

        Public Overrides Function Tostring() As String

            Return name

        End Function
    End Class
0
 
LVL 64

Accepted Solution

by:
Fernando Soto earned 2000 total points
ID: 16252466
This now defines the Delegates inside of Student and Dog Classes. I included the Form11 code as well so you can see how to access Pair with the changes.

'==========================================
' Classes Modified Start
'==========================================

Option Strict On

Imports System

Namespace DelegatesAndEvents

    ' a simple collection to hold two items
    Public Class Pair

        ' enumeration to indicate which value is smaller
        Public Enum comparison
            theFirst = 1
            theSecond = 2
        End Enum

        ' private array to hold the two objects
        Private thePair(2) As Object

        ' the delegate declaration
        Public Delegate Function WhichIsSmaller( _
          ByVal obj1 As Object, ByVal obj2 As Object) As comparison

        ' constructor take two objects,
        ' added to array in order received
        Public Sub New( _
           ByVal firstObject As Object, _
           ByVal secondObject As Object)
            thePair(0) = firstObject
            thePair(1) = secondObject
        End Sub

        ' public method that orders the two objects
        ' by whatever criteria the objects like
        Public Sub Sort(ByVal theDelegatedFunc As WhichIsSmaller)
            If theDelegatedFunc(thePair(0), thePair(1)) = _
               comparison.theSecond Then
                Dim temp As Object = thePair(0)
                thePair(0) = thePair(1)
                thePair(1) = temp
            End If
        End Sub

        ' public method that orders the two objects
        ' by the reverse of whatever criteria the objects like
        Public Sub ReverseSort(ByVal theDelegatedFunc As WhichIsSmaller)
            If theDelegatedFunc(thePair(0), thePair(1)) = _
                    comparison.theFirst Then
                Dim temp As Object = thePair(0)
                thePair(0) = thePair(1)
                thePair(1) = temp
            End If
        End Sub

        ' ask the two objects to give their string value
        Public Overrides Function ToString() As String
            Return thePair(0).ToString() & ", " & thePair(1).ToString()
        End Function
    End Class

    Public Class Dog

        Private weight As Integer
        Public theDogDelegate As New _
            Pair.WhichIsSmaller(AddressOf _
            WhichDogIsSmaller)

        Public Sub New(ByVal weight As Integer)
            Me.weight = weight
        End Sub

        ' dogs are ordered by weight
        Public Shared Function WhichDogIsSmaller( _
          ByVal o1 As Object, ByVal o2 As Object) As Pair.comparison
            Dim d1 As Dog = DirectCast(o1, Dog)
            Dim d2 As Dog = DirectCast(o2, Dog)
            If d1.weight > d2.weight Then
                Return Pair.comparison.theSecond
            Else
                Return Pair.comparison.theFirst
            End If
        End Function

        Public Overrides Function ToString() As String
            Return weight.ToString()
        End Function
    End Class

    Public Class Student

        Private name As String
        Public theStudentDelegate As New _
            Pair.WhichIsSmaller(AddressOf _
            WhichStudentIsSmaller)

        Public Sub New(ByVal name As String)
            Me.name = name
        End Sub

        ' students are ordered alphabetically
        Public Shared Function WhichStudentIsSmaller( _
          ByVal o1 As Object, ByVal o2 As Object) As Pair.comparison
            Dim s1 As Student = DirectCast(o1, Student)
            Dim s2 As Student = DirectCast(o2, Student)
            If String.Compare(s1.name, s2.name) < 0 Then
                Return Pair.comparison.theFirst
            Else
                Return Pair.comparison.theSecond
            End If
        End Function

        Public Overrides Function ToString() As String
            Return name
        End Function
    End Class

End Namespace

'==========================================
' Classes Modified End
'==========================================

'==========================================
' Form1 Start
'==========================================

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    Friend WithEvents txtStudentBefore As System.Windows.Forms.TextBox
    Friend WithEvents txtDogBefore As System.Windows.Forms.TextBox
    Friend WithEvents Label1 As System.Windows.Forms.Label
    Friend WithEvents Label2 As System.Windows.Forms.Label
    Friend WithEvents txtStudentAfter As System.Windows.Forms.TextBox
    Friend WithEvents txtDogAfter As System.Windows.Forms.TextBox
    Friend WithEvents txtDogResort As System.Windows.Forms.TextBox
    Friend WithEvents txtStudentResort As System.Windows.Forms.TextBox
    Friend WithEvents Label3 As System.Windows.Forms.Label
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.txtStudentBefore = New System.Windows.Forms.TextBox
Me.txtDogBefore = New System.Windows.Forms.TextBox
Me.Label1 = New System.Windows.Forms.Label
Me.Label2 = New System.Windows.Forms.Label
Me.txtStudentAfter = New System.Windows.Forms.TextBox
Me.txtDogAfter = New System.Windows.Forms.TextBox
Me.txtDogResort = New System.Windows.Forms.TextBox
Me.txtStudentResort = New System.Windows.Forms.TextBox
Me.Label3 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(80, 224)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 0
Me.Button1.Text = "Button1"
'
'txtStudentBefore
'
Me.txtStudentBefore.Location = New System.Drawing.Point(16, 40)
Me.txtStudentBefore.Name = "txtStudentBefore"
Me.txtStudentBefore.TabIndex = 1
Me.txtStudentBefore.Text = ""
'
'txtDogBefore
'
Me.txtDogBefore.Location = New System.Drawing.Point(144, 40)
Me.txtDogBefore.Name = "txtDogBefore"
Me.txtDogBefore.TabIndex = 2
Me.txtDogBefore.Text = ""
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(24, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(240, 23)
Me.Label1.TabIndex = 3
Me.Label1.Text = "Student              Before      Dog"
Me.Label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(16, 80)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(240, 23)
Me.Label2.TabIndex = 4
Me.Label2.Text = "Student                After         Dog"
Me.Label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'txtStudentAfter
'
Me.txtStudentAfter.Location = New System.Drawing.Point(16, 112)
Me.txtStudentAfter.Name = "txtStudentAfter"
Me.txtStudentAfter.TabIndex = 5
Me.txtStudentAfter.Text = ""
'
'txtDogAfter
'
Me.txtDogAfter.Location = New System.Drawing.Point(144, 112)
Me.txtDogAfter.Name = "txtDogAfter"
Me.txtDogAfter.TabIndex = 6
Me.txtDogAfter.Text = ""
'
'txtDogResort
'
Me.txtDogResort.Location = New System.Drawing.Point(144, 184)
Me.txtDogResort.Name = "txtDogResort"
Me.txtDogResort.TabIndex = 9
Me.txtDogResort.Text = ""
'
'txtStudentResort
'
Me.txtStudentResort.Location = New System.Drawing.Point(16, 184)
Me.txtStudentResort.Name = "txtStudentResort"
Me.txtStudentResort.TabIndex = 8
Me.txtStudentResort.Text = ""
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(16, 152)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(240, 23)
Me.Label3.TabIndex = 7
Me.Label3.Text = "Student             After Resort         Dog"
Me.Label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 270)
Me.Controls.Add(Me.txtDogResort)
Me.Controls.Add(Me.txtStudentResort)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.txtDogAfter)
Me.Controls.Add(Me.txtStudentAfter)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.Label1)
Me.Controls.Add(Me.txtDogBefore)
Me.Controls.Add(Me.txtStudentBefore)
Me.Controls.Add(Me.Button1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Form1_Load(ByVal sender As Object, _
        ByVal e As System.EventArgs) Handles MyBase.Load

        frm1 = Me

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click


            ' create two students and two dogs
            ' and add them to Pair objects
            Dim Jesse As New DelegatesAndEvents.Student("Jesse")
            Dim Stacey As New DelegatesAndEvents.Student("Stacey")
            Dim Milo As New DelegatesAndEvents.Dog(65)
            Dim Fred As New DelegatesAndEvents.Dog(12)

            Dim studentPair As New DelegatesAndEvents.Pair(Jesse, Stacey)
            Dim dogPair As New DelegatesAndEvents.Pair(Milo, Fred)
            txtStudentBefore.Text = studentPair.ToString()
            txtDogBefore.Text = dogPair.ToString()

            ' sort using the delegates
            studentPair.Sort(Jesse.theStudentDelegate)
            txtStudentAfter.Text = studentPair.ToString()
            studentPair.ReverseSort(Jesse.theStudentDelegate)
            txtStudentResort.Text = studentPair.ToString()

            dogPair.Sort(Milo.theDogDelegate)
            txtDogAfter.Text = dogPair.ToString()
            dogPair.ReverseSort(Milo.theDogDelegate)
            txtDogResort.Text = dogPair.ToString()


    End Sub
End Class

'==========================================
' Form1 End
'==========================================

Fernando
0
 

Author Comment

by:VBdotnet2005
ID: 16259526
What is I want to display Jesse and Stacey in two seperate two textboxes to display the result(instead of one textbox). Then I need to remove " Public Overrides Function ToString() As String
            Return name
        End Function "  and use Public Property instead to return each value?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16260018
In answer to your last post, "Then I need to remove Public Overrides Function ToString", I would not remove it I would just leave it there and just add the public properties.

Or you could still use the ToString function in this way:

            Dim students() As String = studentPair.ToString().Split(","c)
            Dim dogs() As String = dogPair.ToString().Split(","c)

students(0) will hold the first of the two names and students(1) will hold the second and like wise for dogs.

--------------------------------------------------------------------

The original question was "How can I display it in textboxes or lables instead of in Console." That question has been answered. Then you have asked the question "Can I instantiate the delegates inside Class Dog and Class Student? Would that eliminate Class Tester?" and I answered that. Todays question is "What is I want to display Jesse and Stacey in two seperate two textboxes to display the result(instead of one textbox)." and I have answered that above. In EE guidelines it states the following "The idea at Experts Exchange is to ask a question, assign a point value to it, get an answer from several possible answers, select the best one, and award the points for it. Frequently, new users will ask several questions in one (because they don't have very many points), and then withhold awarding the points until one or more Experts have answered a few follow-up questions -- sometimes totally unrelated to the original question.

This will be met with disfavor by Experts, and will likely get you a warning from the Moderators. Let's keep it simple and to the point: You're better off asking one question at a time for few points than you are asking one question that has five questions plus a couple of follow-ups."

Thank you for your understanding.

Fernando
0
 

Author Comment

by:VBdotnet2005
ID: 16260232
I was not aware of this rule. My bad. However, when you are totally awared of the rule, you should have said something earlier. No warning is needed. I would have post a different question. I know it a long sample program. I wanted to see how flexible the language is. I did not want to post it again and again. Anyway, I really appreciated your help... and your patient  :)
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 16260417
Hi VBdotnet2005;

I had no problem answering the questions you had. I noticed that you are fairly new to EE and so I just wanted to make you aware of the rules. I hope that no offense was taken.

You have a great day;

Fernando
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month14 days, 23 hours left to enroll

840 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question