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

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 1827
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.MyTextBox.Text = "Hello World";

Application.Run(myForm);
}
}

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

public MyForm()
{
this.Controls.Add(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.Add(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.Add(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.comwrote:
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.comwrote in message
news:Xn************************@216.151.153.44...
>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.statusTextBox.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
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...
4
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="#"...
3
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
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
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...
1
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...
2
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 =...
5
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();...
2
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...
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...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.