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

首頁(yè) > 開發(fā) > 綜合 > 正文

HOW TO:初始化共享變量

2024-07-21 02:16:12
字體:
供稿:網(wǎng)友

“初始化共享變量”本不是一個(gè)how to問題。這里提出來,主要的再次認(rèn)識(shí)一下new。

在創(chuàng)建構(gòu)造函數(shù)時(shí),new是很有趣的,sub new()有別于別的函數(shù),因?yàn)樗梢猿霈F(xiàn)兩次。

public class testclass



    shared sub new()



        '...



    end sub



    sub new()



        '...



    end sub




 


    '...



end class




 


當(dāng) dim t as new testclass時(shí),它先執(zhí)行shared sub new()然后再執(zhí)行sub new()。shared sub new()只執(zhí)行一次,在實(shí)例類中,它在第一個(gè)實(shí)例產(chǎn)生時(shí)執(zhí)行。




 


運(yùn)用這個(gè)特性,shared sub new()在初始化類的共享變量時(shí)就會(huì)特別的方便。



下面作一個(gè)比較:




 


在靜態(tài)類中,



public class mycommand '使用shared sub new()



    private shared cn as sqlclient.sqlconnection



    shared sub new()



        cn = new sqlclient.sqlconnection("persist security info=false;integrated security=sspi;database=northwind;server=(local)")



    end sub




 


    public shared function command(byval commandtext as string) as sqlclient.sqlcommand



        return new sqlclient.sqlcommand(commandtext, cn)



    end function



end class




 


public class mycommand0 '不使用shared sub new()



    private shared cn as sqlclient.sqlconnection




 


    public shared function command(byval commandtext as string) as sqlclient.sqlcommand



        if cn is nothing then



            cn = new sqlclient.sqlconnection("persist security info=false;integrated security=sspi;database=northwind;server=(local)")



        end if




 


        return new sqlclient.sqlcommand(commandtext, cn)



    end function



end class




 


在實(shí)例類中,



public class mycommand1 '使用shared sub new()




 


    private shared cn as sqlclient.sqlconnection



    private _command as sqlclient.sqlcommand




 


    shared sub new()



        cn = new sqlclient.sqlconnection("persist security info=false;integrated security=sspi;database=northwind;server=(local)")



    end sub




 


    sub new()



        _command = new sqlclient.sqlcommand("select * from orders", cn)



    end sub




 


    sub new(byval commandtext as string)



        _command = new sqlclient.sqlcommand(commandtext, cn)



    end sub




 


    public readonly property command() as sqlclient.sqlcommand



        get



            return _command



        end get



    end property




 


    '以下省略......



end class




 


public class mycommand2 '不使用shared sub new()




 


    private shared cn as sqlclient.sqlconnection



    private _command as sqlclient.sqlcommand




 


    sub new()



        if cn is nothing then



            cn = new sqlclient.sqlconnection("persist security info=false;integrated security=sspi;database=northwind;server=(local)")



        end if



        _command = new sqlclient.sqlcommand("select * from orders", cn)



    end sub




 


    sub new(byval commandtext as string)




 


        if cn is nothing then



            cn = new sqlclient.sqlconnection("ersist security info=false;integrated security=sspi;database=northwind;server=(local)")



        end if




 


        _command = new sqlclient.sqlcommand(commandtext, cn)



    end sub




 


    public readonly property command() as sqlclient.sqlcommand



        get



            return _command



        end get



    end property




 


    '以下省略......



end class




 




發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 长治县| 西乌珠穆沁旗| 双流县| 辽阳县| 来安县| 汾西县| 禹城市| 乌鲁木齐市| 南雄市| 唐海县| 大埔县| 蒙阴县| 榆社县| 台中市| 洪雅县| 依兰县| 平遥县| 从化市| 昔阳县| 乐平市| 宝坻区| 财经| 潮安县| 甘谷县| 巫山县| 利川市| 宜宾县| 荔浦县| 炉霍县| 义乌市| 馆陶县| 凤凰县| 石狮市| 台山市| 辽宁省| 甘德县| 班戈县| 绍兴县| 苗栗市| 四子王旗| 响水县|