View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Harald Staff Harald Staff is offline
external usenet poster
 
Posts: 1,327
Default Creating a new Object

Hi Tony

Crashcourse:

Create a new class module. Name it "MyClass1" (without the quotes). Paste
this code into it:

'************ MyClass1 code ***********
Option Explicit

Public LID As Long
Public SName As String

Public Sub SpeakMe()
MsgBox "My name is " & SName & " and my ID is " & LID
End Sub
'*************** end MyClass1code ************

Insert a standard module. Paste this code itno it:

'************ Module 1 code: ************

Option Explicit

Public TheCol As Collection

Sub InitMe()
Dim i As Long
Dim NewClass As MyClass1
Set TheCol = Nothing
Set TheCol = New Collection

For i = 1 To 1000
Set NewClass = New MyClass1
NewClass.LID = i
NewClass.SName = Chr((i Mod 26) + 65) & _
Chr(Int(Rnd * 26) + 97)
TheCol.Add NewClass, "C" & NewClass.LID
Next

End Sub

Sub TestMe()
Dim i As Long
Dim ThisClass As MyClass1
i = Val(InputBox("ID to speak:"))
Select Case i
Case 1 To TheCol.Count
Set ThisClass = TheCol("C" & i)
Call ThisClass.SpeakMe
Set ThisClass = Nothing
Case Else
End Select
End Sub

'************ End Module 1 code: ************

Now runf initme and then several ibstances of TestMe. I believe you will
figure our what happens.

This has endless possibilities, so don't give up if it's a little confuring
the first time.

After a while, you may replace Public LID As Long with "property let" and
"property get" code, those are event macros running when you assign and read
the variables. But that is the next level...

HTH. Best wishes Harald

"ADG" skrev i melding
...
I am experimentins with the Collection Object and want to create a new

object
to hold a user defined type.
I want to store my data in the user defined type and keep track of the

sets
of data using the collection. I have never used the Class module before,

can
anyone point me in the right direction?
--
Tony Green