473,809 Members | 2,733 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Visibility/Access of Class Members

Liz
ok, this is really simple stuff, or it should be ... but I'm stuck

In a Windows Forms app, I have something resembling this:

Form1.cs
========

namespace NS

Class Form1
{
public Label label1

...

private void func1()
{
iterateRows();
}
}

Class1.cs
=========

using NS;

public class GetLabel
{
private void iterateRows()
{
Form1.label1.Te xt = "some value";
}

}
the method iterateRows is really performing a loop operation and I just want
to show values in label1 with each iteration; simple, right ? should be
but label1 is not visible from here ... I cannot find anything in the docs
which makes clear to me why Form1.label1.Te xt is not visible ... this kind
of stuff used to be a piece of cake in "C" .. but I've been away from coding
for awhile ...

I suppose I could pass a reference to label1 to iterateRows() and make this
work ? but I'd rather not and I'd like to understand why this won't work
in any case

Thanks for any help/explanations ...

L


Nov 17 '05
13 1756
Liz

"Bob Grommes" <bo*@bobgrommes .com> wrote in message
news:#P******** *****@TK2MSFTNG P15.phx.gbl...
I'm not sure why you're declaring a class member named myForm within the
Form1 class definition. As coded, it would produce a null Form
reference within the Form1 instance, and would be of no use.
correct ... it doesn't need declaration there
If label1 is still declared as a public field, myForm.label1 should be
able to be referenced. Don't conclude it's not from the absence of
Intellisense, in my experience that's only 80% reliable until the
project has had a recompile (or sometimes it starts working on its own
after some IDE background thread has caught up with reality). It's not
a good sign though. Is code completion failing in Main() or somewhere else?
You can always test by putting some dummy reference in Main() like
myForm.label1.T ext = "foo";


sure .. and that works, in the Class block, anyway .. but not in the other
..cs file which references the namespace containing the class Form1 ...

I still don't really get why I cannot perform the same test with
Form1.label1.Te xt = "foo"; ... what's the deal ? does the **IDE** actually
create an instance of the class when you type in Form1 myform = new Form1()
?? and if it hasn't seen a "new" yet it doesn't know that label1 is a
member of the class ?

static void Main()
{
myForm.label1. .... // code completion does not work here
Form1 myForm = new Form1();
myForm.label1.T ext = "XXX"; // but works here ...
Application.Run (myForm);
}
L
Nov 17 '05 #11
Liz <li*@tiredofspa m.com> wrote:
No, because you've declared myForm to be of type Form, not type Form1.


true ... thank you; but the other code file still does not "see" the public
var label1 even though the namespace in which it "lives" is referenced


The namespace is pretty irrelevant. Could you post a short but complete
program which demonstrates the problem? If you've got a variable of
type Form1, you should be

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
The compiler can't know that the value won't be a reference to an
instance of a completely different form.


well, the compiler should know that it's either of type Form or of type
Form1, no ?


No - it could be any other form, as far as the compiler knows.
Declare it as Form1 (preferrably renaming it to a more useful name, of
course) and it should start working. Having said that, it's not good
practice to have public variables in the first place.


well, I'll leave the "religious" aspects of coding alone ;) this is just a
sandbox so I can nail down the functional characteristics of the language
...


It's still worth learning best practices at the same time though.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #12
Bob Grommes <bo*@bobgrommes .com> wrote:
I'm not sure why you're declaring a class member named myForm within the
Form1 class definition. As coded, it would produce a null Form
reference within the Form1 instance, and would be of no use.

If label1 is still declared as a public field, myForm.label1 should be
able to be referenced.


