What are the differences between a standard module and a class module?

Showing Answers 1 - 1 of 1 Answers

Dev

  • Oct 23rd, 2006
 

I have refer MSDNClasses differ from standard modules in the way their data is stored. There's never more than one copy of a standard module's data. This means that when one part of your program changes a public variable in a standard module, and another part of your program subsequently reads that variable, it will get the same value.Class module data, on the other hand, exists separately for each instance of the class (that is, for each object created from the class).By the same token, data in a standard module has program scope — that is, it exists for the life of your program — while class module data for each instance of a class exists only for the lifetime of the object; it's created when the object is created, and destroyed when the object is destroyed.Finally, variables declared Public in a standard module are visible from anywhere in your project, whereas Public variables in a class module can only be accessed if you have an object variable containing a reference to a particular instance of a class.All of the above are also true for public procedures in standard modules and class modules. This is illustrated by the following example. You can run this code by opening a new Standard Exe project and using the Project menu to add a module and a class module.Place the following code in Class1:' The following is a property of Class1 objects.Public Comment As String' The following is a method of Class1 objects.Public Sub ShowComment() MsgBox Comment, , gstrVisibleEverywhereEnd SubPlace the following code in Module1:' Code in the standard module is global.Public gstrVisibleEverywhere As StringPublic Sub CallableAnywhere(ByVal c1 As Class1) ' The following line changes a global variable ' (property) of an instance of Class1. Only the ' particular object passed to this procedure is ' affected. c1.Comment = "Touched by a global function."End SubPut two command buttons on Form1, and add the following code to Form1:Private mc1First As Class1Private mc1Second As Class1Private Sub Form_Load() ' Create two instances of Class1. Set mc1First = New Class1 Set mc1Second = New Class1 gstrVisibleEverywhere = "Global string data"End SubPrivate Sub Command1_Click() Call CallableAnywhere(mc1First) mc1First.ShowCommentEnd SubPrivate Sub Command2_Click() mc1Second.ShowCommentEnd Sub

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions