RE: Reuse code on multiple pages of project
- From: Tom.PesterDELETETHISSS@xxxxxxxxxx
- Date: Thu, 23 Jun 2005 12:07:51 -0700
In one of the previous posts I mentioned a link to 4guys. Thats a good practical start. I thnk there is lots of good content on the internet about OO.
Try to read as much as you can about OO cause it takes a while to realy start thinking OO.
Here you will find some other great resources : http://groups-beta.google.com/group/microsoft.public.dotnet.framework.aspnet/browse_frm/thread/425ac63cf104c958/9c3d1da01c29884a?q=ASP.NET+2.0+reading+material...&rnum=1&hl=en#9c3d1da01c29884a
I can recommend the books of Jesse Liberty : http://www.libertyassociates.com/pages/Books.htm
Learning Visual Basic.NET seems a good title after which you can switch to Programming ASP.NET 2nd Edition.
Try to look into the book before buying it cause I may be wrong. I dont know what you exactly need.
I cant see your source but I think you didnt declare the function public. If you didnt specify and access modifier it defaults to private.
That's OO lingo again...
I wrote a small sample with version 2 of asp.net (switch to it if you can) but it should work in VS 2003
==================== Start of file MyLibrary.vb ===========================
Namespace CommonFunctions
Public Class MyLibrary
' Make the sub public so you can call the sub if you want to call it from the outside
' Make it shared so you dont have to instantiate the class to an object
Public Shared Sub Hello()
HttpContext.Current.Response.Write("Hi there")
End Sub
End Class
End Namespace ==================== End of file MyLibrary.vb =========================== ==================== Start of file consume.aspx ===========================
<%@ Page Language="VB" %> <%@Import Namespace="CommonFunctions" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<script runat="server">
</script>
<html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <% MyLibrary.Hello() %> </div> </form> </body> </html> ==================== End of file consume.aspx ===========================
I didnt use a code behind file to keep things simple.
Cheers, Tom Pester
Hi Tom -
Thanks again for responding. Okay. The error is cause by an OO issue.
What do I put in my vb code behind page exactly? I tried using RFELIB.SetFocus(txtFirstName) and when I type the dot after RFELIB I get "Equals" and "ReferenceEquals" as choices.
Do you know any good books on this subject? I have eight books and out of those eight, although they discuss classes and inheritance, etc., all they give are car and cat/dog/animal examples which never connect up to using an instance on a page.
"Tom.PesterDELETETHISSS@xxxxxxxxxx" wrote:
Hi Sandy,
What I don't understand about it is, for instance, he has <%@ Import Namespace="ASPNet101" %> <%@ Assembly src="LIB.vb" %> Am I supposed to put that in the HTML page, or put the Imports in the vb code behind page instead . . . or both?You just need to tell the compiler where the functions are located in the page you are using the common functions. If you place the class file in the bin directory than asp.net will automaticly compile that class each time the application runs and make it available to the whole app. So <%@ Assembly src="LIB.vb" %> isn't striclty necessary unless you want to reference code thats in a directory outside the bin. <%@ Import Namespace="ASPNet101" %> is necessary cause it tells the compiler where to look if you write a function name. You are saying with that statement that you want to make the classes available to you in that page.If I can possibly impose on you further, could you peruse my code again and see where I made a mistake? What is causing the "squiggly" error?
The error is caused by and OO issue. Remember that asp.net is OO based so it supports inheritance, encapsulation,etc. When you say that a method is shared, like you do with SetFocus, you make it so that the user of that function doesnt have to instantiate the class to an object to use the function (again this is OO lingo which you got to get familiar with). But RegisterStartupScript is not a shared function so it needs to be coupled to an object.
If you remove the shared keyword from SetFocus than the error will be gone.
I know why you put the shared keyword there cause you want to call common functions as if they were baked in the runtime and not create an object firs. But I set you on the wrong foot with my first reply so abondon Scotts article and try to grasp the asp101. I bet this is what you want.
So to sum it up : Write a class file (.vb) and put it in the bin directory. Put your class in a namespace and make all its functions shared. In the page that you want to use the functions use the import namespace directive. Now you can use the functions like this : classX.functionX
Let me know if you have any more questions.. but I suggest you read everything you can about asp.net and its OO features cause I can't write good tutorials :)
Cheers, Tom Pester
.
- Follow-Ups:
- RE: Reuse code on multiple pages of project
- From: Sandy
- RE: Reuse code on multiple pages of project
- References:
- RE: Reuse code on multiple pages of project
- From: Sandy
- RE: Reuse code on multiple pages of project
- Prev by Date: AllowPaging not working
- Next by Date: Re: AllowPaging not working
- Previous by thread: RE: Reuse code on multiple pages of project
- Next by thread: RE: Reuse code on multiple pages of project
- Index(es):
Relevant Pages
|