简体   繁体   中英

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM