473,569 Members | 2,737 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.For m1 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 7745
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.For m1 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.co m>
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.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
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.For m1 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.co m>
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.co m>
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.For m1 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.For m1 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*******@user s.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.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.For m1 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.For m1 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.co m>
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
2455
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
9
290
by: Mike Oliszewski | last post by:
Given the following c# code: namespace Company2 { public class SomeFunctions { public void FunctionA() { // Do Something. }
3
2784
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 IDs. Thanks Vivek
2
2107
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 the other is a debug window with a multiline text field for displaying debug messages within the application. What happens is i load the debug form...
5
2691
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 properties. I want to know if there is a way to access the methods and the properties of the Owner class for the class that's inside it? I.e. I...
0
1142
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 using r.QuestionsList.Add(q) to add a new instance within the loop but it does not work. Any help will be appreciated...
3
7764
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 residing in a different translation unit, then the current translation unit was simply oblivious to it and had no way of accessing it. Any time the...
1
1592
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 class Program {
5
2502
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 pointer to object of class Form1. I don't known how to use method add(), where can I find it. From Form1 I can add value like this this->listBox1-...
0
7700
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7614
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8125
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7676
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7974
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5513
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3642
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1221
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.