Returning a Class from a function

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to return a class (not an instance of a class) from a
function? For example (actually tried this code, it didn't work, but it's
what I want sort of):

Class EventMessage
Public Function MessageHandler() as Type
Return GetType(EventMessageHandler)
End FUnction
End Class

EventMessageHandler is all class level methods (shared or static or whatever
your terminology). I'm trying to decouple knowledge of a message handler from
the application. The Message object itself will know what it's handler is.

my application can receive an xml message (any message that matches our
interface even those not yet defined), turn the message into an object in an
abstract fashion, ask the object for its MessageHandler class, then pass the
message to the Handler. This seems straightforward except I can't figure out
how to return a class (and not an object) from a function.

Any one willing to tackle this one? If you understand architecturally what
i'm trying to do and have a better suggestion I'll listen to those as well.

ken
 
As long as EventMessageHandler is a type (name of your class) and it shows
up in intellisense in your IDE, the procedure you've written should work.
You can also use Type.GetType which will give you the type object from the
name of the type. But if you can get GetType to work, that would be better.
What error are you getting when using that procedure? A little more would
help..

Imran.
 
If I have the following 2 classes defined:
Public Class EventMessage
Public Function MessageHandler() As Type
Return GetType(EventMessageHandler)
End Function
'EventMessage properties go here
End Class

Public Class EventMessageHandler
Public Shared Sub HandleMessage(ByVal msg As EventMessage)
'Do something with the EventMessage
End Sub
Public Shared Function TestMethod() As String
Return "It worked!"
End Function
End Class

And I want to test it by calling TestMethod, this doesn't work...
Private Sub MyAppsMethod()
Dim evnt As New EventMessage

'If I want to call TestMethod here, what do I do?
MsgBox(evnt.MessageHandler.TestMessage)
End Sub

Basically I need to be able to call any shared method on
EventMessageHandler...how? (BTW, I get an error saying 'TestMethod is not a
member of System.Type)
 
Ken Foster said:
If I have the following 2 classes defined:
Public Class EventMessage
Public Function MessageHandler() As Type
Return GetType(EventMessageHandler)
End Function
'EventMessage properties go here
End Class

Public Class EventMessageHandler
Public Shared Sub HandleMessage(ByVal msg As EventMessage)
'Do something with the EventMessage
End Sub
Public Shared Function TestMethod() As String
Return "It worked!"
End Function
End Class

And I want to test it by calling TestMethod, this doesn't work...
Private Sub MyAppsMethod()
Dim evnt As New EventMessage

'If I want to call TestMethod here, what do I do?
MsgBox(evnt.MessageHandler.TestMessage)
End Sub

Basically I need to be able to call any shared method on
EventMessageHandler...how? (BTW, I get an error saying 'TestMethod is not
a
member of System.Type)

That's because MessageHandler is returning a Type object and, as the error
message says, TestMethod is not a member of System.Type. To access shared
methods, you need the type identifier; so you won't be able to call the
shared methods directly as you would do when you know the type at compile
time. You'll have to resort to reflection to execute the method. Use the
InvokeMember method to call a method, property, etc from the type object of
a given type.

Private Sub MyAppsMethod()
Dim evnt As New EventMessage
MessageBox.Show(CStr(evnt.MessageHandler.InvokeMember( _
"TestMethod", Reflection.BindingFlags.InvokeMethod Or _
Reflection.BindingFlags.Static Or
Reflection.BindingFlags.Public, _
Nothing, Nothing, New Object() {})))
End Sub

hope that helps..
Imran.
 
That worked, thanks. I even played with it to get it to work with passing an
argument.

I appreciate your help on this.
ken
 
Ken,
In addition to Imran's suggestion of using Reflection to call shared
methods, consider designing with polymorphism & OOP.

Your MessageHandler function returns an instance of an object that has
methods on it that your code calls, the object returned would either inherit
from a base type or implement an interface.

Something like:

Public Interface IEventMessageHandler
Function TestMethod() As String
Sub HandleMessage(ByVal msg As EventMessage)
End Interface

Public Class EventMessage
Public Function MessageHandler() As IEventMessageHandler
Return New EventMessageHandler
End Function
'EventMessage properties go here
End Class

Public Class EventMessageHandler
implements IEventMessageHandler

Public Sub HandleMessage(ByVal msg As EventMessage) Implements
IEventMessageHandler.HandleMessage
'Do something with the EventMessage
End Sub

Public Function TestMethod() As String Implements
IEventMessageHandler.TestMethod
Return "It worked!"
End Function

End Class


Public Shared Sub Main()
Dim evnt As New EventMessage

'If I want to call TestMethod here, what do I do?
MsgBox(evnt.MessageHandler.TestMethod)
End Sub

Alternatively, if there is only a single "method" involved, I would consider
using a Delegate, something like:

Public Delegate Function Method() As String

Public Class EventMessage
Public Function MessageHandler() As Method
Return AddressOf EventMessageHandler.TestMethod
End Function
'EventMessage properties go here
End Class

Public Class EventMessageHandler
Public Shared Sub HandleMessage(ByVal msg As EventMessage)
'Do something with the EventMessage
End Sub
Public Shared Function TestMethod() As String
Return "It worked!"
End Function
End Class


Public Shared Sub Main()
Dim evnt As New EventMessage

'If I want to call TestMethod here, what do I do?
MsgBox(evnt.MessageHandler.Invoke())
End Sub

For a good explanation of how to use OOP (Polymorphism, Inheritance,
Encapsulation, Interfaces) in VB.NET see Robin A. Reynolds-Haertle's book
"OOP - with Microsoft Visual Basic .NET and Microsoft Visual C# .NET - Step
by Step" from MS Press.

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top