472,328 Members | 1,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

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 3260
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: andrewcw | last post by:
I have a class with a number of functions. For some form of errors I wanted to use the MessageBox function of Window.Forms. I want to reuse this...
16
by: ad | last post by:
I have write a clss, say DM.cs, and I save it in a independent directory like c:\MyUtil I have a new project in c:\proj1. When I use VS to add...
5
by: jqpdev | last post by:
Hello all... I'm coming from a Borland Delphi background. Delphi has a specific component called a Data Module. In the designer the Data Module...
4
by: Grigs | last post by:
Hello, I have a fairly simple class that I created and it works great. Basically it remembers a certain amount of strings. When a new one comes...
1
by: Raed Sawalha | last post by:
I have a web application with 100 pages and 30 user controls so we decided to separate some pages and controls into sub projects ( the generated...
0
by: MIGUEL | last post by:
Hi all! Be patient because what I'm going to explain all of you it's more than very strange. I've developed a webservice project that contains...
4
by: Jeremy S. | last post by:
We're in the process of writing a new Windows Forms app and the desktop support folks want for it to be run from a network share. I know it's...
3
by: bbrewder | last post by:
I am interested in hearing how other people have handled sharing resources in multiple projects (for example, a save icon). We have a product...
6
by: Immortal Nephi | last post by:
First class is the base class. It has two data: m_Base1 and m_Base2. Second class and third class are derived classes and they are derived from...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.