Strong-Typing? Or Nested Collections

A

Atley

I am trying to set up a nested collection so I can run statements like this:

me.lblquestions.text = mysteps.item(1).questions(1).Asked

Any ideas on how to do this? I have created a Class(Steps) with a nested
Class(Questions) inside of it and a Collection of that Class(Steps) and it
works fine, but the Nested Collection(Questions) is escaping me.

Help!


My Code:





Public Class Steps
'Info Out
Public SOutTitle as string
Public SOutName as string
Public SOutDesc as string

Public SQuestions As QuestionsCol

End Class



Public Class Questions
Public Title As String
Public Type As Integer
Public Answer ' Variant
Public WControl As Integer
End Class



Public Class StepsColl
Inherits System.Collections.CollectionBase

Public Sub Add (ByVal aStep as Steps)
list.add(aStep)
End Sub

Public Sub Remove (ByVal index as Integer)
If Index > Count or Count < 1 Then
Msgbox ("Index is not valid")
Else
list.remove(aStep)
End If
End Sub

Public ReadOnly Property Item (ByVal index as Integer) As Steps
Get
If Index > Count or Count < 1 Then
Msgbox ("Index is not valid")
Else
Return CType (list.Item(Index), Steps)
End If
End Get
End Sub
End Class



Public Class QuestionsColl
Inherits System.Collections.CollectionBase

Public Sub Add (ByVal aQuestion as Questions)
list.add(aQuestion)
End Sub

Public Sub Remove (ByVal index as Integer)
If Index > Count or Count < 1 Then
Msgbox ("Index is not valid")
Else
list.remove(aStep)
End If
End Sub

Public ReadOnly Property Item (ByVal index as Integer) As Questions
Get
If Index > Count or Count < 1 Then
Msgbox ("Index is not valid")
Else
Return CType (list.Item(Index), Questions)
End If
End Get
End Sub

End Class


Public MySteps as New StepsColl


Public Function PopulateSteps
Dim X As Integer
Dim Y As Integer

StepCount = 5

For X = 0 to StepCount -1
Dim CurrStep As New Steps

CurrStep.SOutName = "Drawings Start Date" & X + 1
''''
' I Cannot Figure out how to make the nested questions 1-5 for this step and
make it stick. HELP!
''''

MySteps.Add(CurrStep)
CurrStep = Nothing
Next X



End Function
 
J

Jay B. Harlow [MVP - Outlook]

Atley,
In the Steps class you need to create an instance of the QuestionsColl:
Public Class Steps

Public Readonly Questions As New QuestionsColl
End Class


I would make the Item properties Default, to allow not having to type Item
(not actually part of the problem):
Public Class StepsColl
Inherits System.Collections.CollectionBase

Default Public ReadOnly Property Item (ByVal index as Integer) As Steps
End Class
Public Class QuestionsColl
Inherits System.Collections.CollectionBase
Default Public ReadOnly Property Item (ByVal index as Integer) As Questions

End Class

Just create a question and add it to the currstep.questions collection:
''''
' I Cannot Figure out how to make the nested questions 1-5 for this step and
make it stick. HELP!
''''

MySteps.Add(CurrStep)

Dim CurrQuestion As New Questions
CurrStep.Questions.Add(CurrQuestion )
CurrStep = Nothing

Also I would consider adding constructors (Sub New) to your Steps &
Questions classes.

Hope this helps
Jay
 
A

Atley

Why did you make the Questions readonlu in the Steps Class?

Do I add the CurrQuestions to the CurrStep.Questions collection and then
Add the CurrSteps to the MySteps collection, because it looked like you were
doing the opposite?
 
J

Jay B. Harlow [MVP - Outlook]

Atley,
Why did you make the Questions readonlu in the Steps Class?
Questions the collection is immutable, once it is set (when you create the
Steps object) the collection itself cannot be replaced. You can however
still modify what the collection contains.

In other words it prevents the collection itself from being replaced!
Do I add the CurrQuestions to the CurrStep.Questions collection and then
Add the CurrSteps to the MySteps collection, because it looked like you were
doing the opposite?
No!

I'm adding the CurrStep to the MySteps,

then I am adding a single CurrQuestions to CurrSteps. If you want to add
multiple questions, you would have a loop above instead of just the two
lines above:
Next

However! the order doesn't really matter, as long as you add the items to
the respective collection.

Hope this helps
Jay
 
A

Atley

So this will allow me to have a collection of several different steps with
several different questions?
 
J

Jay B. Harlow [MVP - Outlook]

Atley,
Yes.

Are you still having problems?

Hope this helps
Jay
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top