Collection Class problems

F

Flemming Dahl

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
 
F

Flemming Dahl

Hi

Succes for me - I made it work :)

Cheers,
Flemming



Flemming Dahl said:
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
 
B

Bob Phillips

Fleming,

This was a complex one (must admit, your code looks overly complex for an
errors collection). I had it down to look at tomorrow.

Can you post you solution? If I do look at it I will post my suggestion.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

Flemming Dahl said:
Hi

Succes for me - I made it work :)

Cheers,
Flemming



Flemming Dahl said:
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
 
O

onedaywhen

Bob, He wasn't adding the instance of the of the object to the
collection.

Flemming, Are you aware your allows you to short cut the Item method
in the traditional manner i.e.

MsgBox gobjErrCodes(2).CodeID & vbCrLf & _
gobjErrCodes(2).Name

--

Bob Phillips said:
Fleming,

This was a complex one (must admit, your code looks overly complex for an
errors collection). I had it down to look at tomorrow.

Can you post you solution? If I do look at it I will post my suggestion.

--

HTH

Bob Phillips
... looking out across Poole Harbour to the Purbecks
(remove nothere from the email address if mailing direct)

Flemming Dahl said:
Hi

Succes for me - I made it work :)

Cheers,
Flemming



Flemming Dahl said:
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
 
F

Flemming Dahl

Hi

Thanks for looking - but don't waste you time it is solved.

In the Add function the line
Dim objErrCode As clsErrCode
should be
Dim objErrCode As New clsErrCode

I belive there was some other small things, but the New part is the main
part.

Cheers,
Flemming
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top