We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Delegate  - shared

VBdotnet2005
VBdotnet2005 asked
on
Medium Priority
220 Views
Last Modified: 2010-04-23
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
Comment
Watch Question

Author

Commented:
Idle_Mind ?
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

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

Bob

Author

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.

Author

Commented:
TheLearnedOne , I saw you code last week, about Delegate, it was really hard to understand.

Author

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.
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.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.