Solved

Array of a Class in a Class

Posted on 2004-03-25
13
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 5
13 Comments
 
LVL 12

Expert Comment

by:dfiala13
ID: 10682203
VB6 or VB.NET?

Have you considered using collections?
0
 

Author Comment

by:Shadowedvaca
ID: 10682212
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
ID: 10682239
Hmm...  can u elaborate on the collections?

Thanks,
Shadowedvaca
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Accepted Solution

by:
dfiala13 earned 250 total points
ID: 10682308

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
ID: 10682390
How is a attached to that?
0
 

Author Comment

by:Shadowedvaca
ID: 10682541
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
 
LVL 12

Expert Comment

by:dfiala13
ID: 10682556
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
ID: 10689063
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
ID: 10689093
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
ID: 10689143
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
ID: 10689258
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
ID: 10689386
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
ID: 10689483
You're welcome.  

Have fun!
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

752 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