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

accessing a field of another class within same namespace

P: n/a
Hi all,

I am trying to access a public field of another form class within
the same namespace. The field is public, what is the best way
to access it from a different class? I defined as
private MyNameSpace.Form1 cForm1;

and I am trying to use it later as

TextBox.Text = cForm1.TextBox.Text;

I see the properties, but I have a warning telling me that cForm1 is never
assigned, and that its value will always be null. How can I avoid that?

Thanks again in advance,

Carlos.




Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Carlos <ch******@yahoo.com> wrote:
I am trying to access a public field of another form class within
the same namespace. The field is public
Oh dear - you should consider making it private, backing a public
property.
, what is the best way
to access it from a different class? I defined as
private MyNameSpace.Form1 cForm1;

and I am trying to use it later as

TextBox.Text = cForm1.TextBox.Text;

I see the properties, but I have a warning telling me that cForm1 is never
assigned, and that its value will always be null. How can I avoid that?


Simple - give it a value. I think you're missing that you need an
instance of the form whose field you want to change. You need to ask
yourself *which* form you want to use the field of. There could be
several forms of the same type being shown at once (at least
conceptually).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #2

P: n/a
Thanks Jon,

I instantiated it, but the value which has been assigned does not appear.
What I did was to make it static public, but ideally I would just
like to access it with the value that it has (not globally).

Thanks,

Carlos.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Carlos <ch******@yahoo.com> wrote:
I am trying to access a public field of another form class within
the same namespace. The field is public


Oh dear - you should consider making it private, backing a public
property.
, what is the best way
to access it from a different class? I defined as
private MyNameSpace.Form1 cForm1;

and I am trying to use it later as

TextBox.Text = cForm1.TextBox.Text;

I see the properties, but I have a warning telling me that cForm1 is
never
assigned, and that its value will always be null. How can I avoid that?


Simple - give it a value. I think you're missing that you need an
instance of the form whose field you want to change. You need to ask
yourself *which* form you want to use the field of. There could be
several forms of the same type being shown at once (at least
conceptually).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Nov 17 '05 #3

P: n/a
Carlos <ch******@yahoo.com> wrote:
I instantiated it, but the value which has been assigned does not appear.
What I did was to make it static public, but ideally I would just
like to access it with the value that it has (not globally).


What do you mean by "the value which has been assigned does not
appear"? Don't forget that it will be specific to that instance - if
you've got another instance, that will have a different value for the
field.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #4

P: n/a
There seems to be a mix up between class and object

The class Form1 exists within the Namespace MyNameSpace

Objects exist within a scope and are access with reference to that
scope.

Where are you placing the

private MyNameSpace.Form1 cForm1;

declaration?

That is what defines how it can be accessed.

By making it Private you are limiting access to it, saying that only
items in the same declaration context / scope (or those nested within
it) can access it.
If you declared it inside a class, only members of that class can see
it
If you declared it in a module, only methods within that module can
see it.

Can you give some code snippets showing the classes/modules involved
and where you are declaring / trying to access the object?

Alan.

Nov 17 '05 #5

P: n/a
Thank you all for your interest in helping. Yes, I have a Form, that
defines a private field which is a TextBox that contains a value.
say:

private System.Windows.Forms.TextBox EFW;
During the processing of this form, a new form can be
launched, and in the new form, I would like to capture the
value that was entered in the first form TextBox.
Since the 2 forms (classes) are within the same namespace,
I just though to define a variable of type Form1 inside the second form.
i.e.
private MyNameSpace.Form1 cForm1;

This will be pointing to null if I do not instantiate, but if I do

instantiate, a new object is created in the heap, which is not referencing

the initial Form1. The question is what is the best way to declare the

variable in the first form, and how to do it in the second form, so that it
can access

the variable that has a value in it.

I kind of solved it by declaring a

static public int sVar;

in the first form, so that will hold such value, and I will access it

in the second form without instatiating a new object. Since I am

just getting the skills in C#, I just wanted to know if there is a more

elegant way to do it using object programming theory.

Thanks in advance..



static public int spiEFW;
<al*******@users.com> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
There seems to be a mix up between class and object

The class Form1 exists within the Namespace MyNameSpace

Objects exist within a scope and are access with reference to that
scope.

Where are you placing the

private MyNameSpace.Form1 cForm1;

declaration?

That is what defines how it can be accessed.

By making it Private you are limiting access to it, saying that only
items in the same declaration context / scope (or those nested within
it) can access it.
If you declared it inside a class, only members of that class can see
it
If you declared it in a module, only methods within that module can
see it.

Can you give some code snippets showing the classes/modules involved
and where you are declaring / trying to access the object?

Alan.

Nov 17 '05 #6

P: n/a
Carlos <ch******@yahoo.com> wrote:
Thank you all for your interest in helping. Yes, I have a Form, that
defines a private field which is a TextBox that contains a value.
say:

private System.Windows.Forms.TextBox EFW;

During the processing of this form, a new form can be
launched, and in the new form, I would like to capture the
value that was entered in the first form TextBox.
Since the 2 forms (classes) are within the same namespace,
I just though to define a variable of type Form1 inside the second form.
i.e.
private MyNameSpace.Form1 cForm1;
The namespace here is irrelevant.
This will be pointing to null if I do not instantiate, but if I do
instantiate, a new object is created in the heap, which is not referencing
the initial Form1. The question is what is the best way to declare the
variable in the first form, and how to do it in the second form, so that it
can access the variable that has a value in it.


You need to tell the second form which object to look at. Pass the
second form's constructor a reference to the first form ("this" if
you're creating the second form from the first form).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.