Global Dataset in windows application

J

James Morton

I am writing a c# windows forms application. I read in an XML file to a
dataset for read-only access (by the user and the application logic).

This is not an MDI app but has LOTS of seperate forms. They all need to get
to tables within this DataSet. How can I make the DataSet "Global" for all
forms within the application. I could just read in the file in every form to
a newly instanced dataset (the XML is not that large) but this has become a
rather intriguing design issue for me. It seems their is some "correct" way
to have a DataSet global in memory for the entire application so your not
copying schema and data in client memory for every new form thats opened.

My main thought is to create a class for the dataset, make the dataset
static and declare an instance of the class in all of the forms. I
appreciate any feedback, help, etc.
 
M

Mark Broadbent

if you have the member as static, you do not need to create an instance to
reference it -in fact you shouldnt (and I believe in C# that you cant access
a static member via the instance *but* can in vb -although I would need to
double check that). You access it through it's class.

Anyhow, creating the static member is probably the way to go in this
instance.

--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
 
J

James Morton

Yeah i mis-spoke a little in the previous thread heres my solution please
critque.

1) Make a new class.
public class AppGlobal

{

public static DataSet dsGlobal;

public AppGlobal()

{



}

public static void buildGlobalDataSet()

{

dsGlobal = new DataSet();

dsGlobal.DataSetName = "GlobalDataset";

dsGlobal.Locale = new System.Globalization.CultureInfo("en-US");

dsGlobal.ReadXml("CustomImport.XML");

}

}

2) In your startup forms main method. Note, since i use the data in form1 i
have to fill the dataset before launching the Application.

static void Main()

{

AppGlobal.buildGlobalDataSet();

Application.Run(new Form1());


}



3) Now from any form you can reference the static dataset

dataGrid1.DataSource = AppGlobal.dsGlobal.Tables["Table1"];



I am a little unsure on how memory is handled (not really handled but more
like how its created) since i never once instance the AppGlobal class. I do
instance the dataset in the static buildGlobalDataSet() method.
 
M

Mark Broadbent

Code looks ok to me, public members should ideally be named in Pascal casing
with capitals for abbreviations e.g DSGlobal, BuildGlobalDataSet(). See
http://msdn.microsoft.com/library/d...en-us/cpgenref/html/cpconnamingguidelines.asp

Ideally you could create another static method to call after the Application
loop e.g. CloseGlobalDataSet() and put any clean up code there.
--

--

Br,
Mark Broadbent
mcdba , mcse+i
=============
James Morton said:
Yeah i mis-spoke a little in the previous thread heres my solution please
critque.

1) Make a new class.
public class AppGlobal

{

public static DataSet dsGlobal;

public AppGlobal()

{



}

public static void buildGlobalDataSet()

{

dsGlobal = new DataSet();

dsGlobal.DataSetName = "GlobalDataset";

dsGlobal.Locale = new System.Globalization.CultureInfo("en-US");

dsGlobal.ReadXml("CustomImport.XML");

}

}

2) In your startup forms main method. Note, since i use the data in form1 i
have to fill the dataset before launching the Application.

static void Main()

{

AppGlobal.buildGlobalDataSet();

Application.Run(new Form1());


}



3) Now from any form you can reference the static dataset

dataGrid1.DataSource = AppGlobal.dsGlobal.Tables["Table1"];



I am a little unsure on how memory is handled (not really handled but more
like how its created) since i never once instance the AppGlobal class. I do
instance the dataset in the static buildGlobalDataSet() method.

James Morton said:
I am writing a c# windows forms application. I read in an XML file to a
dataset for read-only access (by the user and the application logic).

This is not an MDI app but has LOTS of seperate forms. They all need to get
to tables within this DataSet. How can I make the DataSet "Global" for all
forms within the application. I could just read in the file in every
form
to
a newly instanced dataset (the XML is not that large) but this has
become
a
rather intriguing design issue for me. It seems their is some "correct" way
to have a DataSet global in memory for the entire application so your not
copying schema and data in client memory for every new form thats opened.

My main thought is to create a class for the dataset, make the dataset
static and declare an instance of the class in all of the forms. I
appreciate any feedback, help, etc.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top