Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
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 code:

C#
public class FormA //which would in reality be a form requiring an object containing data for displaying and modifying
{
    CustomDataClass DataObject;    
    public A(ref 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 A(ref 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...
}

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 3:07am
v4

Winston_D, T. wrote:
This does not seem to work.
What do you mean exactly?
Note: you usually don't need the ref keyword while passing objects, because they are already passed by reference (strictly speaking a reference to the object is passed by value) hence, using
C#
public class A 
{
    CustomDataClass DataObject;
    public A(CustomDataClass MyCustomCreatedObject)
    {
        DataObject = MyCustomCreatedObject;
    }
}

when you change DatatObject state you actually change the state of the original object.
 
Share this answer
 
Comments
Winston_D 23-Jan-12 8:30am    
I did not mention it, 'public A(ref CustomDataClass MyCustomCreatedObject)', is what the constructor should read. I need to retrieve and modify data stored in the object across each form to which it is passed as a ref object parameter
CPallini 23-Jan-12 8:49am    
No, you don't need the 'ref' call if you want to modify the object (you only need it if you want modify the reference itself, e.g. substitute the current object -in fact discarded - with a freshly created one).
Winston_D 23-Jan-12 8:39am    
The problem is that if I cannot create an object of class A in my base (main) form. It does not allow me to pass an object into the constructor of A. eg

Base Form
---------

CustomDataClass ProgramData = new CustomDataClass(); // This object has data //set into it by the time I want to create a form from class A.

A SecondForm = new A(ProgramData); // This is where I have the trouble
SteveAdey 23-Jan-12 8:50am    
CPallini is right. You would only pass a ref in for structs normally. You don't want to change the reference to the object, merely the object's contents. So, follow the example CPallini gave and you should be ok.
Sergey Alexandrovich Kryukov 23-Jan-12 23:29pm    
Correct, my 5. Passing reference type by reference is passing a reference to another reference, which is redundant.
--SA
If you declare Constructor as public A(ref CustomDataClass MyCustomCreatedObject), you pass class object with ref keyword.

If you declare Constructor as public A(CustomDataClass MyCustomCreatedObject), you pass class object without ref keyword.

In both cases, the object modified in constructor A will reflect changes in class object MyCustomCreatedObject.
 
Share this answer
 
Comments
CPallini 23-Jan-12 8:52am    
Clearly stated, my 5.
on Form1
-------------
public Form2 objForm2;//Global var
public string testVariable;//Global var

on Form2
------------------
Form1 objForm1=new objForm1();
objForm1.objForm2=this;
objForm1.testVariable='xyz';
 
Share this answer
 

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