473,396 Members | 1,938 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Reuse code on multiple pages of project

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!
--
Sandy
Nov 19 '05 #1
21 2093

Just referenced that page myself a minute ago :)

http://aspnet.4guysfromrolla.com/articles/041305-1.aspx

Let me know if you have any more questions..

Cheers,
Tom Pester
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!
--
Sandy

Nov 19 '05 #2
TJS
http://www.c-sharpcorner.com/2/pr12.asp

"Sandy" <Sa***@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com...
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!
--
Sandy

Nov 19 '05 #3
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?
--
Sandy

Nov 19 '05 #4
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
Nov 19 '05 #5
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
"To********************@pandora.be" wrote:
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

Nov 19 '05 #6
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
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

"To********************@pandora.be" wrote:
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

Nov 19 '05 #7
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.

--
Sandy
"To********************@pandora.be" wrote:
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
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

"To********************@pandora.be" wrote:
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


Nov 19 '05 #8
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
Nov 19 '05 #9

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
Nov 19 '05 #10
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.

--
Sandy
"To********************@pandora.be" 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

Nov 19 '05 #11
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/...3d1da01c29884a

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.

"To********************@pandora.be" 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

Nov 19 '05 #12
Hello Tim -

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

Thanks also for the book suggestions!
--
Sandy
"To********************@pandora.be" wrote:
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/...3d1da01c29884a

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.

"To********************@pandora.be" 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


Nov 19 '05 #13
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

"To********************@pandora.be" 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

Nov 19 '05 #14

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
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
"To********************@pandora.be" 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

Nov 19 '05 #15
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

"tom pester" wrote:

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
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
"To********************@pandora.be" 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


Nov 19 '05 #16
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/
=============================

"Manso" <Ma***@discussions.microsoft.com> wrote in message
news:5A**********************************@microsof t.com... 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

"tom pester" wrote:

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
> 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
> "To********************@pandora.be" 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


Nov 19 '05 #17
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
Nov 19 '05 #18
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

"tom pester" wrote:
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

Nov 19 '05 #19
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/
=============================

"Manso" <Ma***@discussions.microsoft.com> wrote in message
news:0E**********************************@microsof t.com... 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

"tom pester" wrote:
> 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

Nov 19 '05 #20

If you use Visual Studio it will do the compile for you and place it in the
bin directory AFAIK.

If you are able to make the swithc to asp.net version 2 I suggest you make
it cause it makes these things easier:
drop the class file in the App_Code folder and after importing the namespace
you are ready to go.

Cheers,
Tom Pester
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/
=============================
"Manso" <Ma***@discussions.microsoft.com> wrote in message
news:0E**********************************@microsof t.com...
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
"tom pester" wrote:
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

Nov 19 '05 #21
re:
If you use Visual Studio it will do the compile for you and place it in the bin
directory AFAIK.
Not if the source code file is in the bin directory, and only if
ASP.NET 2.0 is being used *and* the source code is dropped
in the App_Code dir, as you point out.


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

"tom pester" <To********************@pandora.be> wrote in message
news:a1***************************@news.microsoft. com...
If you use Visual Studio it will do the compile for you and place it in the bin
directory AFAIK.

If you are able to make the swithc to asp.net version 2 I suggest you make it cause it
makes these things easier:
drop the class file in the App_Code folder and after importing the namespace you are
ready to go.

Cheers,
Tom Pester
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/
=============================
"Manso" <Ma***@discussions.microsoft.com> wrote in message
news:0E**********************************@microsof t.com...
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
"tom pester" wrote:

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

Nov 19 '05 #22

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
72
by: Mel | last post by:
Are we going backwards ? (please excuse my spelling...) In my opinion an absolute YES ! Take a look at what we are doing ! we create TAGS, things like <H1> etc. and although there are tools...
6
by: Frank Oquendo | last post by:
I'm looking for advice from people who are using C# in a production environment with multiple developers. How do you reuse your code between projects? Do you organize your class files into...
171
by: tshad | last post by:
I am just trying to decide whether to split my code and uses code behind. I did it with one of my pages and found it was quite a bit of trouble. I know that most people (and books and articles)...
17
by: tshad | last post by:
Many (if not most) have said that code-behind is best if working in teams - which does seem logical. How do you deal with the flow of the work? I have someone who is good at designing, but...
3
by: Carl Johansen | last post by:
I have a big ASP website (used by several thousand car dealers) that is a collection of lots of small and medium-sized applications. Now I want to start adding ASP.NET applications to it. I have...
1
by: Bob Dufour | last post by:
I created a form for enetering values in lookup tables (eg Phone Types) in a project. The form uses a SQLdataAdapter and works fine in project A. In another solution - Project B- I want to reuse...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
19
by: jacob navia | last post by:
There is an interesting discussion running in Slashdot now, about code reuse. The thema of the discussion is here: < quote > Susan Elliot Sim asks: "In the science fiction novel, 'A Deepness...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.