No it shouldn't - note that myForm is just a Form variable, not a Form1
variable.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #13
The IDE knows about the Form1 type. It doesn't need to create a
design-time instance, it just is aware of the metadata that defines
those types. (The designer probably does create a design time instance,
but that's separate from the code editor AFAIK).

At any rate ... I don't understand your final question, "I ... don't ...
get why I cannot perform ... Form1.label1.Te xt = "foo"". If that wasn't
a slip of the keyboard then I must return to my original explanation:
Form1 is a type, not an instance. The only thing you can access through
the type are static members. Instance members must be addressed through
an instance. Form1.label1 is not a valid construct because label1 is an
instance member.

If on the other hand you're asking (given a Form1 instance named myForm)
why "myForm." doesn't auto-complete, but "myForm.label1. " *does* offer
you auto-complete options -- I don't know offhand. I've seen stuff like
this intermittently and just chalk it up to One Of Those Things.
Sometimes it clears up on its own (because some background parsing
thread caught up with what you're typing) and sometimes it Just Won't
Work, At Least Not Today.

--Bob

Liz wrote:
"Bob Grommes" <bo*@bobgrommes .com> wrote in message
If label1 is still declared as a public field, myForm.label1 should be
able to be referenced. Don't conclude it's not from the absence of
Intellisens e, in my experience that's only 80% reliable until the
project has had a recompile (or sometimes it starts working on its own
after some IDE background thread has caught up with reality). It's not
a good sign though. Is code completion failing in Main() or somewhere


else?

You can always test by putting some dummy reference in Main() like
myForm.label1 .Text = "foo";

sure .. and that works, in the Class block, anyway .. but not in the other
.cs file which references the namespace containing the class Form1 ...

I still don't really get why I cannot perform the same test with
Form1.label1.Te xt = "foo"; ... what's the deal ? does the **IDE** actually
create an instance of the class when you type in Form1 myform = new Form1()
?? and if it hasn't seen a "new" yet it doesn't know that label1 is a
member of the class ?

static void Main()
{
myForm.label1. .... // code completion does not work here
Form1 myForm = new Form1();
myForm.label1.T ext = "XXX"; // but works here ...
Application.Run (myForm);
}
L

Nov 17 '05 #14

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

Similar topics

3
1525
by: kele | last post by:
Do sub Namespaces have visibility to parent class. ie. I am trying to develop an application that is made up of modules and the main module contains a variables class that contains all the variables needed for the application. eg. MainModule Namespace Contains MainMod Class
12
3946
by: lawrence | last post by:
The following function correctly makes everything invisible but then fails to turn the one chosen DIV back to visible. I imagine I'm getting the syntax of the variable wrong? I've tried this with both single quotes and no single quotes, but it doesn't work either way. What am I doing wrong? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) { document.getElementById('weblogs').style.visibility='hidden';
4
5486
by: lawrence | last post by:
Can anyone tell me why this code works in Netscape 7.1 but not in IE??? <SCRIPT type='text/javascript'> function makeVisible(nameOfDiv) { document.getElementById(nameOfDiv).style.visibility='visible'; document.getElementById(nameOfDiv).style.height='auto'; if (nameOfDiv != 'weblogs')
7
1506
by: Steve Marsden | last post by:
Hi As a C programmer just starting to look at Managed C++, I was shocked to find that you cannot declare a managed type globally so that for example in a partiuclar module you can declare some data globally so that all functions in that module can see it to save passing it about as parameters. How do you achieve the same thing with Managed C++ managed data. Thanks
7
5461
by: Alan Foxmore | last post by:
I understand about public, internal, protected, etc. Is there a way to ensure members are accessible within the same namespace only? In other words, I want to prohibit access to members outside the namespace. Can this be done? I don't think so. Thanks -- Alan Foxmore
4
3199
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can get public members but not protected, private, static members"
8
1480
by: khalprin | last post by:
Hello, I'm trying to create a component that will be used from .net clients and COM clients. I've got an object model that looks something like this: ISystem IRuntime IConfiguration ICollectionOfConfigurableThings IConfigurableThing
3
2469
by: RitualDave | last post by:
This compiles and runs successfully in VS2005: ref class A { private: ~A() { this->!A(); } // private! !A() { } // private! }; ....
2
1859
by: Jamey Bon | last post by:
I am a C# newbie. I am having a tough time with several issues of scope and visibility. In short, why can't I see any of the elements of Form1 (the base form generated by the "Windows Application Project Wizard") from anywhere else in my project? For example, why can I not change the text of a TextBox control from Main() (which is, by default, in the Program class) even after I have set its visibility to public in Form1? Furthermore,...
0
10637
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10376
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10379
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
10115
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9199
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7660
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6881
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.