Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I have got a class that when created, I need to have another object passed to it.
I have got an application with multiple forms. I have a base form from which all other forms are created and destroyed as need be during runtime... At the moment, my code is clumsy as I used a 'DataObject' with all its internal variables declared as static so that with all the copies of the same object I have in each form, the same data would be used. I know very well that this is NOT good programming but this seems to be a temporary solution around my problem of trying to use the same 'DataObject' in each form. Here is an abbreviated copy of my suggested code that I am trying to use:
C#
public class FormA //which would in reality be a form requiring an object containing data for displaying and modifying
{
    CustomDataClass DataObject;    
    public FormA(CustomDataClass MyCustomCreatedObject)
    {
        DataObject = MyCustomCreatedObject;
    }
    //with all other code related to the form...
}
 
public class FormB //which would in reality be a form requiring an object containing data for displaying and modifying
{
    CustomDataClass DataObject;    
    public FormB(CustomDataClass MyCustomCreatedObject)
    {
        DataObject = MyCustomCreatedObject;
    }
    //with all other code related to the form...
}
//...
//However many forms I will be using...
//...

public class MainForm
{
    CustomDataClass ApplicationData = new CustomDataClass();
    //some code for populating the ApplicationData object with data etc...
    FormA FirstAdditionalForm = new FormA(ApplicationData);
    FormB SecondAdditionalForm = new FormB(ApplicationData);
    //until the number of required forms has been reached...
    
    //on a certain button click method in the MainForm:
    FirstAdditionalForm.ShowDalog();
    //User does what is needed on the form and closes it...
    
    //on a certain button click method in the MainForm:
    SecondAdditionalForm.ShowDalog();
    //User does what is needed on the form and closes it...
}

My Problem:
This does not seem to work when I try to create the two forms from their respective classes. I am not able to pass the 'ApplicationData' object in as a parameter into their constructors and if I do, I have build/compile errors etc.

Where am I going wrong or what is a better way of creating a new form class and passing a data object to it as a ref? This is for a multiple forms application needing data to be transferred between forms using the same data object. I hope this clarifies my problem. Please help...
Posted
Updated 23-Jan-12 20:54pm
v7

Here are the bugs:

The declaration public A is not a valid method as it does not declare a return type. It could be a valid constructor (which never declared return type because it always returns the instance of the declaring class or structure), but in this case you should give it the same name as the class. If you change public A to public FormA and public FormB, it will work.

You need to remove "ref" from the parameter of the class types. Formally, you can use passing by reference for the parameters of reference types (the class in your case), but this is simply silly. This means passing a reference by reference, double referencing (indirection), which makes no sense.

When you pass an object of a reference type (like a class) by reference, it will perfectly serve you purpose. In your case: you will have to form instances holding different references to the same object of CustomDataClass. If you modify data through one form, it will be modified in another form, as this is the same object albeit referenced by two different references. I think this is what you wanted.

It looks like if you make these two fixes you are good to go.

—SA
 
Share this answer
 
Comments
Winston_D 24-Jan-12 2:54am    
I corrected the attached copy of my code, fixing the mistakes you pointed out however it does not fix my problem of 'ApplicationData' not being able to be passed into the constructors of the respective additional forms inside the main form. That is where my problem persists...
You're trying to construct the FirstAdditionalForm and SecondAdditionalForm in the object initializer. Move the new statements to your constructor for MainForm
 
Share this answer
 
v2

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