473,320 Members | 2,024 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Accessing the same data from multiple forms using C#

I am in need of some guidance on asscessing the same data from multiple
forms. I tried using a collection, but everytime that I try to access the
collection, I have to use the new word.
colStuff ns = new colStuff
When I try to access the items in the collection, they are not there (ie the
values from a different form).

Could somebody point me in the right direction to get this working?

Thanks,
Sam Berry
Nov 16 '05 #1
1 6105
You have two decisions to make here, not just one.

First, how are all of the forms that need the common data going to get
at the common data?

Second, in what form is it most convenient to store the common data?

Worry about the first problem first. That is, don't worry about whether
to use a collection or a specialized class or whatever. First figure
out how all of your forms are going to get at the common data.

If there is only ever going to be one copy of your common data for any
given run of your program, then you have two choices for providing
universal accessibility: using a static property, or creating a
singleton class.

The static property is the simplest solution. You can put the static
property in any class, but usually you would put it in your main class,
the one that starts up your application. It would look something like
this:

public class MyApplication
{
private static colStuff myData = null;

public static int Main(string[] argv)
{
MyApplication.myData = new colStuff(...);
}

public static colStuff Data
{
get { return MyApplication.myData; }
}
}

Now you can just say MyApplication.Data from any form and you will get
the collection.

The singleton option is a bit more complicated, but has some advantages
to do with polymorphism. It looks something like this:

public class colStuff
{
private static colStuff singleton = null;

private colStuff(...)
{
}

public static colStuff Instance
{
get
{
if (colStuff.singleton == null)
{
colStuff.singleton = new colStuff(...);
}
return colStuff.singleton;
}
}
}

Now you can say colStuff.Instance from anywhere in any form, and if
there is not already a colStuff then it will make a new one and return
it. If one has already been created then it will return that.

Once you've decided which method you want to use, you can then store
your common data using any structure you like.

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Carlos Ribeiro | last post by:
Hello all, I'm posting this to the list with the intention to form a group of people interested in this type of solution. I'm not going to spam the list with it, unless for occasional and...
1
by: Chris Beach | last post by:
Hi, I have a JSP page with several forms on it. Some of these forms are generated dynamically, and each of them submits some information to a database. Handling one form is easy, as I can...
2
by: mattias | last post by:
I have an app using two forms. No problem getting form1 to open and accessing public controls on form2, but how do i get form2 to access a TextBox on form1? I think I have included Form2.h...
0
by: sonu | last post by:
I have following client side code which i have used in my asp.net project SummaryFeatured Resources from the IBM Business Values Solution Center WHITEPAPER : CRM Done Right Improve the...
7
by: Jeff | last post by:
I plan to write a Windows Forms MDI application for a medical office. Users must be able to select a patient and view related information on multiple forms; with1-4 forms opened at the same time...
0
by: dtrudell75 | last post by:
Hi there, I'm running Visual Basic .Net 2003 and developing a windows mobile 2003 application. I'm using the following code to create and dispose of forms, but I'm running into a Stack...
5
by: c676228 | last post by:
Hi everyone, my colleagues are thinking about have three insurance plans on one asp page: I simplify the plan as follow: text box:number of people plan1 plan2 plan3
19
by: Zytan | last post by:
I want multiple instances of the same .exe to run and share the same data. I know they all can access the same file at the same time, no problem, but I'd like to have this data in RAM, which they...
2
by: =?Utf-8?B?VGFtbXkgTmVqYWRpYW4=?= | last post by:
Hi, I am going to write a large application using Visual Studio C#. I am going to use only one Form as main menu and go to other pages by cliking on next button in each page. I dont want to create...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.