David
How about creating a class which does not have to be instantiated in order
to access its methods? That way your user control can simply access the
dataset via a function or property in the class as though it existed within
the user control?
To do this you need to create a static method to initially create the
dataset. This will be called once from form A and will act like a
constructor. Then write a property or function which (once again static)
returns this dataset (called from the user control ).
Note that any variables declared at class level will need to be static
variables if they are to be used by methods which are static. The example
code here will make it all look simpler.
hope it helps...
Kuv
using System;
using System.Data.SqlClient;
using System.Data;
namespace WindowsApplication3
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Class1
{
private static SqlConnection conn;
private static SqlDataAdapter da;
private static DataSet ds = new DataSet();
private static SqlCommand cmd;
// when calling static methods or poperties use classname.methodname i.e.
Class1.CreateDataSet.
public static void CreasteDataSet()
{
conn = new SqlConnection("database=northwind;server=(local); user id =sa;
pwd=;");
da = new SqlDataAdapter();
cmd = new SqlCommand("select * from customers",conn);
da.SelectCommand = cmd;
da.Fill(ds,"Customers");
}
// function to get the dataset
public static DataSet GetDataSet()
{
return ds;
}
// property example to get the dataset but also to set it should the user
control
// happen to change it.
public static DataSet MyDataSet
{
get
{
return ds;
}
set
{
ds = value;
}
}
}
}
"David Adams" <me*****@hotmail.com> wrote in message
news:eL**************@TK2MSFTNGP09.phx.gbl...
Hi,
I am afraid I am asking a very basic question, or just something that is
not possible.
I have a WinForm app that contains a Form (Form A), and then 20-30
UserControls/WinForms that are created by Form A. Form A calls a class to
populate a strongly typed DataSet (contains about 20 tables) that is to be
shared amongst the UC's and other Forms. Right now, I am passing this
DataSet to each and every UserControl/Form via the constructor and then
setting the local strongly typed DataSet reference in each UC/Form to the
parameter passed to it such as:
public UserControl1(dsEmployees ds)
{
this.mdsEmployees=ds;
}
This is getting to be quite tedious and I would much rather have a public
class that I can access the DataSet from Form A and every UC/Form as well.
The class would contain nothing but the DataSet mentioned above along with
a few other variables that I am also having to pass to each UC/Form. I
would like to instantiate this class in Form A, and somehow have it be availble
to each UC/Form without passing through the constructor or public property.
I tried creating a static member of the strongly typed DataSet similar to
a module in VB, but I want the object to be destroyed when Form A closes.
Is there a better solution out there?
Thanks,
Dave