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

Class/Form scope question

Kim
Like
http://groups.google.dk/group/micros...280aaeed78de0/
and
http://groups.google.dk/group/micros...ea4d944b12bf0/
am I trying to access something in a form in one class from another
class. And without luck :(

In my case I tried to change the Text of a Label. Here is my problem:

I'm using Microsoft Visual Studio 2005 Professional Edition.
I have 2 files (Form1.cs and Stuff.cs) with 1 class each and they are
in the same namespace. Form1.cs has a form with some elements, among
these, a label (Label1) and a button (Button1). I made an event so that
when I click on Button1 a fuction in Stuff.cs is called/run.
When the function in Stuff.cs is done, I want to signal this on the
form window by changing the Label1.Text.

I have tried making Label1 public (by default its private). See below,
note that this is inside Form1.cs.
public System.Windows.Forms.Label Label1

When I tried to change the text, from Stuff.cs, like this
Form1.Label1.Text= "NewText";
I got a compile error, "An object reference is required for the
nonstatic field, method, or property".

My question is then: How can I change something on an element in a form
in another class but while in the same namespace ?

Jun 22 '06 #1
5 1762
The compile error says it all:

Form1 is a type declaration, not an instance; as far as the compiler is
concerned you could have 400 instances of Form1 all side-by-side : so which
one should it update? To do this using direct access, you *must* have a
reference between the forms. It really comes down to how you are creating
the forms, and how they behave; if you create them together and they both
sit side by side, then you might have something like:

Form1 form1 = new Form1();
Form2 form2 = new Form2();
form2.SomeProperty = form1;
form1.Show();
form2.Show();

where SomeProperty is defined something like:
public class Form2 : Form {
// ...
private Form1 _someField;
public Form1 SomeProperty {get {return _someField;} set {_someField =
value;}}
}

This now allows any instance of Form2 to have a handle to the corresponding
Form1 instance.

In a simple scenario, you could proably get away with using some static
property, but I don't recommend it.

Also : it is bad practive to make the Label (or whatever) public; at a push,
Internal maybe, but it would be better to encapsulate it

public class Form1 : Form {
// ...
public string MessageCaption {
get {return Label1.Text;} set {Label1.Text = value;}
}
}

Now a Form2 instance can set SomeProperty.MessageCaption = "Frodo";

Finally, you may want to consider using an eventing mechanism to communicate
between the forms; a little bit more involved than direct property access,
but (for more complex scenarios) far more extensible.

Marc
Jun 22 '06 #2
"Kim" <ki*****@gmail.com> a écrit dans le message de news:
11**********************@p79g2000cwp.googlegroups. com...

| I'm using Microsoft Visual Studio 2005 Professional Edition.
| I have 2 files (Form1.cs and Stuff.cs) with 1 class each and they are
| in the same namespace. Form1.cs has a form with some elements, among
| these, a label (Label1) and a button (Button1). I made an event so that
| when I click on Button1 a fuction in Stuff.cs is called/run.
| When the function in Stuff.cs is done, I want to signal this on the
| form window by changing the Label1.Text.

Create a new public event on the form that can be caught in the outside
class.

Handle the button event in a handler on the form and call your public event
from that handler.

After the the public event returns, set the label in the end of the button
handler on the form.

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 22 '06 #3
Kim
Thanks for the quick replies.

Maybe I wasn't clear about, but when I run the program file, my
constructor opens the form window and it's in this window Label1 and
Button1 are.
Creating a new instance of Form1 in Stuff.cs
Form1 somename = new Form1();
is not what I want, as this creates a new window and leaves the first
window unchanged.
The only way the function (lets call it FuncA) can be called is when
the Button1_Click event is trigger in the form window.
The whole idea is that the form window can call functions from Stuff.cs
and some of those functions should give back responses so it can be
noted that the function has been completed.
I can only see one other option to solve my problem, and thats to make
the functions return error_values, which then I must handle in the
event in Form1.cs.

As you might have noticed Im fairly new into C#, so please give
examples.

Marc: As you said, "It really comes down to how you are creating the
forms, and how they behave", I hope this is more clear now what I do
and want.

Joanna: Think I get the idea, but I under what you mean with "Create a
new public event on the form that can be caught in the outside class".
Could elaborate it ?

Jun 22 '06 #4
Kim
Thanks for the quick replies.

Maybe I wasn't clear about, but when I run the program file, my
constructor opens the form window and it's in this window Label1 and
Button1 are.
Creating a new instance of Form1 in Stuff.cs
Form1 somename = new Form1();
is not what I want, as this creates a new window and leaves the first
window unchanged.
The only way the function (lets call it FuncA) can be called is when
the Button1_Click event is trigger in the form window.
The whole idea is that the form window can call functions from Stuff.cs
and some of those functions should give back responses so it can be
noted that the function has been completed.
I can only see one other option to solve my problem, and thats to make
the functions return error_values, which then I must handle in the
event in Form1.cs.

As you might have noticed Im fairly new into C#, so please give
examples.

Marc: As you said, "It really comes down to how you are creating the
forms, and how they behave", I hope this is more clear now what I do
and want.

Joanna: Think I get the idea, but I under what you mean with "Create a
new public event on the form that can be caught in the outside class".
Could elaborate it ?

Jun 22 '06 #5
"Kim" <ki*****@gmail.com> a écrit dans le message de news:
11**********************@b68g2000cwa.googlegroups. com...

| Joanna: Think I get the idea, but I under what you mean with "Create a
| new public event on the form that can be caught in the outside class".
| Could elaborate it ?

public partial class Form1 : Form
{
...

#region public event

private EventHandler button1Clicked;

public event EventHandler Button1Clicked
{
add { button1Clicked += value; }
remove { button1Clicked -= value; }
}

#endregion

private void button1_Click(object sender, EventArgs args)
{
if (button1Clicked != null)
{
button1Clicked(sender, args);
label1.Text = "some value";
}
}
}

public class Stuff
{
private void HandleForm1Button1Click(object sender, EventArgs args)
{
// do something
}

public void ShowForm()
{
Form1 test = new Form1();
test.Button1Clicked += HandleForm1Button1Click;
test.Show;
}
}

But better still would be to pass an instance of Stuff to the constructor of
the form, then your event handlers can work directly on the object.

public partial class Form1 : Form
{
...

private Stuff stuff;

public Form1(Stuff stuff)
{
this.stuff = stuff;
}

private void button1_Click(object sender, EventArgs args)
{
stuff.DoSomething();
label1.Text = "some value";
}
}
}

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Jun 22 '06 #6

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

