Click here to Skip to main content
15,942,847 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

There is a multi-form program. In some of these forms, a structure should be used. Not knowing what to do, I put the definition of the structure in all forms in which there was a variable of that structure type. I think there should be a better way of putting the definition of the structure in one form or file and supporting all other forms needing the structure's definition. If the suggestion sounds be working, Could you please let me know how I could implement it?

Thanks a lot

It is not a good idea to re-define your structure in each form: If you change one, you must remember to change the rest. Also, they cannot share instances between forms if you need to.

If the program is a single project, then create a new class as usual via Solution ExplorerL Right click the project name, then select "Add..New Class" and give it the name of your structure - this will create a separate file called YourStructure.cs
Change the class definition to struct and fill in the details.
Each class within your project will then be able to access the structure definition.
You may want to make the struct internal to limit access to the project namespace.

If each form is in a separate project, then create a new project called "Utilities" or similar as a DLL and include your structure in that. You can then add a reference to Utilities in each form project. In this case, you will want the struct to be defined as public rather than internal
Share this answer
ahhashemi 22-Aug-10 4:27am    
Reason for my vote of 5
I found it Very informative and explanatory, written step by step
You should create a class file with the definition of that structure.
In your form set a reference to it by using ns.class;.
Now you can define variables of that type.

Share this answer
ahhashemi 22-Aug-10 6:10am    
Reason for my vote of 4
Straightforward directions. I found out that there is no need to set a reference to the class. My Class definition was as follows:
namespace MyProject
class MyClass
public struct MyStruct
public int x;
public int y;

Then with the code below, one can readily define a variable of the structure user defined:
MyClass.MyStruct MyVariable = new MyClass.MyStruct();
MyVariable.x = 6;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900