关于asp.net的代码重用
发表于:2007-06-30来源:作者:点击数:
标签:
在asp .net 中我们通过添加组件类来实现代码重用 在asp.net项目文件中添加组件类假如为conn. vb 打开这个文件,这个文件是 public class conn end class 这样我们有两种方式来进行代码重用 一种方式为直接在class中写代码,一种为在外面定义一个namespaces 如
在asp
.net中我们通过添加组件类来实现代码重用
在asp.net项目文件中添加组件类假如为conn.
vb
打开这个文件,这个文件是
public class conn
end class
这样我们有两种方式来进行代码重用
一种方式为直接在class中写代码,一种为在外面定义一个namespaces
如下(1)
Public Class conn
Inherits System.ComponentModel.Component
Dim connstring As String
#Region " 组件设计器生成的代码 "
Public Sub New(ByVal Container As System.ComponentModel.IContainer)
MyClass.New()
@#
Windows.Forms 类撰写设计器支持所必需的
Container.Add(Me)
End Sub
Public Sub New()
MyBase.New()
@#该调用是组件设计器所必需的。
InitializeComponent()
@#在 InitializeComponent() 调用之后添加任何初始化
End Sub
@#组件重写 dispose 以清理组件列表。
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
@#组件设计器所必需的
Private components As System.ComponentModel.IContainer
@#注意:以下过程是组件设计器所必需的
@#可以使用组件设计器修改此过程。
@#不要使用代码编辑器修改它。
<System.Diagnostics.De
buggerStepThrough()> Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
#End Region
Public Function Conned()
connstring = "user id=sa;password=;initial catalog=huoyun_0405;data source=chenyang;Connect Timeout=30"
Conned = connstring
End Function
end class
这是第一种方式我们定义了一个返回
数据库连接字符串的函数conned
这样我们就可以在其他文件中通过下面的方式来进行代码重用
dim include as new {asp.net项目名称}.conn = new {asp.net项目名称}.conn
myConnection.ConnectionString = include.Conned()
这样我们就可以定义数据库连接了
第二种方式就是自定义命名空间
Namespace include
public class class1
end class
public class class2
end class
End Namespace
好处在于可以包含多个函数,过程,直至变量
其他文件中可以通过
imports {asp.net项目名称}.include引用
或
imports {asp.net项目名称}.include.class1进行明确引用
原文转自:http://www.ltesting.net