By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,432 Members | 790 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,432 IT Pros & Developers. It's quick & easy.

Accessing Variables of another form

P: n/a
Hello, I have created a form which I intend to use a a dialog box. It has
a few controls on that form and I changed the properties of those controls
to "public" with the idea that I could access those controls from the
"calling form", but I am unable to access/see those controls. Why?

Here are portions of my code

Here I am substantiating the new form and trying to set values for the
controls on that form...

Form frm = new frmGenericInputDialog();
frm.txtSkaterName.Text = item.SubItems[0].Text;
frm.txtCurrentPlacement.Text = cboFinalsFinish.SelectedIndex+1;
DialogResult rslt = frm.ShowDialog();
Here are the declarations of those controls in the

public System.Windows.Forms.TextBox txtSkaterName;
public System.Windows.Forms.Label lblNewPlacement;
public System.Windows.Forms.ComboBox cboCurrentPlacement;
public System.Windows.Forms.TextBox txtCurrentPlacement;

So why can't I access these controls?

Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Your declaring a Form type not your actual Form derived type so the compiler
will have no knowledge of your *extra* fields:
Form frm = new frmGenericInputDialog();
Also a couple of extra points. It would be better not to make the controls
public as you are exposing much more than is necessary and the lesser the
interface area to the class the better. You should instead create public
assessor type properties that in-turn update the controls. Also if you make
something public then you should really adhere to a Pascal naming convention
for the public members.

Regards
Lee

"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.16... Hello, I have created a form which I intend to use a a dialog box. It has
a few controls on that form and I changed the properties of those controls
to "public" with the idea that I could access those controls from the
"calling form", but I am unable to access/see those controls. Why?

Here are portions of my code

Here I am substantiating the new form and trying to set values for the
controls on that form...

Form frm = new frmGenericInputDialog();
frm.txtSkaterName.Text = item.SubItems[0].Text;
frm.txtCurrentPlacement.Text = cboFinalsFinish.SelectedIndex+1;
DialogResult rslt = frm.ShowDialog();
Here are the declarations of those controls in the

public System.Windows.Forms.TextBox txtSkaterName;
public System.Windows.Forms.Label lblNewPlacement;
public System.Windows.Forms.ComboBox cboCurrentPlacement;
public System.Windows.Forms.TextBox txtCurrentPlacement;

So why can't I access these controls?

Nov 15 '05 #2

P: n/a
Thanks Lee for your comments....but
My form is a Class and I am creating an instance of that class, so what are
not all my public objects, contained within the form not available to me if
the form is in the same namespace and in the same project?

You indicated that I and creating a "Form Type" - but am I not creating an
instance of the object which is of that type? And should not all Public
variables/object be available to me?

Don't mean to be dense... but I don't get it...

I have passed variable values as part of the constructor, but I will still
need to access the results of the dialog box, And I am guessing that if I
declare a Public method(s) such return(s) the values I need, then all will
be fine....but why would public methods work and not public variables???
Nov 15 '05 #3

P: n/a
Hi Jim,

"Jim Heavey" <Ji*******@nospam.com> wrote in message
news:Xn*********************************@207.46.24 8.16...
Thanks Lee for your comments....but
My form is a Class and I am creating an instance of that class, so what are not all my public objects, contained within the form not available to me if the form is in the same namespace and in the same project?

<snip>
A lengthy explanation (you've been warned). A line from your code:

Form frm = new frmGenericInputDialog();

But you could also have coded:

Form frm = new Form();

or:

Form frm = new SomeOtherForm(); // SomeOtherForm inherits Form

The point is, it is possible to point a variable of type Form (such as
frm, above) to any object instance that either is an instance of the Form
class or is an instance of a class that inherits from the Form class (as
does frmGenericInputDialog). Now, if your variable "frm" points to anything
other than an instance of frmGenericInputDialog, you know that this line:

frm.txtSkaterName.Text = item.SubItems[0].Text;

is just not going to work. Furthermore, there is no way for the compiler
to know at compile-time what specific type of object "frm" is going to
reference, but compiler *does* know that "frm" can only reference an object
that is a Form or inherits from Form. Therefore it makes sense that the
compiler will limit you to the operations (method calls, properties, ect.)
that are valid for a Form object only.

There are two ways around this. First, you could declare frm like this:

frmGenericInputDialog frm = new frmGenericInputDialog();

Now "frm" can only reference instances of frmGenericInputDialog or
instances that inherit frmGenericInputDialog. Thus any operation that is
valid for frmGenericInputDialog will be valid for "frm".

The second way is to tell the compiler that even though "frm" is of type
Form, you know the object "frm" references is specifically of type
frmGenericInputDialog. You do this with casting. You can read about casting
in the docs, but it can look like this:

Form frm = new frmGenericInputDialog();
...
(frmGenericInputDialog)frm.txtSkaterName.Text =
item.SubItems[0].Text;

HTH.

Regards,
Dan
Nov 15 '05 #4

P: n/a
I got it...Thanks A bunch!!!!!!!!!!!!!!!
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.