Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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;

VB.NET
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



However, I want to save the DataTable as a CSV file in another Class as follows;

VB.NET
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 


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

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[^]

[EDIT]
On the second look...
The definition of New_Yellow_Ball_Competition class is wrong:
VB
Public YBResultsTable As DataTable = New DataTable()
Private Sub 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.
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 8-Jan-16 14:18pm    
I would better remove everything about global variables. Most good developers quite reasonably consider them evil...
—SA
Maciej Los 8-Jan-16 14:22pm    
Done!
Sergey Alexandrovich Kryukov 8-Jan-16 16:08pm    
5ed then. :-)
It's a pretty wide look at the "problem".
—SA
Maciej Los 8-Jan-16 16:12pm    
Thank you, Sergey.
And thank you for your collaboration.
Wendelius 8-Jan-16 14:19pm    
Same idea at the same time :) +5
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)[^]
 
Share this answer
 
Comments
Maciej Los 8-Jan-16 14:23pm    
Good point, a 5!
Dave the Golfer 8-Jan-16 16:13pm    
I decided on CSV rather than XML because each set of data records are the results of a golf competition. There are going to be a lot of competitions and I was concerned that the loading of the xml into a DOM would start to become a slow activity.
I plan to use a csv file for each competition with the file name related to the date of the competition just incase I need to recall the results at a later date.
Solved the problem by reading some notes on the internet. I need to include the class name that the DataTable is declared in. So
C#
Rfc4180Writer.WriteDataTable(YBResultsTable, writer, True)

becomes
C#
Rfc4180Writer.WriteDataTable(New_Yellow_Ball_Competition.YBResultsTable, writer, True)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900