473,320 Members | 1,950 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,320 software developers and data experts.

accessing a field of another class within same namespace

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
6 7716
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
9
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
3
by: Vivek Sharma | last post by:
Hi, I have created a dropdownlist as a web user control. I am using its multiple instances on the webpage. How do I access the selectedValue of each instance? All the instances have different...
2
by: Gary Townsend | last post by:
I have seen similar requests to this but not quite what i was looking for i have an mdi interface and i have 3 child forms 2 are application forms (meaning they do stuff for the application) and...
5
by: Cyril Gupta | last post by:
Hello, I have a class inside another class. The Scenario is like Car->Engine, where Car is a class with a set of properties and methods and Engine is another class inside it with its own set of...
0
by: rlueneberg | last post by:
I am getting this error when trying to add typed object "Questions" to Arraylist QuestionsList: Object reference not set to an instance of an object. --> r.QuestionsList.Add(q); I tried...
3
by: Frederick Gotham | last post by:
Back in the day, if you wanted a function to be self-contained within a translation unit, you defined the function as "static". If there were an external linkage function by the same name...
1
by: bg_ie | last post by:
Hi, I have my own UserSettings class that I wish to include within my windows application. To include it at the moment, I do something like this - namespace WindowsApplication1 { static...
5
by: lukasmazur | last post by:
Hi I have a problem with using listBox1. I have a two forms form1 and form2. In form1 are controls listBox1, textBox1 and button witch creating object of class Form2. In class Form2 I create a...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

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.