zh3 发表于 2007-12-17 10:29:00

创建类实例时为什么要用Collection集合???

<p>有这样一段代码:</p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><span lang="EN-US" style="mso-bidi-font-size: 10.5pt;">‘类模块Cmds的代码</span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><span style="FONT-FAMILY: SimSun; mso-bidi-font-size: 10.5pt; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman';">Option Explicit <p></p></span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;">Public WithEvents cmd As CommandButton <p></p></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;">Private Sub cmd_Click() <p></p></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><span style="mso-spacerun: yes;">&nbsp; UserForm1.TextBox1 = cmd.Caption <p></p></span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;">End Sub </p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><p></p>&nbsp;</p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;">‘窗体UserForm1的代码
                        <p></p></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;">Option Explicit <p></p></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><font color="#f70909">Dim co As New Collection</font>
                        <p></p></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;">Private Sub UserForm_Initialize() <p></p></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><span style="mso-spacerun: yes;">&nbsp; Dim i% <p></p></span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><span style="mso-spacerun: yes;">&nbsp; Dim myc As Cmds <p></p></span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><span style="mso-spacerun: yes;">&nbsp; For i = 1 To 5 <p></p></span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; Set myc = New Cmds <p></p></span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; Set myc.cmd = Me.Controls("CommandButton" &amp; i) <p></p></span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><font color="#ff3300"><span style="mso-spacerun: yes;">&nbsp;&nbsp;&nbsp; co.Add myc</font>
                        <p></p></span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><span style="mso-spacerun: yes;">&nbsp; Next i <p></p></span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><span style="mso-spacerun: yes;">&nbsp; Set myc = Nothing <p></p></span></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;">End Sub </p><p>请问:红色的两段代码有什么意义?为什么去掉了就不行?</p><p>&nbsp;
                        <p></p></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"></p><p class="MsoNormal" align="left" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"></p><p class="MsoNormal" style="BACKGROUND: #fafdf9; MARGIN: 0cm 69.95pt 0pt 21.65pt; TEXT-INDENT: -0.65pt; mso-para-margin-top: 0cm; mso-para-margin-right: 6.66gd; mso-para-margin-bottom: .0001pt; mso-para-margin-left: 2.0gd; mso-char-indent-count: -.06;"><p></p></p>

雪山飞狐_lzh 发表于 2007-12-18 11:24:00

本帖最后由 作者 于 2007-12-20 15:13:20 编辑 <br /><br /> <p>myc的生存期在UserForm_Initialize后就完结了,无法触发cmd_Click事件</p><p>set myc=nothing释放内存</p>

兰州人 发表于 2007-12-19 09:27:00

类模块定义问题。
页: [1]
查看完整版本: 创建类实例时为什么要用Collection集合???