Public Variables

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello

I have a simple C# Windows application with 2 forms. Both forms use data from the same dataset. The problem is that even if I declare the dataset as a public variable in one of the forms, the other cannot use it because of scope. Does anyone know how I can declare the dataset and fill it with data once my application starts, then be able to work with this dataset and update the data in it from any form in the application

Thanks
Boris
 
Boris:

First, don't use Public Variables instead of Properties, they are evil.
http://www.knowdotnet.com/articles/properties2.html

Second, just create them in a class and mark them as static. You can create
a Sealed class and have the DataSet as a Static Public Property and it will
simulate a Module in VB.NET where you can see it an access it from
everywhere in your app. Also, if you do it this way, you don't have to
worry about the form holding the dataset being closed and losing your
Dataset.

YOu may also want to implement this as a Singleton which basically looks
like this:

sealed class PatientInfo
{


public static readonly PatientInfo Instance = new PatientInfo();
public DataSet MyDataSet
{
get {return (m_DataSet);}
set {m_DataSet = value;}
}
}
Boris Ivanov said:
Hello,

I have a simple C# Windows application with 2 forms. Both forms use data
from the same dataset. The problem is that even if I declare the dataset as
a public variable in one of the forms, the other cannot use it because of
scope. Does anyone know how I can declare the dataset and fill it with data
once my application starts, then be able to work with this dataset and
update the data in it from any form in the application?
 
Back
Top