e.g.
Form4 form4
Public Form1(Form4 form4)
{
this.form4=form4
Initializecomponent();
}
I want to draw the attention of our community members to the confusion surrounding the passing of references into form constructors. I am always against passing a reference to the form constructor.
It is important to know what is a Form Constructor before preparing to write a big project.
According to the MSDN article:
The Form Constructor "Initialises a new instance of the Form class"
I think we can all agree on this definition. Knowing this I want to ask is it bad practice to pass a reference to the Form Constructor in large projects? And Is it a Bug To Write A Reference To The Form Constructor?
The reason I ask is because in big projects a particular form is not stand alone. It may have many relationships with other forms up to 10,20 or even 30+. So passing a reference to Form Constructor causes an "overload method error."
mahesh waghelaPosted Jun 22, 2011, 11:56 AM
Now I hereby End my Discussion as there are nobody has time to discuss it. Even I agreed that my discussion is useless.
Thanks Every Body.
Sam HobbsPosted Jun 22, 2011, 11:15 AM
You asked for guidance and we provided it. I don't have time to argue. You need to spend time learning.
mahesh waghelaPosted Jun 22, 2011, 11:06 AM
This is much better way also, but can you tell me that why I have to prefer your three tier solution and why not I have to prefer my own solution?. What is the deference between those two solution?.
mahesh waghelaPosted Jun 22, 2011, 8:52 AM
Sam Hobbs
Agreed with your comments about passing a parameter to Form Constructor is good thing. Now tell me one thing that here in my post of three tier example in where a parameter is passed is it good thing?.
1. If it is Good thing than why not I have to prefer it(my three tier example)?. Why you tell me to looks to other option?.
2. Is there no Action & Reaction of passing a parameter to Form Constructor?. If it is no than why you have to do extra job with "Overload Method Error" between multiple forms?.
3. Is there always dependency of two or three forms in a project?. If So than it is good thing other wise not.
As per my knowledge the forms is not container of data but it is container of controls and the other controls are containing the data. The other controls means textBox/Label/DGV/Listview etc.
Here to pass a parameter in Form Constructor is like Doctor's treatment. Where patients having complaints of fever and he prescribed for it and what happen the fever is gone but the other complaint starting due to reaction of medicine. The same thing goes here that the previous problem solved and the new problem start.
Sam HobbsPosted Jun 21, 2011, 11:24 PM
mahesh waghelaPosted Jun 21, 2011, 11:19 PM
I also do agree with you as why am always against to pass a reference as parameterized at form constructor. and same I am discussed here that what is form constructor and what are you doing with it. it is the bad idea to pass parameter into form constructor.
Sam HobbsPosted Jun 21, 2011, 3:34 PM
A lot of this discussion is the result of the use of forms as a container of data. Forms are a UI and a better design is to separate the data from the UI. That would be a very useful thing for Mahesh and other beginners to understand. The C# IDE seems to have been designed by programmers that are not experienced with object oriented programming; perhaps they are former VB 6 programmers. In C++ Microosft has MFC and the IDE for it supports classes for documents separate from the UI.
Mahesh, look for articles about three-tier architectures. There are similar design methodologies with other names. A three-tier architecture has a UI, a data layer and a business layer. For simple programs, that could be reduced to a UI and data. Try to look for articles in this web site about things like that. I am embarrased to admit that I am unsure of the details of how it is done in C# but I assume that it would be very typical for the data to be passed as a parameter for the form constructor.
VulpesPosted Jun 21, 2011, 2:56 PM
Here's the same code but using (say) a static field to expose a reference to Form1 to the other two forms:
The functionality is the same but it's much simpler and easier to understand.
If you introduce further forms, then they can likewise call:
Form1.Me.Mytext();
without further ado.
mahesh waghelaPosted Jun 21, 2011, 1:11 PM
Vulpes
Here I brings to you a small project of a passing a reference as a parameter of delegates into form constructor:
//Form2 Class:
// form3 Class:
The above project is for a calling MyText() Method of form1 from form2 and form3. and it's works well both forms calling method perfect on button click event.
Now the question is If some other forms may have to contact or have to visit by making of instance than what they have to do they have to pass unnecessary reference even they not have any concern about it. the form2 and form3 relation with other form may be more than 20 or 30 than what you have to do with it?.
VulpesPosted Jun 21, 2011, 8:36 AM
mahesh waghelaPosted Jun 21, 2011, 7:09 AM
I also agree with you as because I have also argue "the global variable are bad?" at "Stack-Overflow" the link is not available to me because it's removed and I have got success about it that it is easiest way to communicate between multiple form and it's not affect at c#. but it's past.
But I am not agree with you at pass parameter at form constructor causing "overload method Error" subject. And You called it a "compiler error" not "run time error". in this case I want to ask you that what is the deference between compiler error and run time error. The compiler error can be debug even run time error also can be debug. So why you give importance to run time error why not to equal?.
If one form has many relation with other forms it may be 10,20 or 30+ and each form having instance of main form about 4 or 5+ than you mean each time I have to pass a reference into form instance and for that I have to create the object of third party form. is it good practice?.
Error is Error than what it may be run time or compiler.
I am not ready to do this type of over loaded work and hence I am against of to do extra job with concern it.
VulpesPosted Jun 20, 2011, 2:05 PM
mahesh waghelaPosted Jun 20, 2011, 12:41 PM
So you are saying to pass a reference to the form constructor is not wrong by below way.
Form4 form4
Public Form1(Form4 form4)
{
this.form4=form4
Initializecomponent();
}
So It's not causing "Overload Method Error" on further instance of it for other relative forms.
and here you arise a new question about to pass static variable. Do you not think about passing global variable are bad. Here I disclose the article which describe how global variable are bad?. The Link is : http://c2.com/cgi/wiki?GlobalVariablesAreBad.
VulpesPosted Jun 20, 2011, 12:01 PM
There is nothing wrong with passing a reference in the constructor or obtaining one in either of the two other methods I've described. Which one is best depends on the circumstances of your application.
If you have a lot of forms that require references to each other, then Application.OpenForms has the merit of not requiring the constructors to be changed or static fields or properties being added to each form. For example, if Form4 requires a reference to Form1, it can get one like this:
Form1 f1 = (Form1)Application.OpenForms["Form1"];
mahesh waghelaPosted Jun 20, 2011, 11:48 AM
May be you wright on your idea. I have no idea about "Internal" Modifier because I think it is not in .Net 2.0 Version and I am using it. hence I have not tested your code.
But once again here I am remind you that passing a reference like above way is it correct?. You have applied here different way than me. It is advance version code hence I can't say anything about it.
Vilas GitePosted Jun 20, 2011, 10:30 AM
Its really nice info for me... which is not explain in brief in my class room.
:)
VulpesPosted Jun 20, 2011, 9:54 AM
mahesh waghelaPosted Jun 20, 2011, 9:25 AM
I am Well-Come Vulpes, Sam Hobbs,Pradeep Chandraker, and Vilas Gite Here we are discussing about form constructor's bug.
I am request all of you to consider my edited question once again. Where I am suggested why I am against of passing a reference to the form constructor. The question is not about how you call form reference to the other forms. but the question is that if you have three form and you want to call the method of particular one form from two forms. And you have passed the reference of forms into form constructor and call it perfectly and it's works well too. Than what happen that if form1 has more relation to other forms and whenever we make the instance of form1 than it's cause to "Overload Method Error".
Sam HobbsPosted Jun 20, 2011, 2:35 AM
I sure don't understand what it is that you think is a bug. Do you think it is a bug because the constructor has a parameter or is it a bug because the constructor is like that or something else?
Vilas GitePosted Jun 20, 2011, 1:20 AM
as per replies of Vulpes sir and pradeep Sir,
There are some rules..........
--------------------------------------
Pradeep ChandrakerPosted Jun 19, 2011, 11:39 PM
The question here is how are you calling Form1? does it has default constructor?
It will be good if you can provide complete code sample which is causing this error.
VulpesPosted Jun 19, 2011, 2:18 PM
The chance of there being a bug in simple Windows Forms code is extremely remote.
However, I can't tell whether there's a bug or not without knowing how you're calling the constructor.
mahesh waghelaPosted Jun 19, 2011, 1:52 PM
vulpes
don't take it lightly it's a serious question. Here I am asking for is it bug or not?
mahesh waghelaPosted Jun 19, 2011, 1:50 PM
VulpesPosted Jun 19, 2011, 10:25 AM
Vilas GitePosted Jun 19, 2011, 10:02 AM
i think you have error description..
"No overload for method 'Form1' takes '0' arguments"
please check this:
http://www.dreamincode.net/forums/topic/68275-no-overload-for-method-mainform-takes-0-arguments/
-------------------------------------------------------------------
If this reply hepls your post…then "MARK AS ANSWER"!