469,934 Members | 1,861 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,934 developers. It's quick & easy.

How can I share a class amongst WinForms?

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
Nov 15 '05 #1
2 3185
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

Nov 15 '05 #2
Hi Kuv,

Thanks for your help. That might just work but I have one reservation in
that once it is opened, it will be held in memory for the lifetime of the
application. This DataSet is strongly typed and contains about 20 tables
with DataRelations. It's huge. I only want it to exist when Form A is
opened, and destroyed when Form A is closed (especially since Form A could
be opened again and a new DataSet would be filled).

Could I do something as simple as this:

public static DataSet MyDataSet

{

get

{
if (ds==null)
{
ds = new DataSet()
FillDataSet(ds)
}
return ds;

}
}

and when Form A closes I could do this:

Class1.MyDataSet=null;

----

Would that work??

Thanks,
Dave


Nov 15 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by andrewcw | last post: by
16 posts views Thread by ad | last post: by
4 posts views Thread by Grigs | last post: by
1 post views Thread by Raed Sawalha | last post: by
3 posts views Thread by bbrewder | last post: by
6 posts views Thread by Immortal Nephi | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.