473,387 Members | 1,892 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Passing information between Forms--is there any other way than usingparametized constructors?

This thread is about how variables or parameters (including objects)
are passed between forms (namely, using parameterized constructors,
e.g., to pass an int between forms (e.g., a calling form and a called
dialog form) (see below).

My question is that though this works fine, and is consistent with
everything I learned in C++, is there a 'better' way to pass
information, including member variables, objects, and the like, other
than using a parametized constructor as below?

Just to clarify: I don't believe a global variable is a 'better' way,
so that way is excluded.

RL

====calling form, has this somewhere in an event trigger procedure
where you want to call the called form:

int kay = myClass.SomeProperty;

Form2MyWinForm myForm01 = new Form2MyWinForm(kay); //a
parametized constructor, since parameter int 'kay' is passed

myForm01.Show(); //shows the called form

====called form (dialog), has this parametized normal constructor:

public partial class Form2MyDialogBox : Form
{
int F2MDBint;

public Form2MyDialogBox(int j) //parameterized instance
constructor, takes an 'int', which is therefore passed from calling to
called form.
{
InitializeComponent();
F2MDBint = j;
}
///

Jul 7 '08 #1
6 1910
Create a public property on the child form you access from the parent.

public partial class Form2 : Form {
int _myInt;

public Form2() {
this.Initialize();
}

public int MyInt {
get { return this._myInt; }
set { this._myInt = value; }
}

public string MyText {
get { return this.myTextBox.Text; }
set { this.myTextBox.Text = value; }
}
}

Example of the code that would create the form:

Form2 frm = new Form2();
frm.MyInt = 111;
frm.MyText = "Hello world!";
frm.Show();

I'm not a huge fan of creating gigantic constructors. I usually limit the
arguments on a constructor to those absolutely necessary for the object to
function correctly. If the above example must have MyInt to work correctly,
I'd create an argument for it in the constructor, otherwise I'd leave it as
a property. Also, if you're need to update a control on the child form from
an exposed property you can do so quite easily so long as your object calls
the Initialize method on the form in the constructor (if you're using Visual
Studio).

"raylopez99" <ra********@yahoo.comwrote in message
news:39**********************************@y21g2000 hsf.googlegroups.com...
This thread is about how variables or parameters (including objects)
are passed between forms (namely, using parameterized constructors,
e.g., to pass an int between forms (e.g., a calling form and a called
dialog form) (see below).

My question is that though this works fine, and is consistent with
everything I learned in C++, is there a 'better' way to pass
information, including member variables, objects, and the like, other
than using a parametized constructor as below?

Just to clarify: I don't believe a global variable is a 'better' way,
so that way is excluded.

RL

====calling form, has this somewhere in an event trigger procedure
where you want to call the called form:

int kay = myClass.SomeProperty;

Form2MyWinForm myForm01 = new Form2MyWinForm(kay); //a
parametized constructor, since parameter int 'kay' is passed

myForm01.Show(); //shows the called form

====called form (dialog), has this parametized normal constructor:

public partial class Form2MyDialogBox : Form
{
int F2MDBint;

public Form2MyDialogBox(int j) //parameterized instance
constructor, takes an 'int', which is therefore passed from calling to
called form.
{
InitializeComponent();
F2MDBint = j;
}
///
Jul 7 '08 #2
On Jul 7, 7:32*pm, raylopez99 <raylope...@yahoo.comwrote:
This thread is about how variables or parameters (including objects)
are passed between forms (namely, using parameterized constructors,
e.g., to pass an int between forms (e.g., a calling form and a called
dialog form) (see below).

My question is that though this works fine, and is consistent with
everything I learned in C++, is there a 'better' way to pass
information, including member variables, objects, and the like, other
than using a parametized constructor as below?

Just to clarify: *I don't believe a global variable is a 'better' way,
so that way is excluded.

RL

====calling form, has this somewhere in an event trigger procedure
where you want to call the called form:

* * * * int kay = myClass.SomeProperty;

* * * * * * Form2MyWinForm myForm01 = new Form2MyWinForm(kay); //a
parametized constructor, since parameter int 'kay' is passed

* * * * * * myForm01.Show(); *//shows the called form

====called form (dialog), has this parametized normal constructor:

* * public partial class Form2MyDialogBox : Form
* * {
* * * * int F2MDBint;

* * * * public Form2MyDialogBox(int j) //parameterized instance
constructor, takes an 'int', which is therefore passed from calling to
called form.
* * * * {
* * * * * * InitializeComponent();
* * * * * * F2MDBint = j;
* * * * }
///
It's just my personal opinion, but I do not like to use parameterized
form constructors. The only time I even use class constructors with
parameters is when I need to set up some overloads.

I prefer to pass data to another form via properties.

And, AFAIK, C#.NET doesn't even support true global variables anyway.
Jul 8 '08 #3
On Jul 8, 9:44*am, "Jeff Winn" <jw...@nospam.comwrote:
Create a public property on the child form you access from the parent.
<snip>
>
I'm not a huge fan of creating gigantic constructors. I usually limit the
arguments on a constructor to those absolutely necessary for the object to
function correctly. If the above example must have MyInt to work correctly,
I'd create an argument for it in the constructor, otherwise I'd leave it as
a property. Also, if you're need to update a control on the child form from
an exposed property you can do so quite easily so long as your object calls
the Initialize method on the form in the constructor (if you're using Visual
Studio).

"raylopez99" <raylope...@yahoo.comwrote in message

news:39**********************************@y21g2000 hsf.googlegroups.com...
I'd also add that, if both forms require acess to a number of shared
values, it may be worthwhile creating an object which is passed (via a
parameterised constructor in the child form).
The advantage here is that - assuming the parent form wants to know
about values changed in the child - it has access to the object
'immediately' - i.e. without having to access parameters on the child
form.
I also am not a fan of exposing controls on child forms - much better
(IMHO) to expose a property which is of relevant type - but not of the
actual control type (e.g. a string property ratherthan a TextBox
propertyif the parent wants to set the child's textBox's .Text
property. This abstraction allows the child form to be changed (e.g.
to use a label instead of a textbox) without the need to change the
parent form at all.
Jul 8 '08 #4
On Jul 8, 9:44*am, "Jeff Winn" <jw...@nospam.comwrote:
Create a public property on the child form you access from the parent.
<snip>
>
I'm not a huge fan of creating gigantic constructors. I usually limit the
arguments on a constructor to those absolutely necessary for the object to
function correctly. If the above example must have MyInt to work correctly,
I'd create an argument for it in the constructor, otherwise I'd leave it as
a property. Also, if you're need to update a control on the child form from
an exposed property you can do so quite easily so long as your object calls
the Initialize method on the form in the constructor (if you're using Visual
Studio).

"raylopez99" <raylope...@yahoo.comwrote in message

news:39**********************************@y21g2000 hsf.googlegroups.com...
I'd also add that, if both forms require acess to a number of shared
values, it may be worthwhile creating an object which is passed (via a
parameterised constructor in the child form).
The advantage here is that - assuming the parent form wants to know
about values changed in the child - it has access to the object
'immediately' - i.e. without having to access parameters on the child
form.
I also am not a fan of exposing controls on child forms - much better
(IMHO) to expose a property which is of relevant type - but not of the
actual control type (e.g. a string property ratherthan a TextBox
propertyif the parent wants to set the child's textBox's .Text
property. This abstraction allows the child form to be changed (e.g.
to use a label instead of a textbox) without the need to change the
parent form at all.
Jul 8 '08 #5
On Jul 7, 4:44*pm, "Jeff Winn" <jw...@nospam.comwrote:
Create a public property on the child form you access from the parent.
Thanks, that was nice and easy.

RL
Jul 9 '08 #6
On Jul 8, 12:18*pm, za...@construction-imaging.com wrote:
>
It's just my personal opinion, but I do not like to use parameterized
form constructors. The only time I even use class constructors with
parameters is when I need to set up some overloads.

I prefer to pass data to another form via properties.
That's interesting. I wonder why it's a personal preference, except
for the fact that constructors are maybe more inflexible since they
are only run once, during instantiation, while properties can be set
and get anytime.
>
And, AFAIK, C#.NET doesn't even support true global variables anyway.
That's interesting, I did not know that. Somebody said properties are
the new global variables, but I guess they meant it as hyperbole.

RL

Jul 9 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: vbMark | last post by:
Hello, I am doing this: window.location="page2.php?subj="+subj+"&body="+body; to send information to another page. However, I don't want that second page to show up or display in any way....
4
by: Grant | last post by:
Im using C# in a web app. Ive got a page with a linkbutton with the following code for the click event: ----------------- Session = "Test"; Server.Transfer("TestPage.aspx"); -----------------...
1
by: Gaye Kruchten | last post by:
Hi! I'm just getting starting in web development using ASP.NET and have a very basic question. I have a web page that captures name and address information. I want to pass this information on...
3
by: A Ward | last post by:
I am trying to find a way to have multiple seperate ASP.Net applications where I can response.redirect() to a second web application and pass information. From what I have tried: * HTTP-GET - I...
7
by: Tim | last post by:
When there is a need to pass some dynamic information between 2 managed assemblies, the "Dictionary object" in Generic form can be used as a method parameter to pass the information. The...
7
by: Drum2001 | last post by:
I am creating a database to track employee time. I have created a form that allows them to select from a combo box their task, enter their hours, and add any comments they may have. In the...
1
by: nona | last post by:
Hi all, i need some help here, i'm using a datagrid on my ASP.net application, inside the datagrid was inserted a hyperlink on the table this hyperlink was passing information about the data set...
1
by: rfr | last post by:
I have a need to use a single version of a Visitor Response Feedback Form on numerous HTML documents. Rather than have numerous versions of this, one on each HTML document, it makes more sense to...
3
by: hg | last post by:
I have a function called logprintf that take a printf-like syntax. On top of that I want to implement a debug-printf function which calls logprintf but prefix the information using a string: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.