Publicly accessible functions
I'm creating an httphandler. The file type is probably irrelevant.
My .ashx file:
<%@ webhandler class="MyHandler" %>
<%@ assembly src="Functions.vb" %>
<%@ assembly src="Classes.vb" %>
Public Class MyHandler
...
End Class
My Functions.vb file:
Module PublicFunctions
Function SayHello(greeting As String)
...
End Function
...
End Module
My Classes.vb file:
开发者_如何转开发Imports This
Imports That
Imports TheOther
...
Enum AnEnum
End Enum
...
Class AClass
End Class
...
In my classes file, I'd like to be able to use the functions in Functions.vb without wrapping them in a class so I can call them directly like
SayHello("Hello World")
instead of
MyPublicFunctions.SayHello("Hello World")
Is it possible?
This is a fundamental idea of object oriented programming. Every function, every bit of code belongs somewhere and needs to be organized. One way to do this is through the use of objects (classes). If you have a function you would like to preform, conciser what that function is doing and what is using it, and place it in the appropriate place.
I would also suggest not implementing an HttpHandler. In the Asp.Net framework, just about anything you would ever want a web app to do is already handled by something somewhere. I've been on a lot of web projects, and every time I see someone trying to write one, their is always another (usually out of the box) way to do it.
You could turn "SayHello" into an extension method with the type of the handler as the parameter type but you'll probably still have to specify Me.SayHello()
.
Does
Imports PublicFunctions
help at all?
EDIT - since that didn't work, try converting PublicFunctions to a class, make its members Shared (so that they work without an instance of the class, the equivalent of c# static), and do the import. This is working for me, although I am not in an ASP environment.
精彩评论