How can I re-use an object created in VS2005 visual form editor among different forms

ivj

New member
Joined
May 31, 2006
Messages
3
Programming Experience
5-10
Ok say I have one form where I create some DataSet (in the visual editor, so it appears at the bottom). Now I want to re-use that exact same instance in a different control, so that I can see it in the visual editor as well (so I can easily bind it to any properties).

How can I achieve that?

Biggest problem is that I can't figure out a way to prevent VS2005 from instantiating any objects you create in the visual editor, so 2 DataSets = two separate instances. Any suggestions? Or am I gonna have to bind ever freaking control by hand?
 
You don't stop the IDE form creating instances of objects you add to a form. The whole point is that they ARE instances, even at design time. You can add typed DataSets to your project just like you can add forms. You right-click the project in the Solution Explorer and select Add -> New Item. You then select DataSet as the item type and MAKE SURE that you give it a descriptive name. DataSet1 is NOT a good name. Now you can design the DataSet visually and it becomes a type in your project. To add an instance of that type to a form you double-click the DataSet in the Toolbox and select the typed DataSet that you designed. That creates an instance of that type in that form.

If you actually want a single DataSet object that is accessible throughout your project then, just like any other type, you have to put it somewhere that is accessible throughout the project. You can use a module but I prefer to use the ApplicationEvents.vb file in 2005. Any public members declared there are accessible through the My.Application object. You have the choice of either creating the DataSet object in that location, or else creating it elsewhere and assigning it to a variable in that location. For instance, let's say that you have declared a public variable named ApplicationData in the ApplicationEvents.vb file and a you've added a DataSet object named FormData to your main form in the designer. In the Load event handler of the form you could do this:
VB.NET:
My.Application.ApplicationData = Me.FormData
That same DataSet object will now be available throughout your app via the My.Application.ApplicationData variable.

Having said all that, having one DataSet for the entire application is not always the best idea. A separate DataSet for each form containing just the data required by that form is often the better choice.
 
Thanks for the reply!

I know how to manually share that object, the whole idea is that I want to use the designer to make it easy to databind all my textboxes to the dataset.

What would be ideal for me is if in my local code I could have MyDataSet, and I could specify some attribute (or somehow hint to VS to let me databind to this object without forcing me to create a new instance of MyDataSet), like this:

[VS05Bindable]
MyDataSet mds;

Is there any way at all? Or do I have to do it all by hand (which means all that effort that VS2005 devs put into the whole visually data-binding thing is wasted)?
 
When you add an object to a form in the designer you are creating an instance, plain and simple. You cannot bind to an object that doesn't exist, so setting up data-binding at design time without creating an instance makes no sense. Just use a separate instance for each form. You don't have to ever populate that instance at run time. You can simply assign the global to the variable and the empty DataSet you created at run time will be discarded. If you use BindingSources as the DataSource for each of your controls then all you have to do at run time is set the DataSource of each BindingSource.
 
Still, with your approach, even though a DataSet is not populated, it is STILL CREATED. An instance of an object, that takes time and memory to create and delete, when we do not use it at all.

Being a C++ programmer that for me is a big no no.
 
Then you will need to create everything in code. As I've said several times, the designer creates an instance and there is no way around that. If you want the designer then you get an instance. If you don't want an instance then you don't use the designer. Simple. Being a C++ programmer you should have no issues writing reams of code.
 
Back
Top