国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

關于asp.net的代碼重用

2024-07-10 12:58:30
字體:
來源:轉載
供稿:網友

最大的網站源碼資源下載站,

在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.debuggerstepthrough()> 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進行明確引用
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 富阳市| 安溪县| 木兰县| 平邑县| 广安市| 康马县| 浮梁县| 阜城县| 称多县| 姚安县| 武安市| 锦州市| 大宁县| 准格尔旗| 噶尔县| 西和县| 大关县| 衡山县| 金山区| 湘西| 榕江县| 巩义市| 石林| 梁山县| 东乌珠穆沁旗| 遵义县| 杭锦后旗| 上杭县| 通江县| 白沙| 延川县| 惠来县| 长阳| 柳河县| 琼海市| 公安县| 靖江市| 玉树县| 马山县| 陇西县| 黄梅县|