从一个更高级别的信息?别的、高级、信息

2023-09-05 02:18:09 作者:喜你成疾

我真的不知道如何字的问题,所以请多多包涵......

I don't know really how to word the question so please bear with me...

我有3类:服务器,数据库和表。每个类都有一个名称属性。我多么希望它的工作是每个服务器可以有多个数据库,每个数据库可以有多个表。所以在服务器级别我有这个属性。

I have 3 classes: Server, Database, and Table. Each class has a "Name" property. How I want it to work is that each server can have multiple databases and each database can have multiple tables. So in the Server class I have this property.

Private _databases As List(Of Database)
Public Property Databases() As List(Of Database)
    Get
        Return _databases
    End Get
    Set(ByVal value As List(Of Database))
        _databases = value
    End Set
End Property

和我在数据库类的表类似的​​东西。这工作得很好,因为我可以做这样的事情来获得服务器中的所有数据库。

And I have something similar in the Database class for the tables. This works fine now because I can do something like this to get all the databases in the server.

For Each db In s.Databases 's being the server object
        Debug.Print(db.Name)

    Next

我想扩大这些类。我希望服务器类来处理所有的连接信息,我想其他类使用服务器类的连接信息在其中。

I would like to expand these classes. I want the server class to handle all the connection information and I would like the other classes to use the server class's connection information in them.

例如,我安装一个服务器类,并设置连接字符串到服务器。然后,我希望数据库类使用serverclass.connectionstring属性来连接到服务器,并得到所有数据库的列表。但是,我要保留code。在数据库类。我怎样才能做到这一点?

For example, I setup a server class and set the connection string to the server. Then I want the database class to use serverclass.connectionstring property to connect to the server and get a list of all the databases. But I want to keep that code in the database class. How can I do this?

我已经附加了什么,我想要做一些code。

I've attached some code of what I want to do.

Public Class Server

Private _name As String
Public Property Name() As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
    End Set
End Property


Private _databases As List(Of Database)
Public Property Databases() As List(Of Database)
    Get
        Return _databases
    End Get
    Set(ByVal value As List(Of Database))
        _databases = value
    End Set
End Property
End Class

'-----New class  

Public Class Database

Private _name As String
Public Property Name() As String
    Get
        Return _name
    End Get
    Set(ByVal value As String)
        _name = value
    End Set
End Property


Private _tables As List(Of Table)
Public Property Tables() As List(Of Table)
    Get
        Return _tables
    End Get
    Set(ByVal value As List(Of Table))
        _tables = value
    End Set
End Property

'This is where I need help!
Private Sub LoadTables () 
    dim connectionstring as string = server.connectionstring 'Possible?

    'Do database stuff
End Class

感谢您的阅读!

Thanks for reading!

推荐答案

您将有中提及到所有者类的子类。

You'll have to include a reference to the "owner" class in your child class.

如果不使用code(为了简洁起见),你现在所拥有的

Without using code (for the sake of brevity), you have right now

Server
----------
Databases   -----> Database
Name               ------------
                   Tables       -----> Table
                   Name                --------------
                                       Name

您的什么想的是这样的:

Server
----------
Databases   -----> Database
Name               ------------
  ^                Tables       -----> Table
  |                Name                --------------
   --------------- Server              Name
                     ^                 Database
                     |-------------------|

数据库服务器属性类和数据库表格类属性的本意是参考各拥有它们的实例。

The Server property on the Database class and the Database property on the Table class are intended to be references to the instance of each that owns them.