国产探花免费观看_亚洲丰满少妇自慰呻吟_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進行明確引用
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 胶州市| 定兴县| 临澧县| 潼南县| 蕲春县| 龙江县| 讷河市| 开江县| 莲花县| 宜良县| 同德县| 仙游县| 中方县| 台山市| 平远县| 三台县| 彝良县| 洛川县| 礼泉县| 德庆县| 垦利县| 临泉县| 临湘市| 夏邑县| 孙吴县| 双流县| 太谷县| 沅江市| 玛曲县| 惠来县| 晋城| 大姚县| 平遥县| 阳东县| 名山县| 吴堡县| 南川市| 固阳县| 资源县| 柏乡县| 安宁市|