View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
Flemming Dahl[_2_] Flemming Dahl[_2_] is offline
external usenet poster
 
Posts: 18
Default Collection Class problems

Hi

Succes for me - I made it work :-)

Cheers,
Flemming



"Flemming Dahl" < wrote in message
...
Hi.

I've just trying to make a class, and I have a error, that I need help

with.

I have no problem getting data into my collection class, but I have

problems
using the functions 'Exists' and 'Item'........... Error 427: Object
required

Can you tell me why ?

Thanks
Flemming



My Class 'clsErrCode' looks like this:
Option Explicit

Private mvCodeID As Variant
Private msName As String
Private mdFreq As Double
Private mdDura As Double


Public Property Let CodeID(ByVal vValue As Variant)
mvCodeID = vValue
End Property
Public Property Get CodeID() As Variant
CodeID = mvCodeID
End Property


Public Property Let Name(ByVal sValue As String)
msName = sValue
End Property
Public Property Get Name() As String
Text = msName
End Property


Public Property Let Frequency(ByVal dValue As Double)
mdFreq = dValue
End Property
Public Property Get Frequency() As Double
Frequency = mdFreq
End Property


Public Property Let Duration(ByVal dValue As Double)
mdDura = dValue
End Property
Public Property Get Duration() As Double
Duration = mdDura
End Property


My collection class 'clsErrCodes' looks like this:
Option Explicit

Private mCol As Collection


' Add
Public Function Add( _
ByVal vntCode As Variant, _
ByVal sErrText As String _
) As clsErrCode

On Error GoTo ErrHandle
Dim objErrCode As clsErrCode

' If Not (Exists(vntCode)) Then
' Tilføjer Code til samlingen
mCol.Add vntCode, CStr(vntCode)

' Sætter Code's Egenskaber
objErrCode.CodeID = vntCode

' Returnerer 'ErrCode' objektet
Set Add = objErrCode
' End If

Set objErrCode = Nothing
Exit Function

ErrHandle:
' 457 - This key already associated with an element of this collection
If Err.Number = 457 Then
Set Add = Nothing
End If
End Function


' Count
Public Property Get Count() As Long
Count = mCol.Count
End Property


' Remove
Public Function Remove(ByVal vntCode As Variant) As Boolean
mCol.Remove vntCode
End Function


' Exists
Public Function Exists(ByVal vntCode As Variant) As Boolean
Dim objErrCode As New clsErrCode
Dim bRetVal As Boolean

bRetVal = False

For Each objErrCode In mCol
If objErrCode.CodeID = vntCode Then
bRetVal = True
Exit For
End If
Next objErrCode

Set objErrCode = Nothing
Exists = bRetVal
End Function


' Clear
Public Sub Clear()
Class_Initialize
End Sub

' Item
Public Function Item(ByVal Index As Variant) As clsErrCode
' This line is inserted using NotePad before import
' It is not visible here after import.
'Attribute Item.VB_UserMemId = 0
Set Item = mCol.Item(Index)
End Function


' NewEnum
Public Function NewEnum() As IUnknown
' Thise two lines is inserted using NotePad before import
' Thy are not visible here after import.
'Attribute NewEnum.VB_UserMemId = -4
'Attribute NewEnum.VB_MemberFlags = "40"
Set NewEnum = mCol.[_NewEnum]
End Function


' Class
Private Sub Class_Initialize()
Set mCol = New Collection
End Sub
Private Sub Class_Terminate()
Set mCol = Nothing
End Sub


Trying to display something with this command:
gobjErrCodes is the global variable of clsErrCodes that have been
filled - the 'Count' works fine.
MsgBox gobjErrCodes.Item(2).CodeID & vbCrLf & _
gobjErrCodes.Item(2).Name