如何在多个类中提供数据表 [英] How to make a datatable available in more than one class
问题描述
这是我第一次使用CSV文件来保存我的数据。
我在我的一个类中创建了一个DataTable,如下所示;
This is the first time I have used CSV files to hold my data.
I have created a DataTable in one of my classes as follows;
Public Class New_Yellow_Ball_Competition
Public YBResultsTable As DataTable = New DataTable()
Private Sub ResultsTable()
YBResultsTable.Columns.AddRange(New DataColumn() {
New DataColumn("Date", GetType(Date)),
New DataColumn("TeamNumber", GetType(Integer)),
New DataColumn("Player1", GetType(String)),
New DataColumn("P1Handicap", GetType(Integer)),
New DataColumn("P1Hole1", GetType(Integer)),
New DataColumn("P1Stab1", GetType(Integer)),
New DataColumn("YB1Lost", GetType(Boolean)),
New DataColumn("P1Hole2", GetType(Integer)),
.... some 230 plus columns
})
End Sub
End Class
但是,我想将DataTable保存为另一个类中的CSV文件,如下所示;
However, I want to save the DataTable as a CSV file in another Class as follows;
Public Class MainMenu
Private Sub YellowBallNew_Click(sender As Object, e As EventArgs) Handles YellowBallNew.Click
New_Yellow_Ball_Competition.MdiParent = Me
New_Yellow_Ball_Competition.WindowState = FormWindowState.Maximized
New_Yellow_Ball_Competition.Show()
End Sub
Private Sub YellowBallClose_Click(sender As Object, e As EventArgs) Handles YellowBallClose.Click
Using writer As StreamWriter = New StreamWriter("dump.csv")
Rfc4180Writer.WriteDataTable(YBResultsTable, writer, True)
End Using
End Sub
End Class
但我收到错误说明
未声明YBResultsTable。由于其保护级别,它可能无法访问。
那么如何让MainMenu类中的DataTable可用?
代码工作提供它是从创建DataTable的类中调用的。
but I get an error stating
YBResultsTable is not declared. It maybe inaccessible due to its protection level.
So how do I get the DataTable to be available in the MainMenu class?
The code works providing it is called from the class the DataTable was created in.
推荐答案
实现这一点的方法很少。最好的方法是创建界面 [ ^ ]。这取决于你想要达到的目标。
更多:
演练:创建和实现接口(Visual Basic) [ ^ ]
在类和接口之间选择 [ ^ ]
创建类在Visual Basic .NET中 [ ^ ]
第二次看...
<$的定义c $ c> New_Yellow_Ball_Competition 类错误:
There's few ways to achieve that. The best way to achieve that is to create Interface[^]. It depends on what you want to achieve.
More:
Walkthrough: Creating and Implementing Interfaces (Visual Basic)[^]
Choosing Between Classes and Interfaces[^]
Creating Classes in Visual Basic .NET[^]
On the second look...
The definition ofNew_Yellow_Ball_Competition
class is wrong:
Public YBResultsTable As DataTable = New DataTable()
Private Sub ResultsTable()
在第一行中,您已声明公共成员 YBResultsTable
类型 DataTable
,但它从未正确启动(它确实不包含任何列)。您应该在类构造函数( Public Sub New()
)中调用 ResultsTable
过程来添加列。
还有其他一些可能的错误。
In the first line you have declared public member YBResultsTable
type of DataTable
, but it has been never properly initiated (it does contain none columns). You should call ResultsTable
procedure in class constructor (Public Sub New()
) to add columns.
There's few other possible errors.
关于这个问题,你至少有两个选择,你可以在一个模块中声明数据表,以便它是可见的,或者您可以将New_Yellow_Ball_Competition类的现有实例传递给MainMenu类。如果在程序中广泛使用单个数据表,则使用模块是有意义的,请参阅模块声明 [ ^ ]和类与模块 [ ^ ]。
其他一些观察结果:
- 您似乎只有一个包含数据的表,请考虑规范化以实现易于维护的数据结构,请参阅数据库规范化 - 维基百科,免费的百科全书 [ ^ ]
- 如果规范化数据并最终使用多个数据les,使用单个数据集来保存所有数据表。
- 为什么选择CSV?数据表本身支持读取和写入XML格式的数据,请参阅 DataTable.WriteXml方法(字符串)(System.Data) [ ^ ]
Concerning the question, you have at least two choices, you can declare the datatable in a module in order for it to be visible or you can pass an existing instance of the New_Yellow_Ball_Competition class to MainMenu class. If the single datatable is used widely in the program, it would make sense to use a module, see Module Statement[^] and Classes vs. Modules[^].
Some other observations:
- You seem to have only one table containing the data, consider normalization to achieve an easily maintainable data structure, see Database normalization - Wikipedia, the free encyclopedia[^]
- If you normalize the data and end up using several datatables, use a single dataset to hold all the data tables.
- Why CSV? data table natively supports reading and writing XML formatted data, see DataTable.WriteXml Method (String) (System.Data)[^]
通过阅读互联网上的一些注释解决了这个问题。我需要包含声明DataTable的类名。所以
Solved the problem by reading some notes on the internet. I need to include the class name that the DataTable is declared in. So
Rfc4180Writer.WriteDataTable(YBResultsTable, writer, True)
变为
becomes
Rfc4180Writer.WriteDataTable(New_Yellow_Ball_Competition.YBResultsTable, writer, True)
这篇关于如何在多个类中提供数据表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!