Solved

Array of a Class in a Class

Posted on 2004-03-25
13
301 Views
Last Modified: 2013-11-23
Here is what I am trying to do.

a is a class with several values

a.val1
a.val2
etc

b contains a

b.a.val1

c needs to have an array of b's

c.b(1).a.val1
c.b(2).a.val1

and finally d needs to have an array of c's

d.c(1).b(1).a.val1

How do I code this in the classes?

Thanks,
Shadowedvaca
0
Comment
Question by:Shadowedvaca
  • 8
  • 5
13 Comments
 
LVL 12

Expert Comment

by:dfiala13
Comment Utility
VB6 or VB.NET?

Have you considered using collections?
0
 

Author Comment

by:Shadowedvaca
Comment Utility
VBA in Access 2002 actually.  So mostly VB 6.  As for collections, I am sort of eh, maybe about it.  my primary lookup is going to be numbers so I thought an array might be a better idea.

Thanks,
Shadowedvaca
0
 

Author Comment

by:Shadowedvaca
Comment Utility
Hmm...  can u elaborate on the collections?

Thanks,
Shadowedvaca
0
 
LVL 12

Accepted Solution

by:
dfiala13 earned 250 total points
Comment Utility

inside class c...

private m_colBs as Collection


public property get Bs() as Collection
  set Bs = m_colBs
End property


so you elsewhere you can refer to the collection of bs like so...

'add a b to c
Dim bi as b

 c.Bs.Add(bi)

'retrieve the first b from c
set bi = c.Bs.Item(1)

'loop through the bs

for  i = 1 to c.Bs.Count
 set bi = c.Bs.Item(i)
Next
0
 

Author Comment

by:Shadowedvaca
Comment Utility
How is a attached to that?
0
 

Author Comment

by:Shadowedvaca
Comment Utility
Well the network keeps going down so I am going home.  I will check on this tomorrow, if I don't figure it out at home tonight...  :-)

Thanks,
Mike
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 12

Expert Comment

by:dfiala13
Comment Utility
How is what attached to what?

A collection is a container of objects
You can use it to hold objects inside of other objects


'---- class c

Private col As Collection


Public Property Get Bs() As Collection
    Set Bs = col
End Property

Private Sub Class_Initialize()
    Set col = New Collection
End Sub


'class b -------------

Private m_sName as string

Public property get Name() as string
    Name = m_sName
End Property


Public Property Set Name(sName as String)
 m_sName = sName)
End Property


'code somewhere else
Dim ci as C
set ci = new C

Dim bi as B
set bi = new B
bi.Name ="My first class"

ci.Bs.Add(bi)


'go retrieve the b inside of c

Dim bi2 as B
set bi2 = c.Bs.Item(1)

Debug.Print(bi2.Name) 'will out put "My first class"
0
 

Author Comment

by:Shadowedvaca
Comment Utility
Alright I got what you were saying and put it all in my code.  Now I gots a question.  Here is the code from my module.

Sub TestMe()
    Dim ThisLst As Lst
    Set ThisLst = New Lst
    Dim ThisPhs As Phs
    Set ThisPhs = New Phs
    Dim ThisOwn As Own
    Set ThisOwn = New Own
    Dim i As Integer
    For i = 1 To 3
        ThisOwn.Topside.Called = 20 * i
        ThisOwn.Topside.Contact = 10 / i
        ThisOwn.Topside.Appnt = 5 - i
        ThisOwn.Topside.PivOpp = 0 + (i / 2)
        ThisPhs.Owner.Add ThisOwn, "Mike" & i
    Next i
    ThisPhs.TestThis "Mike1"
    ThisPhs.TestThis "Mike2"
    ThisPhs.TestThis "Mike3"
    Set ThisOwn = Nothing
    Set ThisPhs = Nothing
End Sub

Shouldn't this give me 3 different sets of numbers?  It only gives me Mike3's numbers for all 3 Mikes...  What am I doing wrong?

Thanks,
Mike
0
 

Author Comment

by:Shadowedvaca
Comment Utility
Oh here are the classes code.

Phs
Option Compare Database
Option Explicit
Private m_Owner As Collection

Public Property Get Owner() As Collection
    Set Owner = m_Owner
End Property

Private Sub Class_Initialize()
    Set m_Owner = New Collection
End Sub

Private Sub Class_Terminate()
    Set m_Owner = Nothing
End Sub

Public Function TestThis(index As Variant) As Integer
    With Me.Owner(index).Topside
        Debug.Print .Called
        Debug.Print .Contact
        Debug.Print .Appnt
        Debug.Print .PivOpp
        Debug.Print .Ca2Co
        Debug.Print .Ca2Ap
        Debug.Print .Ca2Pv
        Debug.Print .Co2Ap
        Debug.Print .Co2Pv
    End With
End Function


