Click here to Skip to main content
16,005,552 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am new to this forum and new to .NET.

I am having a hard time creating a Class within a Class. I am not sure if that's even the way to go, so feel free to guide me towards the right way of achieving what i am trying to do.

What i have is 3 classes (HouseClass, StreetClass and SubdivisionClass)

Each SubdivishionClass will store/hold unknowen number of StreetClass. Each StreetClass will hold unknowen number of HouseClass.

When i create a SubdivisionClass i can then add the all the streets to the SubdivisionClass (like: Subdev1.Add("Main Street","two lane street","30 mps")

Then i should be able to add the house to the sreet (like: Subdev1.Add.Street#1.house("house 3","green",5 rooms)


Any advice or samples would be appreciated,

Thank you
Posted

From what you say, I don't think you need to "nest" your classes at all.
Instead, what I would do is keep a collection of the "sub class" within each class:

SubdivisionClass holds a List of StreetClass
StreetClass holds a List of HouseClass
If necessary, each HouseClass would hold a List of RoomClass

This reflects the way it works in the world: A Town is a collection of Divisions, each of which is a collection of streets, each of which is a collection of Houses (or more accurately Addresses because a street may contain empty plots, or offices, shops, factories - all of which need to be allowed for).
 
Share this answer
 
Thank you very much Original Griff. I like the logic a lot. But i am having a hard time understanding on how would i retrieve info on the house if i already have a SubDiv.Street . I have provided the code for viewing.

-----Class ----
Public Class ClassSubD
    Private _SubDName As String

    Private _StreetCollection As New ArrayList

    '--- Constructor 
    Public Sub New(ByVal SubDName As String)
        _SubDName = SubDName
    End Sub

    '--- Method
    Public Sub AddStreet(ByVal Street As ClassStreet)
        _StreetCollection.Add(Street)
    End Sub

    '--- Property
    Public ReadOnly Property NumberOfStreets() As Double
        Get
            Return _StreetCollection.Count
        End Get
    End Property

End Class



Public Class ClassStreet
    Private _StreetName As String
    Private _StreetSpeed As Double

    Private _HouseCollection As New ArrayList

    '--- Constructor 
    Public Sub New(ByVal StreetName As String, ByVal StreetSpeed As Double)
        _StreetSpeed = StreetSpeed
        _StreetName = StreetName
    End Sub

    '--- Method
    Public Sub AddHouse(ByVal House As ClassHouse)
        _HouseCollection.Add(House)
    End Sub

    '--- Property
    Public ReadOnly Property NumberOfHouses() As Double
        Get
            Return _HouseCollection.Count
        End Get
    End Property


End Class


Public Class ClassHouse
    Private _HouseNumber As Double
    Private _HouseDescription As String

    '--- Constructor 
    Public Sub New(ByVal HouseDescription As String, ByVal HouseNumber As Double)
        _HouseNumber = HouseNumber
        _HouseDescription = HouseDescription
    End Sub


End Class


---Main Class---

VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click



    Dim SD As New ClassSubD("Sub Name")

    Dim ST As New ClassStreet("Street One", 52647)

    Dim House As New ClassHouse("One Level with 2 doors", 51485)


    SD.AddStreet(ST)
    ST.AddHouse(House)

    MsgBox(SD.NumberOfStreets)

End Sub
 
Share this answer
 
Comments
Richard C Bishop 22-Aug-13 12:39pm    
Be careful not to post questions/comments as solutions to your own questions. It will cause a paradoxical rift in the space-time continuum!
Trak4Net 22-Aug-13 13:38pm    
Sounds like you need a way to find the street again so you can add more houses? I would recommend setting up some linq queries to find if street already exists.
Here is some sample though not exactly what you are trying to do.

<pre>

Public Class SubDivision


Dim _streets As New List(Of Street)

'get list of all streets where MPS is 30
Public Function GetAllStreetsFor30MPS() As IEnumerable(Of Street)
Return (From s In _streets Where s.MPS = 30 Select s)
End Function

Public Property Streets As List(Of Street)
Get
Return _streets
End Get
Friend Set(value As List(Of Street))
_streets = value
End Set
End Property

Public Function AddStreet(ByVal strt As Street) As Street
_streets.Add(strt)
Return strt
End Function

End Class

Public Class Street

Public Property MPS As Integer

Dim _houses As New List(Of House)

Public Property Houses As List(Of House)
Get
Return _houses
End Get
Friend Set(value As List(Of House))
_houses = value
End Set
End Property

Public Function AddHouse(ByVal hs As House) As House
_houses.Add(hs)
Return hs
End Function

End Class

Public Class House

Public Property Color As String
Public Property Number As Integer

End Class
</pre>
juno101 22-Aug-13 16:57pm    
Thank you very much Trak4Net, i am learning new things.

It's very interesting how you handled getting into another object. I am still trying to understand the function
<pre lang="sql">Public Function GetAllStreets() As IEnumerable(Of Street)
Return (From s In _streets Where s.MPS = 30 Select s)
End Function</pre>
Trak4Net 22-Aug-13 17:21pm    
That is Linq. I would really recommend getting a book or going through some tutorials on it!
It is basically the below example simplified, sometimes saving alot of lines of code. Linq is very powerful and useful for a lot of objects that inherit IEnumerable. I mostly code in C# and Linq with lambda expressions are so much easier for me than in VB. I hope the example below helps you understand the Linq I used, but again, if you aren't familiar with Linq I would recommend researching it when you can. I didn't test my code below just typing it into the comment box so hopefully it is correct :)

<pre lang="vb">

Public Function GetAllStreets() As IEnumerable(Of Street)
'collection to store Street object that matches 30MPS
Dim streetsResult As New List(Of Street)
'Iterate collection and look for Street that has 30MPS and add to our temp collection
For Each _street As Street in _streets
If (_street.MPS = 30) Then
streetResults.Add(_street)
End If
Next

Return streetResults.AsEnumerable(Of Street) 'return our collection as IEnumerable(Of Street)
End Function
</pre>
juno101 22-Aug-13 17:53pm    
Thank you very much Trak. I have several dotNet books but they are loaded with so much information that it just gets overwhelming. I will read up on Linq tonight.

Thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900