How to create global Funtions & Routines

  • Thread starter Thread starter Matthew Hood
  • Start date Start date
M

Matthew Hood

I'm at a loss on how to create global functions for my website.
Essentially, I have several different functions I use throughout my site and
I want to be able to put these in 1 file and call them from anywhere.
Currently, I just copy them into each aspx file... Not what I want to do.

Any help? BTW, I am using WebMatrix to develope the aspx forms.
TIA
-Matt
 
Welcome to object oriented programming. ;o)

Each page in your site is a class. You'll note at the top of your
codebehind file (I'm assuming you have this in webmatrix?) that you specifiy
a class name for your page... e.g. "Public Class myPage"... this "page" is a
public class, so it is available to any part of your application. (Assuming
it exposes the methods you need).

Lets assume you have a function in your codebehind file that you want to
access elsewhere in your web application. You can just create an instance
of the other "page" (class) from within your current codebehind file by
just stating:

dim myTool as new myPage()

where myPage is the class that contains the method you wish to use. For
example if you had a function that added two numbers in your myPage class
called "addIt" you could say:

dim myTool as new myPage()
dim myresult as Integer = myTool.addIt(number1, number2)

What you really need to do is have a class library. (just a file with a .vb
extension) in you application with the classes you wish to implement
globally, that way when you create an instance of the "tool" you need you
aren't using all the extra memory neccessary to create an instance of the
myPage class. (because it inherits from the Page class which has a ton of
methods, etc..)

Hope that helps.

-MC D
 
Webmatrix does not use the Codebehind method.
Initially I created the functions in a .vb file, but when I tried to access
them from a page, I get an error stating that the routines were not defined.
Do the vb files have to be compiled?

TIA-
Matt
 
Back
Top