VB:如何创建嵌套类? [英] VB: How do I create nested classes?

查看:281
本文介绍了VB:如何创建嵌套类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在自己的现有类中编写一个嵌套类。但是我找不到方法,因为我不知道这是怎么称呼的。

I would like to write a nested class into an existing class of my own. But I can't find how because I have no idea how this is really called.

嵌套类是什么意思?使用 DataTable 类中的表 dt ,我可以编写 dt.Columns.add( ) Columns 是主类的属性, add 是嵌套类的方法。

What do I mean by nested class? With a table dt from the DataTable class, I can write dt.Columns.add(). Columns would be property of the main class and add would be a method from a nested class.

有什么建议吗?

推荐答案

那不是嵌套类,它只是一个类。 Columns属性的类型为 DataColumnCollection ,它具有名为 Add 的公共方法。要以类似的方式构建自己的数据库,只需:

That is not a nested class, it's simply a class. The Columns property is of the type DataColumnCollection that has a public method called Add. To build your own in a similar fashion it would simply be:

Public Class MyFirstClass

    Public Sub New()

    End Sub

    Dim _second As New MySecondClass()
    Public Property Second() As MySecondClass
        Get
            Return _second
        End Get
        Set(ByVal Value As MySecondClass)
            _second = Value
        End Set
    End Property
End Class

Public Class MySecondClass
    Public Sub New()
    End Sub

    Public Sub MySecondClassMethod()
        'Do something
    End Sub
End Class

然后将在其他一些类或功能中调用它,例如:

This would then be called in some other class or functionality like:

Dim x as New MyFirstClass()
x.Second.MySecondClassMethod()

这篇关于VB:如何创建嵌套类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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