我有一个具有以下属性的类,该属性由构造函数中的方法生成。
Public Class clsLoadTables
Private _ds As New DataSet
Public Property ds() As DataSet
Get
Return _ds
End Get
Set(ByVal value As DataSet)
_ds = value
End Set
End Property
Sub New()
Try
loadCSVTableII()
loadXMLFiles(pathMainTable, "MainRMDTable")
loadXMLFiles(pathBeneLifeExp, "pathBeneLifeExp")
Catch ex As Exception
MessageBox.Show(ex.Message)
Throw
End Try
End Sub
End Class
我的问题是我不想继承这个类,但我有其他需要访问ds DataSet属性的类。如果可能的话,我想不使用继承,而不是在程序中多次加载我的数据表。
这是我在另一个没有继承clsLoadTables的类中访问该属性的失败尝试:
Dim tableRow As DataRow = ds.Tables("MainRMDTable").Select(String.Format("age={0}", age.ToString()))(0)
关于如何在不使用类继承或全局模块的情况下从程序中只加载一次我想要如何访问此数据集的任何想法?
答案 0 :(得分:3)
您将它作为公共属性,因此如果您有对clsLoadTables类的实例的引用,则应该能够访问它。
Dim foo As New clsLoadTables
Dim tableRow As DataRow = foo.ds.Tables("MainRMDTable").Select(String.Format("age={0}", age.ToString()))(0)
答案 1 :(得分:1)
对于VB.Net中的全局范围,使用带有PUBLIC变量的MODULE和PUBLIC方法或朋友变量,如
Public _ds As New DataSet
或
朋友_ds作为新数据集
希望我没有误解你的问题..
答案 2 :(得分:1)
通常,我为此类做的事情只是将您的班级中的_ds
更改为shared
,并在访问您的媒体资源get
时首次加载数据。