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

Home Posts Topics Members FAQ

Visibility of Form1 question.

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, I can
add a class to the project, make sure it and its fields are public,
instantiate an object of that class (say, in Main()) but won't be able to
see that class's public fields from within Form1. This seems to all hold
true even if I am using the fully qualified (starting with the namespace)
name.

This is actually a smaller part of my larger attempt to understand how to
organize a program into a decent object-oriented, modular design. I have
tried to find some good code samples of such, but so far have failed.

So, can someone please put me on the right track to understanding how the
visibility and scope is working in these cases? Thank you very much.

JB
Mar 25 '07 #1
2 1842
Hi Jamey,

If you could show us some code we might spot the errors.

If the fields are public they are visible to everyone, however, they will only be accessible through an instance of the class those fields belong to (unless the fields are static).

public class MyProgram
{
public static void Main()
{
MyForm myForm = new MyForm();
myForm.MyTextBo x.Text = "Hello World";

Application.Run (myForm);
}
}

public class MyForm : System.Windows. Forms.Form
{
public TextBox MyTextBox = new TextBox();

public MyForm()
{
this.Controls.A dd(MyTextBox);
}
}

The code above lets Main change the text of MyForm's MyTextBox. However, instead of accessing fields directly, you should go through properties..

public class MyProgram
{
public static void Main()
{
MyForm myForm = new MyForm();
myForm.MyText = "Hello World";

Application.Run (myForm);
}
}

public class MyForm : System.Windows. Forms.Form
{
private TextBox MyTextBox = new TextBox();

public string MyText
{
get{ return MyTextBox.Text; }
set{ MyTextBox.Text = value; }
}

public MyForm()
{
this.Controls.A dd(MyTextBox);
}
}

This way MyForm will have more control of what gets set in the TextBox. For instance, MyText can test for InvokeRequired before setting the TextBox value.

Having written all this, I think I understand your scenario

public class MyForm : System.Windows. Forms.Form
{
private TextBox MyTextBox = new TextBox();
private static TextBox MyStaticTextBox = new TextBox();

public string MyText
{
get{ return MyTextBox.Text; }
set{ MyTextBox.Text = value; }
}

public MyForm()
{
this.Controls.A dd(MyTextBox);
}

public static void Main()
{
MyTextBox.Text = "Hello World"; // ERROR! MyTextBox isn't static
MyStaticTextBox .Text = "Hello WOrld"; // Works because MyStaticTextBox is static
}
}

The code above won't work because Main() is static, which means you can't access fields, properties or methods from MyForm that aren't static. Among all MyForm objects you may create, there will be numerous MyTextBox controls created, one for each MyForm, but there will only be one MyStaticTextBox , shared among all the MyForm objects. For the same reason Main() needs to be static, so that the operating system can access the Main() method, even though there may be no MyForm objects created.
On Sun, 25 Mar 2007 09:55:06 +0200, Jamey Bon <jb***@ue.comwr ote:
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, I can
add a class to the project, make sure it and its fields are public,
instantiate an object of that class (say, in Main()) but won't be ableto
see that class's public fields from within Form1. This seems to all hold
true even if I am using the fully qualified (starting with the namespace)
name.

This is actually a smaller part of my larger attempt to understand howto
organize a program into a decent object-oriented, modular design. I have
tried to find some good code samples of such, but so far have failed.

So, can someone please put me on the right track to understanding how the
visibility and scope is working in these cases? Thank you very much.

JB


--
Happy coding!
Morten Wennevik [C# MVP]
Mar 25 '07 #2
PS

"Jamey Bon" <jb***@ue.comwr ote in message
news:Xn******** *************** *@216.151.153.4 4...
>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, I can
add a class to the project, make sure it and its fields are public,
instantiate an object of that class (say, in Main()) but won't be able to
see that class's public fields from within Form1. This seems to all hold
true even if I am using the fully qualified (starting with the namespace)
name.

This is actually a smaller part of my larger attempt to understand how to
organize a program into a decent object-oriented, modular design. I have
tried to find some good code samples of such, but so far have failed.

So, can someone please put me on the right track to understanding how the
visibility and scope is working in these cases? Thank you very much.
These are some additional comments in addition to Morten's.

1. Using the fully qualified namespace makes no difference to visibility
once the class name can be resolved. The using statement just saves you
having to always use the fully qualified name. If your code compiles that
using / not using fully qualified names makes no difference.

2. Generally I avoid exposing controls outside of the form. However if you
need to do this then I would recommend exposing them from a property.
Internal or public fields are generally not used in any class. Even better,
expose just the property that you need access to, e.g.
internal string StatusText {
set { this.statusText Box.Text = value;}
}

3. The overall organizational structure of a solution is that you use
internal and private as much as possible within each project. For any
"cross-project" access you use public (note that there is something called a
friend assembly that allows you to see internal members between projects). A
common example where this is used is object construction. Your objects have
internal constructors so outside of that project you can not create these
objects. So you access them through a factory that has a public (and static)
method.

4. As a point of interest if you have no modifier on a field, property etc
then it defaults to the most private modifier that it can be. For example
namespace MyApplication {
class Employee {
Employee()
{...
is an internal class with a private constructor.

PS
Mar 25 '07 #3

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

Similar topics

4
2806
by: Jonathan | last post by:
Hi, I've read through quite a number of postings on here so far and have seen what look like very simply, reasonable answers to this question, however I am still completely unable to do what I want to do. I just want to know how I should toggle the visibility of divs in Netscape (I'm using 7). For example, say I have the following HTML...
4
25055
by: wasntme | last post by:
I want to toggle a <div in and out of view. My question, which of the below examples would best be supported. Or is there another approach, that would be better used..TIA.. ..A.. <a href="#" onclick="C1.style.visibility='visible';">open</a> <a href="#" onclick="C1.style.visibility='hidden'">close</a> ..B.. <a href="#"...
3
1358
by: Alberto | last post by:
I have in a form a SqlConnection and a SqlCommand but I also want to use them in form shown as DialogForm. I declared the objects as public but I don't know how to go on. Thank you.
13
1740
by: Liz | last post by:
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
1
1445
by: Mark Denardo | last post by:
I stumbled across a weird problem that I don't seem to understand and was wondering if anyone could help explain why it is occurring: Basically I have set up a client/server application where the client and server communicate via a NetworkStream. My problem is on the Client side. The Client's "Main Thread" starts up a form (Form1) which...
1
1638
by: | last post by:
How do I control the layering of 4 forms of identical size and position to cause the desired form to be second from the top.. Form1 has my welcome screen etc.and the code for file manipulations etc. etc Form2 has a foreground image of a decorative picture frame (,png image) with a transparent center, and a tab control for the user to...
2
2336
by: Steve JORDI | last post by:
Hi, I have a strange problem using Internet Explorer. My page has a table included in a <DIV> I have a checkbox that shows the div when checked and hides it when unchecked. var division = document.getElementById("passport") ; Then I use division.style.visibility = "visible" ;
5
2204
by: GoGs | last post by:
HEEEELLLLLPP I hawe two Open form... Form1 and Form2 How I can from Form2 send text to textbox of Form1 //public System.Windows.Forms.TextBox textBox1; //Form1 fo = new Form1(); //fo.textbox1.Text = "something"; --Don't work
2
2596
by: Andrus | last post by:
To reproduce, run code, open file menu, press up and down arrows to move out of menu. Menu items are scrolled and blank items appears in end. I need to change visibility of menu items at runtime. How to fix this ? Is this .NET 3.5 bug ? Andrus.
0
7703
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
7619
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
8138
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...
0
6290
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...
1
5514
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
3662
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2118
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1229
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.