By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,573 Members | 1,812 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,573 IT Pros & Developers. It's quick & easy.

Returning a Class from a function

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

"Ken Foster" <Ke*******@discussions.microsoft.com> wrote in message
news:B4**********************************@microsof t.com...
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

Nov 21 '05 #2

P: n/a
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)

"Imran Koradia" wrote:
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.

Nov 21 '05 #3

P: n/a

"Ken Foster" <Ke*******@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.com...
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.InvokeMem ber( _
"TestMethod", Reflection.BindingFlags.InvokeMethod Or _
Reflection.BindingFlags.Static Or
Reflection.BindingFlags.Public, _
Nothing, Nothing, New Object() {})))
End Sub

hope that helps..
Imran.
Nov 21 '05 #4

P: n/a

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

I appreciate your help on this.
ken

"Imran Koradia" wrote:

"Ken Foster" <Ke*******@discussions.microsoft.com> wrote in message
news:26**********************************@microsof t.com...
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.InvokeMem ber( _
"TestMethod", Reflection.BindingFlags.InvokeMethod Or _
Reflection.BindingFlags.Static Or
Reflection.BindingFlags.Public, _
Nothing, Nothing, New Object() {})))
End Sub

hope that helps..
Imran.

Nov 21 '05 #5

P: n/a
> That worked, thanks. I even played with it to get it to work with passing
an
argument.

I appreciate your help on this.


glad to help :)

Nov 21 '05 #6

P: n/a
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

"Ken Foster" <Ke*******@discussions.microsoft.com> wrote in message
news:B4**********************************@microsof t.com...
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

Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.