vb.net中的类中的类 [英] class within a class in vb.net

查看:71
本文介绍了vb.net中的类中的类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我是这个论坛的新手,也是.NET的新手。



我很难在一个类中创建一个类。我不确定这是不是要走的路,所以请随时引导我走向正确的方式来实现我想做的事情。



我有3个课程(HouseClass,StreetClass和SubdivisionClass)



每个SubdivishionClass将存储/持有未知数的StreetClass。每个StreetClass都会保存未知数量的HouseClass。



当我创建一个SubdivisionClass时,我可以将所有街道添加到SubdivisionClass(如:Subdev1.Add(Main)街道,双车道街道,30 mps)



然后我应该可以将房子添加到sreet(如:Subdev1.Add.Street #1.house(房子3,绿色,5个房间)





任何建议或样品将不胜感激,< br $> b $ b

谢谢

解决方案

从你说的话来看,我觉得你不需要窝你的课程。

相反,我要做的是在每个班级中保留一个子课的集合:



SubdivisionClass持有StreetClass列表

StreetClass持有HouseClass列表

如有必要,每个HouseClass将持有RoomClass列表



这反映了它在世界上的运作方式:一个城镇是一个部门的集合,e其中一个是街道的集合,每个都是房屋的集合(或更准确地说地址,因为街道可能包含空地块,办公室,商店,工厂 - 所有这些都需要被允许)。

非常感谢Original Griff。我非常喜欢这个逻辑。但我很难理解如果我已经有SubDiv.Street如何检索房子的信息。我提供了查看的代码。



-----上课----

公共课ClassSubD 
Private _SubDName As String

Private _StreetCollection As New ArrayList

'---构造函数
Public Sub New(ByVal SubDName As String)
_SubDName = SubDName
End Sub

'---方法
Public Sub AddStreet(ByVal Street As ClassStreet)
_StreetCollection.Add(Street)
结束子

'---房产
Public ReadOnly Property NumberOfStreets()As Double
Get
Return _StreetCollection.Count
End Get
结束物业

结束等级



公共类ClassStreet
私有_StreetName为字符串
私有_StreetSpeed为双

Private _HouseCollection As New ArrayList

'---构造函数
Public Sub New(ByVal StreetName As String,ByVal Stree tSpeed As Double)
_StreetSpeed = StreetSpeed
_StreetName = StreetName
End Sub

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

'---房产
Public ReadOnly Property NumberOfHouses()As Double
Get
返回_HouseCollection.Count
结束获取
结束物业


结束类


公共类ClassHouse
私人_HouseNumber As Double
Private _HouseDescription As String

'---构造函数
Public Sub New(ByVal HouseDescription As String,ByVal HouseNumber As Double)
_HouseNumber = HouseNumber
_HouseDescription = HouseDescription
结束次级


结束等级





- - 主等级---



 私人  Sub  Button1_Click( ByVal  sender  As  System。 Object  ByVal  e  As  System.EventArgs)< span class =code-keyword>句柄 Button1.Click 



Dim SD < span class =code-keyword> As ClassSubD( 子名

Dim ST As ClassStreet( Street One 52647

Dim House 作为 ClassHouse( 一级有2个门 51485


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

MsgBox(SD.NumberOfStreets)

End Sub


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

解决方案

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).


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---

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


这篇关于vb.net中的类中的类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