Similar topics

30
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
2
by: JJ | last post by:
Hi, I am trying to understand the lifetime or scope of a class in this project. Here is the code that I am talking about: private void PopulateCategoryCombo() { ListItem objListItem;
6
by: Carlos | last post by:
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...
4
by: Dean | last post by:
I finally got class session variables to work by putting the following in global.asax in the session_start: dim myDBComp as DBComp = new DBComp........ session("myDBComp") = myDBComp In each...
19
by: Jamey Shuemaker | last post by:
I'm in the process of expanding my knowledge and use of Class Modules. I've perused MSDN and this and other sites, and I'm pretty comfortable with my understanding of Class Modules with the...
2
by: Gman | last post by:
Hi, I have created a usercontrol, a grid control essentially. Within it I have a class: clsGridRecord. I have coded the events such that when a user clicks on the grid, say, the events occur on...
2
by: Michael | last post by:
Hello, I have a newbie question about class scope. I am writting a little program that will move files to one of two empty folders. I am having a hard time understanding scope. So this will be a...
9
by: Rudy | last post by:
Hello All! I'm a little confused on Public Class or Modules. Say I have a this on form "A" Public Sub Subtract() Dim Invoice As Decimal Dim Wage As Decimal Static PO As Decimal Invoice =...
16
by: Robert Dufour | last post by:
Here's the class code snippet Imports System.Configuration.ConfigurationManager Public Class Class1 Public _strTestSetting As String Public Sub SetTestsetting()
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
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...
0
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,...
0
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...
0
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...

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.