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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VBdotnet2005Author Commented:
Idle_Mind ?
Bob LearnedCommented:
Sure, it might be possible, but what are you really trying to do with that code?

Bob
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.
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

VBdotnet2005Author Commented:
TheLearnedOne , I saw you code last week, about Delegate, it was really hard to understand.
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.
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.