views:

128

answers:

3

Disclaimer: I am new to Winforms.

I need to declare a datatable that I can load with data when the main form loads. I then want to be able to reference the datatable from within events like when a button is clicked etc.

Where/how should I declare this?

A: 

Update: If it is a simple one form app, please check the suggestion by Quarrelsome..

Just Declare as a public property of your Data Access class.

Gulzar
+1  A: 

I'd suggest a private member at the top of the form class meaning it will be accessible throughout the entire form. No need for a public property, unless you have to access it outside of the form but its best to default to private if you are unsure.

Quibblesome
agreed. I was thinking he was having a separate class for data access. best to decouple the logic from UI. anyway, if it is a simple app, your suggestion is fine..
Gulzar
If it was data access then I'd recommend it as a return value of a method call to prevent concurrency issues with having one version that many might use.
Quibblesome
A: 

Hi,

Public 

Class Form3

Private myTable as New DataTable

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

MsgBox(t.Rows.Count)

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

MsgBox(t.Rows.Count)

End Sub


End Class

Thanks

Can someone tell me how I apply the code tages when posting a message?

Saif Khan
select the lines and click "Code Sample" button on toolbar.
Gulzar