?
Solved

Delegate  - shared

Posted on 2006-03-20
6
Medium Priority
?
191 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
0
Comment
Question by:VBdotnet2005
  • 4
6 Comments
 

Author Comment

by:VBdotnet2005
ID: 16242622
Idle_Mind ?
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16243204
Sure, it might be possible, but what are you really trying to do with that code?

Bob
0
 

Author Comment

by:VBdotnet2005
ID: 16243326
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
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: 16243339
TheLearnedOne , I saw you code last week, about Delegate, it was really hard to understand.
0
 

Author Comment

by:VBdotnet2005
ID: 16244252
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
 
LVL 16

Accepted Solution

by:
RobertRFreeman earned 2000 total points
ID: 16253812
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

807 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