Classes and Collections - Object Oriented Programming
This tutorial is designed to give people an idea of what OOP is (Object Oriented Programming) as it is related to Class Modules and Collections. This uses a very simple example of showing how to create/use Class Properties and Methods. It also shows how to use Collections to create a Collection of Classes.
AI
Shrnutí AI: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.
Zdrojový kód
<div class=Section1> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Classes/Collections Tutorial by Kevin Wiegand. Please download the Zip file that contains the source code if this tutorial is hard to read due to formatting problems.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Definitions:</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>Module</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:2'> </span>A term used to describe where code is stored within Visual Basic.<span style="mso-spacerun: yes"> </span>The three type of modules are (1) Form Modules, (2) Standard Modules, and (3) Class Modules.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>Standard Module</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:2'> </span>A type of Module that contains (or should contain) publically accessible code, in other words, code that is available to any module.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>Class (Module)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:2'> </span>A type of module that allows you to create objects that contain your customized properties and methods.<span style="mso-spacerun: yes"> </span>(The Standard Form (Default:Form1) is actually a Class Module!)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>Object</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:2'> </span>An Object is a Control (TextBox, Label), or it can be a Variable that defines an instance of a Class.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>Object Oriented Programming (OOP)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:2'> </span>Simply put, OOP is programming with objects.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:2'> </span>A Collection is simply a group of related Objects.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Why use Class Modules?</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>Class modules, as said before, offer a very useful tool - objects.<span style="mso-spacerun: yes"> </span>Classes can has multiple instances of its code, and each instances properties/methods belong to that instance only.<span style="mso-spacerun: yes"> </span>Standard Module code can only exist once.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Lets see an example!</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>All these definitions, and a short explanation of Class Modules vs Standard Modules - you need to see code, right?<span style="mso-spacerun: yes"> </span>OK, start Visual Basic, and start a new Standard Exe Project.<span style="mso-spacerun: yes"> </span>Add a Class Module.<span style="mso-spacerun: yes"> </span>Rename Project1 to ClassTest; Form1 to frmMain; Class1 to clsClassTest.<span style="mso-spacerun: yes"> </span>Paste the following code into each respective module, and then save the project:</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>frmMain:</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>***Start Copy***</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'Require Variable Declaration (I believe that VB.Net already requires that you</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'declare all your variables before use - not only does this save memory, but</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'it also saves you the hassle of keeping track of things!)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Option Explicit</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'This defines a Collection, it is empty right now, but we will fill it later :)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private fClassCollection As New Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'We'll use Form_Keypress instead of using a bunch of CommandButtons - this is easier</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'to do for this example</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Sub Form_KeyPress(KeyAscii As Integer)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Select Case Chr(KeyAscii)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Case "a"</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>AddItemToCollection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Case "f"</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>ReturnNamesByFunction</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Case "o"</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>PrintNames</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Case "p"</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>ReturnNamesByProperty</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Case "s"</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>ReturnNamesBySub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Case " "</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>ClearCollection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>End Select</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Sub Form_Load()</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'Set initial Form properties, if you want, just set the properties in the Properties</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'Pane, and remove this code</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Me.ScaleMode = vbPixels</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Me.Width = Screen.Width</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Me.Height = Screen.Height</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Me.Move 0, 0</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'Lets us know how many Objects in our Collection (should be zero right now)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Me.Caption = "Total Names in Collection is " & fClassCollection.Count</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Sub AddItemToCollection()</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This is what creates a new instance of the Object 'ClassTest'</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Dim clsNewClass As New clsClassTest</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Dim strName As String</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This sets the Property 'Name' for the ClassTest Object</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>clsNewClass.Name = InputBox("Enter a name:")</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This adds the newly created Object to the Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>fClassCollection.Add clsNewClass</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'You need to 'close' the new Collection Object in order to add another one.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Set clsNewClass = Nothing</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'Lets us know how many Objects in our Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Me.Caption = "Total Names in Collection is " & fClassCollection.Count</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Sub ClearCollection()</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This creates an Object Variable that will hold references to the Objects in</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'the Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Dim Obj As Object</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>For Each Obj In fClassCollection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This uses the LIFO (Last In First Out) method to remove each Object in the</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'Collection, putting in '1' in place of fClassCollection.Count will use the</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'LILO (Last In Last Out) method to remove each Object in the Collection.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>fClassCollection.Remove fClassCollection.Count</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Next Obj</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'Lets us know how many Objects in our Collection (should be zero right now)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Me.Caption = "Total Names in Collection is " & fClassCollection.Count</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Sub PrintNames()</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This creates an Object Variable that will hold references to the Objects in</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'the Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Dim Obj As Object</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'Clear the form first</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Me.Cls</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'Print out a litle message</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Me.Print "The following " & fClassCollection.Count & " names are in the Collection:" & vbCrLf</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>For Each Obj In fClassCollection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This calls the Method to Print the Names currently in the Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Obj.PrintName Me</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Next Obj</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Sub ReturnNamesByFunction()</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This creates an Object Variable that will hold references to the Objects in</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'the Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Dim Obj As Object</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>For Each Obj In fClassCollection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This calls the Method to Return the Names currently in the Collection, and then</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'MessageBox it to you, note that this is actually a function instead of a sub as</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'from the MsgBoxNames procedure</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>MsgBox Obj.ReturnName, vbOKOnly + vbInformation</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span><span style="mso-spacerun: yes"> </span>Next Obj</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Sub ReturnNamesByProperty()</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This creates an Object Variable that will hold references to the Objects in</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'the Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Dim Obj As Object</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>For Each Obj In fClassCollection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This calls the Method to Return the Names currently in the Collection, and then</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'MessageBox it to you, note that this is actually a function instead of a sub as</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'from the MsgBoxNames procedure</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>MsgBox Obj.Name, vbOKOnly + vbInformation</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Next Obj</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Sub ReturnNamesBySub()</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This creates an Object Variable that will hold references to the Objects in</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'the Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Dim Obj As Object</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>For Each Obj In fClassCollection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This calls the Method to MessageBox out the Names currently in the Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Obj.MsgBoxName</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Next Obj</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>***End Copy***</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>clsClassTest:</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>***Start Copy***</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Option Explicit</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'These are internal variables that any one particular instance of this Class can see</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'This holds the Name Property</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private fstrName As String</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'These hold the Max and Min sizes for the GenerateRandomText Function</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Const fcMin = 5</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Const fcMax = 10</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'This sets the Name Property, it is based off of the Private 'fstrName' Variable</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Property Let Name(ByVal strName As String)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>If strName = "" Then</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'This will call the Private Sub to create a random jumbled string for the</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>''Name' property if the user add a name that is empty</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Randomize Timer</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>GenerateRandomText (Rnd * (fcMax - fcMin)) + fcMin</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Else</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>fstrName = strName</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>End If</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Property</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'This returns the Name Property, it also must be based on the Private 'fstrName' Variable</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Property Get Name() As String</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Name = fstrName</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Property</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'This is a simple Private Procedure contained in this Class.<span style="mso-spacerun: yes"> </span>Any new instance of this class</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'cannot specifically call this Procedure, it can only be called by itself</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Private Sub GenerateRandomText(ByVal intSize As Integer)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Dim lngCounter As Long</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Dim strTemp As String</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Dim bytRnd As Byte</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Randomize Timer</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>For lngCounter = 1 To intSize</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>bytRnd = CByte((Rnd * (Asc("z") - Asc("a"))) + Asc("a"))</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>strTemp = strTemp & Chr(bytRnd)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Next lngCounter</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>Name = strTemp</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'This is a simple Public Procedure contained in this Class.<span style="mso-spacerun: yes"> </span>Any new instance of this class</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'can specifically call this Procedure</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Public Sub MsgBoxName()</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>MsgBox fstrName, vbOKOnly + vbInformation</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'This is a simple Public Procedure contained in this Class.<span style="mso-spacerun: yes"> </span>Any new instance of this class</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'can specifically call this Procedure</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Public Sub PrintName(ByVal destObj As Object)</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>'Notice how I called the Property 'Name' instead of referencing the 'fstrName' Variable</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>destObj.Print Name</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Sub</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'This is a simple Public Function contained in this Class.<span style="mso-spacerun: yes"> </span>Any new instance of this class</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>'can specifically call this Function</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Public Function ReturnName() As String</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style="mso-spacerun: yes"> </span>ReturnName = fstrName</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>End Function</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>***End Copy***</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>To use this example you can:</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>type 'a' to add a name to the collection, leave the input box empty to create a random string</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>type 'f' to MessageBox the names in the Collection, called using a Function in the Class</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>type 'o' to print the name in the Collection, called using a Procedure in the Class that contains a Private Procedure within the Class.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>type 'p' to MessageBox the names in the Collection, called using the Name Property in the Class</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>type 's' to MessageBox the names in the Collection, called using a Procedure in the Class</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>type ' ' (spacebar) to clear the Collection</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><![if !supportEmptyParas]> <![endif]><o:p></o:p></span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'>Final Notes:</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>In this tutorial, you should have learned how to create and use Class Modules.<span style="mso-spacerun: yes"> </span>You have learned how to create and use Class Properties, and you have learned how to create and use Class Methods (Methods as Public Procedures, Methods as Public Functions, and Methods as Private Procedures).<span style="mso-spacerun: yes"> </span>You have also learned how to use Collections.<span style="mso-spacerun: yes"> </span>You have learned how to Add Objects to a Collection, and Remove Objects from a Collection, as well as loop through each Object in a Collection.</span></font></p> <p class=MsoNormal><font size=3 face="Times New Roman"><span style='font-size: 12.0pt'><span style='mso-tab-count:1'> </span>If you find any bugs or problems with this tutorial, please let me know!<span style="mso-spacerun: yes"> </span>If you have anything to add or comment on, please let me know!<span style="mso-spacerun: yes"> </span>If you have found this tutorial helpful, please vote!<span style="mso-spacerun: yes"> </span>I can be reached at EinsturzendeNeubauten@hotmail.com, or visit my WebSite at http://www.geocities.com/wieganka, or my mirror site at http://4.41.60.122</span></font></p> </div>
Původní komentáře (3)
Obnoveno z Wayback Machine