Delegate - shared

Is it possible to use instance (inside Dog and Student Class instead of Shared).

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


Option Strict On

Imports System

Namespace DelegatesAndEvents

    Public Enum Comparison
        theFirst = 1
        theSecond = 2


    End Enum

    Public Class Pair

        Private thePair(1) As Object

        Public Delegate Function WhichIsSmaller(ByVal obj1 As Object, ByVal obj2 As Object) _
        As Comparison

        Public Sub New(ByVal firstObject As Object, ByVal secondObject As Object)
            thePair(0) = firstObject
            thePair(1) = secondObject

        End Sub


       

        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 Sub ReverseSort(ByVal theDelegatedFunc As WhichIsSmaller)
            If theDelegatedFunc(thePair(0), thePair(1)) = Comparison.theFirst Then
                Dim temp As Object = thePair(0)
                thePair(0) = thePair(0)
                thePair(1) = temp

            End If
        End Sub

        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 Shared ReadOnly OderDogs As New Pair.WhichIsSmaller(AddressOf Dog.WhichDogIsSmaller)


        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

        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




    Class Tester

        Public Sub run()

            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())


            StudentPair.Sort(Student.OrderStudents)
            Console.WriteLine("After Sort StudentPair: {0}", StudentPair.ToString())

            StudentPair.ReverseSort(Student.OrderStudents)
            Console.WriteLine("After ReverseSort StudentPair : {0}", StudentPair.ToString())

            DogPair.Sort(Dog.OderDogs)
            Console.WriteLine("After sort dogPair :{0}", DogPair.ToString())

            DogPair.ReverseSort(Dog.OderDogs)
            Console.WriteLine("After ReverseSort Dogpari: {0}", DogPair.ToString())

        End Sub

     

        Public Shared Sub main()
            Dim t As New Tester
            t.run()

        End Sub
    End Class



End Namespace
VBdotnet2005Asked:
Who is Participating?
 
RobertRFreemanCommented:
Yes you can.
Just change the prefix from Dog.WhichDogIsSmaller to Me.WhichDogIsSmaller.
Make sure you remove the "Shared" from both the Parameter and Function in each class.

Shared just allows the property or function to be called without instantiating the class.  No "New" required.
0
 
VBdotnet2005Author Commented:
Idle_Mind ?
0
 
Bob LearnedCommented:
Sure, it might be possible, but what are you really trying to do with that code?

Bob
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
VBdotnet2005Author Commented:
I want to see how it can be done using instance instead of Shared. If possible, I want to eliminate Class Tester, by instanciate Delegate inside Dog and Student. I am really new to Delegate and Shared Method.
0
 
VBdotnet2005Author Commented:
TheLearnedOne , I saw you code last week, about Delegate, it was really hard to understand.
0
 
VBdotnet2005Author Commented:
This is where I got the code
http://msdn.microsoft.com/msdnmag/issues/03/02/Delegates/default.aspx
My question is  "Is it possible to use instance (inside Dog and Student Class instead of Shared)?" I don't like Console writeline at all. Please put them in textboxes or lable instead.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.