Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Greetings I have written a serie

Status
Not open for further replies.

butler

MIS
Oct 12, 1998
88
US
Greetings

I have written a series of classes (Rules, Rule, Condition, Conditions, CstrList) that allow me to do the following. The 'Rules' and 'Conditions' classes are based on collections. 'CstrList' is based on a dynamic string array. The structure is such that I can say

ParamOfFirstRuleFirstCondition = _
Rules(1).Condition(1).Param(0)

Now the problem. I would like to be able to have access using the 'For Each Next' method. Everything works great until I get to 'Param' where I get an 'Object doesn't support this property or method' error. I could use a collection instead of a string array for Param but this seem like a waist. How can i enum Param? Thx!


Dim rl As Rules
Dim r As Rule
Dim c As Condition
Dim p As CStrList

Set rl = New Rules

Set r = rl.Add("NEW RULE 1", "ANY")
Set c = r.Condition.Add("Condition 1-1", _
"contains", "and")
c.Param.Add "param #1"
c.Param.Add "param #2"
c.Param.Add "param #3"

For Each r In rl
Debug.Print r.Desc & "..."

For Each c In r.Condition
Debug.Print c.Noun & "..."

For Each p In c.Param ''' ERROR HERE!!!
Debug.Print p.Param
Next
Next
Next
 
There's a trick involved. Add this to your Rules class:
Code:
Public Function Count() As Long
    
    On Error GoTo Count_ErrHandler
    
    Count = m_collection.Count
    exit sub

'ErrHandler here
End Function    

Public Function Item(ByVal Index As Long) As clsMyClass
    
    On Error GoTo Item_ErrHandler
    
    Set Item = m_collection(Index)
    Exit sub

'ErrHandler here
End Function

Public Function NewEnum() As IUnknown
    
    On Error GoTo NewEnum_ErrHandler
    
    Set NewEnum = m_collection.[_NewEnum]
    exit sub

'ErrHandler here    
End Function

and here's the trick .. you have to set the Procedure Attribute for the NewEnum to -4 (a magic value). Position your cursor inside the procedure, then click Tools | Procedure Attributes, then click the Advanced >> button. Enter -4 in the Procedure ID box.

To learn more, do a search on "VB6 Custom Collections" in Google.

Chip H.
 
thanks chiph,

I have this working in the Rules & Conditions classes and For/each/next work as great. The problem is the 'Param' property of the Condition class that is based on a StrArray class. This is the one I can't enum:

For Each r In rl ' works great!
For Each c In r.Condition 'works great!
For Each p In c.Param ''' ERROR HERE!!!

Here's the some of the Condition code:

Public Param As CStrList
Private Sub Class_Initialize()
Set Param = New CStrList
End Sub

Here's some of the CStrList codes:

Const INITIAL_SIZE = 10 ' Initial size of the array
Private mElements As Integer
Private mStrArray() As String
Private Sub Class_Initialize()
ReDim Preserve mStrArray(INITIAL_SIZE)
End Sub

Any thoughts???
--bill
 
A lot depends on how you created your StrArray Class, but if like a normal array with an index and upper/lower bounds, why not just loop through it that way.

For i = lbound(c.param) to ubound(c.param)
set p = c.param(i)
....

Instead of the For Each p In c.Param
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top