Results 1 to 5 of 5

Thread: [RESOLVED] Setting a class as a property

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Posts
    28

    Resolved [RESOLVED] Setting a class as a property

    Hi
    I am trying to set a class as a property of another class.
    I have one class clsCompany and another clsPerson
    I am trying to set clsPerson as a property of clsCompany and as an array.
    Elsewhere in clsCompany I am setting the number of of 'clsPerson' instances (PersonCount) and creating a local array of Person instances (moPerson).
    I then try and return the clsPerson as follows in my code

    Code:
    For intCnt = 1 To oCompany.PersonCount
      Debug.print oCompany.Person(intCnt).Surname
    Next
    Code:
    Public Property Get Person(ByVal iI As Integer) As clsPerson
      Set Person(iI) = New clsPerson   'Problem with this line
      iI = iI - 1                                                      
      If iI >= 0 And iI <= UBound(moPerson) Then        
          Set Person(iI) = moPerson(iI)                       
      End If
        
    End Property
    As soon as it gets to 'Set Person(iI) = New clsPerson' it just loops through from 'Public Property Get Person...'

    How can I best achieve what I am trying to do?

    Many thanks in advance.
    Last edited by bobjbarker; Apr 12th, 2011 at 02:13 AM.

  2. #2

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Feb 2003
    Posts
    28

    Re: Setting a class as a property

    Thanks for the response Martin.
    I have just realised I had copied my code wrong into the forum and have edited my original post.
    It loops back round to 'Public Property Get Person...' on the line of code 'Set Person(iI) = New clsPerson'.

  4. #4
    Fanatic Member
    Join Date
    Mar 2009
    Posts
    804

    Re: Setting a class as a property

    Have you tried using a Collection Class?
    Code:
    'Form=========================
    Option Explicit
    Private Sub Form_Load()
     Dim oCmp As Company
     Dim oPer As Person
     Dim i As Long
     Set oCmp = New Company
     'build the company
     For i = 1 To 10
      Set oPer = New Person
      oPer.Name = "FullName " & i  'build a new person
      oPer.SurName = "Surname " & i
      oCmp.Add oPer 'add the person to the company
     Next
     'now enumerate persons in the company
     For Each oPer In oCmp
      Debug.Print oPer.Name, oPer.SurName
     Next
     ' another way
     For i = 1 To oCmp.Count
      Debug.Print oCmp.Item(i).Name, oCmp.Item(i).SurName
     Next
    End Sub
    'class Person=======================
    Option Explicit
    'change to properties to allow data validation
    Public Name As String
    Public SurName As String
    'class Company===============================
    Option Explicit
    Private mCol As Collection
    Public Sub Add(Obj As Person, Optional sKey As String)
     If Len(sKey) = 0 Then
      mCol.Add Obj
     Else
      mCol.Add Obj, sKey
     End If
    End Sub
    Public Property Get Count() As Long
     Count = mCol.Count
    End Property
    Public Sub Remove(vntIndexKey As Variant)
     mCol.Remove vntIndexKey
    End Sub
    Public Property Get Item(vntIndexKey As Variant) As Person
     Set Item = mCol.Item(vntIndexKey)
    End Property
    Public Property Get NewEnum() As IUnknown
     'Tools|Procedure|Advanced
     'Change Procedure ID to -4
     'Allows For Each construct
     Set NewEnum = mCol.[_NewEnum]
    End Property
    Private Sub Class_Initialize()
     Set mCol = New Collection
    End Sub
    Private Sub Class_Terminate()
     Set mCol = Nothing
    End Sub

  5. #5

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width