Reuse code on multiple pages of project

G

Guest

Hello -

I am using Visual Studio .Net. I need an example of how to construct a
class that can be used throughout a project where I can include its subs and
functions in various pages in the project.

I googled this and either I didn't use the correct words, or there doesn't
seem to be much on it.

Any help will be appreciated!
 
G

Guest

Thanks for your responses!

I am using vb .net. I didn't want to go the dll route, if at all possible.

I did see Scott Mitchell's article, but it's about as clear as mud to me.

At any rate, I have the following code and am getting a a blue squiggly line
on RegisterStartupScript with the message:
"Cannot refer to an instance member of a class from within a shared method
or shared member initializer without an explicit instance of the class."

HERE'S MY CLASS:

Imports System.Web.UI.WebControls
Imports System.Web.UI
Imports System.Web.UI.Page

Namespace GenRFE

Public Class RFELIB
Inherits System.Web.UI.Page

Dim ctrl as WebControl

Public Shared Sub SetFocus(ByVal ctrl as Control)
'Sets focus on control
Dim sb as New System.Text.StringBuilder
sb.Append("<script language='javascript'>")
sb.Append("document.getElementById('")
sb.Append(ctrl.ClientID)
sb.Append("').focus();")
sb.Append("</script>")
RegisterStartupScript("SetFocus", sb.ToString)
End Sub
End Class

End Namespace

USING SETFOCUS ROUTINE IN MY PAGE:

Imports RidingForEveryone.GenRFE
Imports RidingForEveryone.GenRFE.RFELIB

Public Class ContactUs
Inherits System.Web.UI.Page

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

SetFocus(txtContactUs)
End Sub

What am I doing wrong?
 
T

Tom.PesterDELETETHISSS

You got it working Sandy? Scott Mitchell's article is not what you needed
while rereading your post. I thought you asked a different question.

You want to create a library of useful functions that you want to be able
to call in every page right?

Give this article a try then:

http://www.aspnet101.com/aspnet101/tutorials.aspx?id=43

(ASP.NET version 2 has another approach but the above code will still work.
If you use Version 2 let me know and I will set you on your way)

I also think you need a good understanding about OOP principles (object oriented
programming). Ian Stallings has wrote about it here:
http://www.4guysfromrolla.com/webtech/091800-1.shtml

Good Luck! Let me know if you have any more questions.

Cheers,
Tom Pester
 
G

Guest

Tom -

Thanks again for responding!

I did read the asp101 article. That's how I came up with the code I did.
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? I guess what I don't understand is
what goes where.

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?

Sandy
 
T

Tom.PesterDELETETHISSS

Are you willing to switch to version 2 of ASP.NET cause I think its so much
better for someone like you who is starting.
A lot of things that used to be hard are simple now. If you do I can send
you some code that will work immidiatly.
I only have version 2 installed so I can't guide you step by step with VS
2003. Ill try your code tomorrow if I have time.

If you can upgrade I advice you download the visual web developper express
edition which is free but is very powerfull.

What version are you using now? VS 2003?


Cheers,
Tom Pester
 
G

Guest

Hi Tom -

Thanks again for your response. I'm using VS 2003. I finally purchased it
after reinstalling the 60-day trial many, many times. Is Version 2 a trial
version that expires? I've heard great things about it.

I'm almost finished with a project that I hope to put on the host shortly
and hate to try messing around with a new version, not knowing what
compatibility problems there may be.

I hope you get a chance to look at my code. I have a feeling it's just
something stupid I neglected to include in the right place.
 
T

Tom.PesterDELETETHISSS

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
 
T

Tom.PesterDELETETHISSS

Visual web developer (VWD) express is still in beta but its good enough to
make the switch now, realy. The express products you download now will expire
in 1 year and are planned to cost very little.
I dont know exectly but it will be 50 dollar or so. What you get for this
is all you need. VS2003 is a cruise ship where you get distracted every day.
But instead drinking cctailsl you should row to the other side :)

Once you get the chance make the switch to VWD express. It will make you
as a starter a *lot* more productive.

If you can't get your common function library than give me a ring. We will
get you there :)


Cheers,
Tom Pester
 
G

Guest

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.
 
T

Tom.PesterDELETETHISSS

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...ing+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
 
G

Guest

Hello Tim -

Thanks so much for your time! I finally got it to work.

Thanks also for the book suggestions!
 
G

Guest

Hi Tom,

Thanks for this post. I have a quick question for you. Does this only work
for inline code? We have created a class and included it with <%@ assembly
src="file.vb"%> and it works inline but if we place it in the codebehind file
(JIT compiled with @page "src" attribute) there's no way we can reach the
file. We've also tried to put it in the bin dir with no luck.

Do you have any suggestions?

Thanks,
Manso
 
T

tom pester

If it's _compiled_ and in the bin directory you can call it from codebehind
files.
Don't forget to import the namespace.

Cheers,
Tom Pester
 
G

Guest

It should get compiled by default when dropping it in bin, shouldn't it? Have
you tried and know that it works? I can't see how it can work. Example:

Test.vb
===================
Namespace NSTest
Public Class Class1
Public Shared Function A () As String
Return "Test"
End Function
End Class
End Namespace

MyTestPage.aspx.vb (code behind)
===================
Imports NSTest

Public Class MyTestPage
Public Sub Overrides OnLoad(e As...)
Response.Write(Class1.A)
End Sub
End Class
===================

How can MyTestPage locate NSTest.Class1?

Any and all help useful.

Thanks,
Manso
 
J

Juan T. Llibre

re:
It should get compiled by default when dropping it in bin, shouldn't it?

No. The /bin directory is for *compiled assemblies* only.

The name bin is short of "binary".
Nothing that isn't compiled goes there.

In ASP.NET 2.0, there's an App_Code directory.
Anything you place in *that* directory will get compiled.



Juan T. Llibre
ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
=============================
 
T

tom pester

It should get compiled by default when dropping it in bin, shouldn't
it? Have you tried and know that it works? I can't see how it can
work. Example:

No it's not compiled by default in asp.net v1.
(in asp.net v2 there is a special dir app_code that does what you describe).

Let me know if it helped you or not...

Cheers,
Tom Pester
 
G

Guest

Alright, I got a bit confused (in an earlier posting you wrote that the class
file gets compiled after a drop in the bin dir, which was all new to me). So,
back to square one. Is there a way one can include a class file (no
assembly), let it be JIT compiled and referenced from a code behind file,
which is also JIT compiled using the src-attribute on the @page directive?

Hope you get what I'm trying to do here. It can be done using the "@assembly
src" directive in an aspx/ascx file so perhaps there might be a
non-declarative way of doing it?

Thanks in advance.
Manso
 
J

Juan T. Llibre

re:
Is there a way one can include a class file (no assembly),
let it be JIT compiled and referenced from a code behind file,
which is also JIT compiled using the src-attribute on the
@page directive?

Short answer : no.



Juan T. Llibre
ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
=============================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top