Own
Option Explicit
Option Compare Database
Private m_Topside As TpSd

Private Sub Class_Initialize()
    Set m_Topside = New TpSd
End Sub

Private Sub Class_Terminate()
    Set m_Topside = Nothing
End Sub
   
Public Property Get Topside() As TpSd
    Set Topside = m_Topside
End Property

TpSd
Option Compare Database
Option Explicit
Private m_Called As Long
Private m_Contact As Long
Private m_Appnt As Long
Private m_PivOpp As Long

Public Property Get Called() As Long
    Called = m_Called
End Property

Public Property Let Called(NewCalled As Long)
    m_Called = NewCalled
End Property

Public Property Get Contact() As Long
    Contact = m_Contact
End Property

Public Property Let Contact(NewContact As Long)
    m_Contact = NewContact
End Property

Public Property Get Appnt() As Long
    Appnt = m_Appnt
End Property

Public Property Let Appnt(NewAppnt As Long)
    m_Appnt = NewAppnt
End Property

Public Property Get PivOpp() As Long
    PivOpp = m_PivOpp
End Property

Public Property Let PivOpp(NewPivOpp As Long)
    m_PivOpp = NewPivOpp
End Property

Public Function Ca2Co() As String
    Dim Conv As Double
    If Me.Called = 0 Then
        Conv = 0
    ElseIf Me.Contact = 0 Then
        Conv = 0
    Else
        Conv = Me.Contact / Me.Called
    End If
    Ca2Co = FormatNumber((Conv * 100), 2) & "%"
End Function

Public Function Ca2Ap() As String
    Dim Conv As Double
    If Me.Called = 0 Then
        Conv = 0
    ElseIf Me.Appnt = 0 Then
        Conv = 0
    Else
        Conv = Me.Appnt / Me.Called
    End If
    Ca2Ap = FormatNumber((Conv * 100), 2) & "%"
End Function

Public Function Ca2Pv() As String
    Dim Conv As Double
    If Me.Called = 0 Then
        Conv = 0
    ElseIf Me.PivOpp = 0 Then
        Conv = 0
    Else
        Conv = Me.PivOpp / Me.Called
    End If
    Ca2Pv = FormatNumber((Conv * 100), 2) & "%"
End Function

Public Function Co2Ap() As String
    Dim Conv As Double
    If Me.Contact = 0 Then
        Conv = 0
    ElseIf Me.Appnt = 0 Then
        Conv = 0
    Else
        Conv = Me.Appnt / Me.Contact
    End If
    Co2Ap = FormatNumber((Conv * 100), 2) & "%"
End Function

Public Function Co2Pv() As String
    Dim Conv As Double
    If Me.Contact = 0 Then
        Conv = 0
    ElseIf Me.PivOpp = 0 Then
        Conv = 0
    Else
        Conv = Me.PivOpp / Me.Contact
    End If
    Co2Pv = FormatNumber((Conv * 100), 2) & "%"
End Function

Whew... that was too much copy & paste for one day.  :-)

Thanks,
Mike
0
 

Author Comment

by:Shadowedvaca
Comment Utility
Well maybe I posted too soon.  I changed my TestCode to be like below.

Sub TestMe()
    Dim ThisLst As Lst
    Set ThisLst = New Lst
    Dim ThisPhs As Phs
    Set ThisPhs = New Phs
    Dim ThisOwn As Own
    Dim i As Integer
    For i = 1 To 3
        Set ThisOwn = New Own
        ThisOwn.Topside.Called = 20 * i
        ThisOwn.Topside.Contact = 10 / i
        ThisOwn.Topside.Appnt = 5 - i
        ThisOwn.Topside.PivOpp = 0 + (i / 2)
        ThisPhs.Owner.Add ThisOwn, "Mike" & i
        Set ThisOwn = Nothing
    Next i
    ThisPhs.TestThis "Mike1"
    ThisPhs.TestThis "Mike2"
    ThisPhs.TestThis "Mike3"
    Set ThisPhs = Nothing
End Sub

Now why is it acting right now?  When I earse and recreate it everytime it works fine...  I don't understand why this works and the other way doesn't.  Can anyone explain this?

Thanks,
Mike
0
 
LVL 12

Expert Comment

by:dfiala13
Comment Utility
Yes,
In your first try you were just adding the references to the same object three times.  In this instance you were creating a new object each time and then adding it.

Take out the  

Set ThisOwn = New Own

from the loop and you will get it to break again.
0
 

Author Comment

by:Shadowedvaca
Comment Utility
So I can now take this model out and layer as deep as it needs to be!  Awesome!

Thanks for all your help.  I didn't understand about collections before at all.  :-)

Thanks,
Mike
0
 
LVL 12

Expert Comment

by:dfiala13
Comment Utility
You're welcome.  

Have fun!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now