如果您有在每个页面上重复使用的代码段,例如。一个标题,页脚或菜单,你可以明显地将它们放在一个单独的文件中并链接 在主页面中声明您的潜艇,或者如果它们在每个页面上都相同,请将它们放在外部文件中并使用#include。 这对未定义的变量没有帮助。问题,除非你是在谈论创建一个包含页面,其中全局变量是声明的。全局变量必须在函数或子函数之外声明。 但是,我建议尽量减少全局变量的使用。全局变量 可能会混淆事物,因为它们的值可以在 代码中的任何位置更改,从而使调试变得非常困难。而不是全局变量,尽可能使用sub 和函数参数来传递你的对象和 值。 OTOH,全局常量可能是一件好事。例如,如果你有一个硬编码的 连接字符串,将 封装在一个常量中肯定是有用的。 To说实话,你试图清理你的页面可能会进一步混淆问题。 如果你有在每个页面上重复使用的代码段,例如。一个页眉,页脚或菜单,你可以明显将它们放在一个单独的文件中并将它们链接起来......但使用subs听起来不是一个非常好的解决方案... 为什么不呢?我想说这两种技术的结合肯定会有所帮助。 让事情更清洁。将代码封装在一个包含页面的函数或子元素中。 。在你的观点中使用函数或子元素的缺点是什么? Bob Barrows - Microsoft MVP - ASP / ASP.NET 请回复新闻组。这个电子邮件帐户是我的垃圾邮件陷阱所以我 不经常检查它。如果您必须离线回复,请删除 无垃圾邮件 I have decided to re-write the intranet site i created over a year ago. Thecoding is pretty awful and hard to read cos I made the mistake of notputting comments in or putting crappy comments in with the code. So I havedecided to try and make things neater by grouping it into subs. So i have asub to create the variables, a sub for the connection etc.Only problem is when i call my subs, it says that the variable is undefined.If i scrap the subs and put everything into the order they would be the pageworks. I use the subs it doesn''t.Can anyone offer any insight or point me to a page that describes in detailthe solution i am trying to achievethanks in advancesample code below.my page would look something like<body bgcolor="#FFFFFF" text="#000000" link="#804040" vlink="#008080"alink="#004080"><%declareVar()%><!--#include file="connection/connstring.inc"--><%DBStrings()%>my subs are below as an example.<%sub declareVar()''The declaration sectiondim conndim strconndim physicaldbpathdim strSQLDim RSdim Fdim Xend sub%><%sub DBStrings()set conn = server.createobject("adodb.connection")conn.open strconnstrSQL = "Select Name, Number, Full_Number From AB_Dials;"set RS = conn.execute(strSQL)end sub%><%sub connClose()RS.closeset RS = nothingconn.closeset conn = nothingend sub%> 解决方案 By declaring the variables within a sub, you are limiting the scope of thevariables to within that sun only...Declare your subs in the main page, or if they are the same on every page,put them in an external file and use #include.To be honest, your attempt to clean up your pages may well further confusethe problem.If you have segments of code that are re-used on every page, eg. a header,footer ot menu, you can obvious place these in a separate file and link themin... but using subs doesnt sound like a very good solution...Chris"Steven Scaife" <nospam> wrote in messagenews:%2****************@TK2MSFTNGP09.phx.gbl... I have decided to re-write the intranet site i created over a year ago.The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in with the code. So Ihave decided to try and make things neater by grouping it into subs. So i havea sub to create the variables, a sub for the connection etc. Only problem is when i call my subs, it says that the variable isundefined. If i scrap the subs and put everything into the order they would be thepage works. I use the subs it doesn''t. Can anyone offer any insight or point me to a page that describes indetail the solution i am trying to achieve thanks in advance sample code below. my page would look something like <body bgcolor="#FFFFFF" text="#000000" link="#804040" vlink="#008080" alink="#004080"> <% declareVar() %> <!--#include file="connection/connstring.inc"--> <% DBStrings() %> my subs are below as an example. <% sub declareVar() ''The declaration section dim conn dim strconn dim physicaldbpath dim strSQL Dim RS dim F dim X end sub %> <% sub DBStrings() set conn = server.createobject("adodb.connection") conn.open strconn strSQL = "Select Name, Number, Full_Number From AB_Dials;" set RS = conn.execute(strSQL) end sub %> <% sub connClose() RS.close set RS = nothing conn.close set conn = nothing end sub %>Ok thanks, any ideas of how i can organise my pages so they are structurednice or should i just stick with the standard way of doing it and comment itproperly. The intranet needs rewriting to use dsn less connections anywayscos its using a dsn and the code i wrote is pap."CJM" <cj*******@newsgroups.nospam> wrote in messagenews:uz*************@TK2MSFTNGP10.phx.gbl... By declaring the variables within a sub, you are limiting the scope of the variables to within that sun only... Declare your subs in the main page, or if they are the same on every page, put them in an external file and use #include. To be honest, your attempt to clean up your pages may well further confuse the problem. If you have segments of code that are re-used on every page, eg. a header, footer ot menu, you can obvious place these in a separate file and linkthem in... but using subs doesnt sound like a very good solution... Chris "Steven Scaife" <nospam> wrote in message news:%2****************@TK2MSFTNGP09.phx.gbl... I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in with the code. So I have decided to try and make things neater by grouping it into subs. So ihave a sub to create the variables, a sub for the connection etc. Only problem is when i call my subs, it says that the variable is undefined. If i scrap the subs and put everything into the order they would be the page works. I use the subs it doesn''t. Can anyone offer any insight or point me to a page that describes in detail the solution i am trying to achieve thanks in advance sample code below. my page would look something like <body bgcolor="#FFFFFF" text="#000000" link="#804040" vlink="#008080" alink="#004080"> <% declareVar() %> <!--#include file="connection/connstring.inc"--> <% DBStrings() %> my subs are below as an example. <% sub declareVar() ''The declaration section dim conn dim strconn dim physicaldbpath dim strSQL Dim RS dim F dim X end sub %> <% sub DBStrings() set conn = server.createobject("adodb.connection") conn.open strconn strSQL = "Select Name, Number, Full_Number From AB_Dials;" set RS = conn.execute(strSQL) end sub %> <% sub connClose() RS.close set RS = nothing conn.close set conn = nothing end sub %>CJM wrote: By declaring the variables within a sub, you are limiting the scope of the variables to within that sun only...Right Declare your subs in the main page, or if they are the same on every page, put them in an external file and use #include.That won''t help with the "undefined variables" problem, unless you''retalking about creating an include page in which the global variables aredeclared. Global variables must be declared outside of functions or subs.However, I suggest minimizing the use of global variables. Global variablescan confuse things because their values can be changed anywhere in yourcode, making debugging very difficult. Instead of global variables, use suband function arguments as much as possible to pass around your objects andvalues.OTOH, global constants can be a great thing. If you have a hard-codedconnection string, for instance, it would certainly be useful to encapsulatethat in a constant. To be honest, your attempt to clean up your pages may well further confuse the problem. If you have segments of code that are re-used on every page, eg. a header, footer ot menu, you can obvious place these in a separate file and link them in... but using subs doesnt sound like a very good solution...Why not? I would say a combination of both techniques will certainly helpmake things cleaner. Encapsulate the code in a function or sub which isplace in an include page. What is the downside of using functions or subs inyour view?Bob Barrows--Microsoft MVP - ASP/ASP.NETPlease reply to the newsgroup. This email account is my spam trap so Idon''t check it very often. If you must reply off-line, then remove the"NO SPAM" 这篇关于使用subs的清洁ASP页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!