Convert UDT to Class

Converted UDT usage sample


Project1: Standard EXE containing Form1
Form1: A simple form with a single command button called Command1
Option Explicit

Private Sub Form_Load()
   Randomize Timer 'Initialize the random number generator
End Sub

Private Sub Command1_Click()
   Dim o As Class1 'Declare the object variable
   Dim c As Collection 'Declare the collection variable
   Dim i As Integer 'Declare the counter variable

   'Variable scope is no longer an issue.
   'This exe can "see" the MultiUse Class that's exposed by Project2.
   Dim oClass As MyClass 'Declare the Class (was UDT) variable
   Set o = New Class1 'Instantiate the class
   Set c = o.CollectInfo 'Populate the collection. (Works perfectly)

   For i = 1 To c.Count 'Loop through the collection
      Set oClass = c(i) 'Set the Class variable = collection item
      Debug.Print oClass.StringField 'Wow! Works!
   Next 

End Sub
 

Project2: ActiveX DLL containing Class1 and a class module called MyClass
Class1: Class module

Option Explicit
'This function will return a collection of MyClass objects
'that have been populated with random data.

Public Function CollectInfo() As Collection
   Dim i As Integer 'Declare counter variable
   Dim oClass As MyClass 'Declare Class (was UDT) variable
   Dim c As Collection 'Declare collection variable 
   Set c = New Collection 'Instantiate the collection
   For i = 0 To 10

      'About the only difference you'll notice here
      '(besides the fact that it works) is that you need to
      'instantiate an instance of the class object before using it.
      'No big deal. One line of code.
      Set oClass = New MyClass
  
      With oClass 'Same syntax as UDT
         .FloatField = Rnd * 255
         .LongField = Rnd * 255
         .StringField = String$(Rnd * 255, Chr$((Rnd * 25) + 65))
      End With
      c.Add oClass 'This works perfectly now

   Next

   For i = 1 To c.Count 'We can now see the data we added.

      Set oClass = c(i) 'Set class object = collection item
      With oClass
         Debug.Print .FloatField
         Debug.Print .LongField
         Debug.Print .StringField
      End With
   Next
   Set CollectInfo = c 'Return the populated collection
End Function


MyClass: Class module
 
'Even though there is far more power available using a class module,
'this can be the entire contents of the class module if you want.
Option Explicit
Public StringField As String
Public FloatField As Double
Public LongField As Long
'By using Public Get/Let Properties instead of simply declaring a
'Public variable, you gain the ability to do full range checking or
'any number of adjustments on other members of the class if needed.
'See Below
 

Alternate coding for MyClass that shows a bit of the power you get from doing the conversion
Option Explicit
Public StringField As String
Public FloatField As Double
Private mlLongField As Long 'Changed to Private

'Converted LongField to a Public Property Get/Let pair
Public Property Get LongField() As Long
   LongField = mlLongField
End Property

Public Property Let LongField(ByVal Setting As Long)
   'Do Range checking. Accept only 51 through 149
   If (Setting > 50) And (Setting < 150) Then
      mlLongField = Setting 'Setting is valid. Save it.
      'Now you know that Setting is in range, you
      'can do anything you want... like manipulating
      'another member of this class.
      'Rare but handy... can't do this automatically with a UDT
      FloatField = mlLongField \ (Rnd * 15) 'Float field adjusted
   Else
      Err.Raise 380 'Invalid property value
   End If
End Property

Private Sub Class_Initialize()
   'You can now pre-set your Default values if you want.
   'You can't do this automatically with a UDT at all.
   mlLongField = 99
End Sub

'Possibilities are endless. You can control how the client App can create instances of the class by setting it's Instancing property = MultiUse (client can create all they want) or PublicNotCreatable (Client can't create any. It can only access those created by this project).