How do I refrence the same dataset in all forms?

  • Thread starter Thread starter Benny Raymond
  • Start date Start date
B

Benny Raymond

I have a program that has a main form and several modeless forms. I'd
like to access the same dataset that is loaded when the main form loads
from all of the forms - How to do this? In vb.net I just used a module
to start up the main form - that module loaded in the dataset - I
couldn't figure out how to use the same method in C#

~Benny
 
Benny Raymond said:
I have a program that has a main form and several modeless forms. I'd like
to access the same dataset that is loaded when the main form loads from all
of the forms - How to do this? In vb.net I just used a module to start up
the main form - that module loaded in the dataset - I couldn't figure out
how to use the same method in C#

~Benny

Could you create a static class to hold the dataset. IT would be available
to all the forms then.
 
you can declare a field with static modifer, like is:
public class ClassName
{
public static DateSet s_dateset;
...
}

In the form, you can access the same dateset throught ClassName.s_dateset.

public class Form1 : System.Windows.Forms.Form
{
...
public void DoSomethin()
{
ClassName.s_dateset
}
}
 
if the dataset is public can't you reference it using something like
frmMain.dataSet?
 
Just create a public class with a static member of type DataSet, along these
lines:

public Globals {
private static DataSet ds = null;
public static DataSet GlobalDataSet {get {return this.ds;}}

public static Globals() {
// Create the DataSet here. This will run the first time you access
// the GlobalDataSet property (or some other static property you
// might define in this class)
}

}

In your code, you simply reference it like this:

DoSomethingWith(Globals.GlobalDataSet);

--Bob
 
Thanks, everyone. I ended up using this (and it's working great!):

public class DataClass
{
private static cpoundtest.Dataset1 s_dataset = null;
public static cpoundtest.Dataset1 GlobalDataSet
{
get
{
if (s_dataset == null)
{
s_dataset = new cpoundtest.Dataset1();
}
return s_dataset;
}
}
}
 
Back
Top