Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Array of a Class in a Class

Posted on 2004-03-25
13
Medium Priority
?
310 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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 12

Accepted Solution

by:
dfiala13 earned 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

916 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