473,796 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Class scope

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 two part question.

Part 1:

I have a form with a text box on it that I want to spit out results to.
Sample code:
namespace MyApplication
{
public class MyApplication : System.Windows. Forms.Form
{
private System.Windows. Forms.TextBox txtMessage;
private System.Componen tModel.Containe r components = null;

public FileProcess()
{
//Some Code to process my file
FileSystemWatch er watcher = new FileSystemWatch er();
watcher.Created += new FileSystemEvent Handler(OnChang ed);
watcher.EnableR aisingEvents = true;
//etc.....
}

static void Main()
{
Application.Run (new FileProcess());
}

public static void OnChanged(objec t source, FileSystemEvent Args e)
{
txtMessage.Text = "File created."
//***** THIS IS WHERE I GET MY ERROR!
}
}

So as you can see, in the "OnChange" function, I cannot see the txtMessage
object. I do not get intellecence(sp ?) and cannot see the object, like it's
out of scope. How can I make it so I can see the text box from anywhere in
my code? Like what if I was inside the code of a new class that I had
created and wanted to spit out some results to my form?
Part 2:

My second question is related. I built a class that reads some values from
an XML file. Those values are set to some properties. In my main code you
see above I have instanciated(sp ?) an instance of this class and can see all
my properties fine. But then when I go into another class I cannot see that
intance and properties anymore. I know I can just intanciate another intance
of the XML class to get the values again, but I don't want to have to keep
running that code to read the XML every time I need the values. Is there a
way to intanciate the class globally? So no matter where I am I can see the
properties?

Thanks for you help.
Michael.

Jul 19 '06 #1
2 3525
You should be able to see the txtMessage control from inside OnChanged, but I
notice that you're missing a closing brace ("}"). Depending on where you put
it, it can change the meaning of what you've typed.

For your second question, you could look into the Singleton pattern which
allows you to have a single instance of a class everywhere. To implement it,
you create a static method on your class that returns a static private
variable that has an instance of your object.

--
Jeffrey Hornby
Hornby Consulting, Inc.

"Michael" wrote:
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 two part question.

Part 1:

I have a form with a text box on it that I want to spit out results to.
Sample code:
namespace MyApplication
{
public class MyApplication : System.Windows. Forms.Form
{
private System.Windows. Forms.TextBox txtMessage;
private System.Componen tModel.Containe r components = null;

public FileProcess()
{
//Some Code to process my file
FileSystemWatch er watcher = new FileSystemWatch er();
watcher.Created += new FileSystemEvent Handler(OnChang ed);
watcher.EnableR aisingEvents = true;
//etc.....
}

static void Main()
{
Application.Run (new FileProcess());
}

public static void OnChanged(objec t source, FileSystemEvent Args e)
{
txtMessage.Text = "File created."
//***** THIS IS WHERE I GET MY ERROR!
}
}

So as you can see, in the "OnChange" function, I cannot see the txtMessage
object. I do not get intellecence(sp ?) and cannot see the object, like it's
out of scope. How can I make it so I can see the text box from anywhere in
my code? Like what if I was inside the code of a new class that I had
created and wanted to spit out some results to my form?
Part 2:

My second question is related. I built a class that reads some values from
an XML file. Those values are set to some properties. In my main code you
see above I have instanciated(sp ?) an instance of this class and can see all
my properties fine. But then when I go into another class I cannot see that
intance and properties anymore. I know I can just intanciate another intance
of the XML class to get the values again, but I don't want to have to keep
running that code to read the XML every time I need the values. Is there a
way to intanciate the class globally? So no matter where I am I can see the
properties?

Thanks for you help.
Michael.
Jul 19 '06 #2
If you want to reference non-static members of a class in a method the
method must also be non-static. Try removing the "static" modifier from
OnChanged.

"Jeffrey Hornby" <Je***********@ discussions.mic rosoft.comwrote in message
news:AE******** *************** ***********@mic rosoft.com...
You should be able to see the txtMessage control from inside OnChanged,
but I
notice that you're missing a closing brace ("}"). Depending on where you
put
it, it can change the meaning of what you've typed.

For your second question, you could look into the Singleton pattern which
allows you to have a single instance of a class everywhere. To implement
it,
you create a static method on your class that returns a static private
variable that has an instance of your object.

--
Jeffrey Hornby
Hornby Consulting, Inc.

"Michael" wrote:
>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 two part question.

Part 1:

I have a form with a text box on it that I want to spit out results to.
Sample code:
namespace MyApplication
{
public class MyApplication : System.Windows. Forms.Form
{
private System.Windows. Forms.TextBox txtMessage;
private System.Componen tModel.Containe r components = null;

public FileProcess()
{
//Some Code to process my file
FileSystemWatch er watcher = new FileSystemWatch er();
watcher.Created += new FileSystemEvent Handler(OnChang ed);
watcher.EnableR aisingEvents = true;
//etc.....
}

static void Main()
{
Application.Run (new FileProcess());
}

public static void OnChanged(objec t source, FileSystemEvent Args e)
{
txtMessage.Text = "File created."
//***** THIS IS WHERE I GET MY ERROR!
}
}

So as you can see, in the "OnChange" function, I cannot see the
txtMessage
object. I do not get intellecence(sp ?) and cannot see the object, like
it's
out of scope. How can I make it so I can see the text box from anywhere
in
my code? Like what if I was inside the code of a new class that I had
created and wanted to spit out some results to my form?
Part 2:

My second question is related. I built a class that reads some values
from
an XML file. Those values are set to some properties. In my main code
you
see above I have instanciated(sp ?) an instance of this class and can see
all
my properties fine. But then when I go into another class I cannot see
that
intance and properties anymore. I know I can just intanciate another
intance
of the XML class to get the values again, but I don't want to have to
keep
running that code to read the XML every time I need the values. Is there
a
way to intanciate the class globally? So no matter where I am I can see
the
properties?

Thanks for you help.
Michael.

Jul 19 '06 #3

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

Similar topics

2
2207
by: Jerry | last post by:
My "main" class is getting a bit long...Is it possble to split a class definition into several files and then import the pieces to get the whole definition? Jerry
6
3196
by: Brian Jones | last post by:
I'm sure the solution may be obvious, but this problem is driving me mad. The following is my code: class a(object): mastervar = def __init__(self): print 'called a'
30
2279
by: Neil Zanella | last post by:
Hello, Suppose I have some method: Foo::foo() { static int x; int y; /* ... */ }
9
2452
by: Steven T. Hatton | last post by:
It was once suggested to me that I could accomplish much the same thing that modules would accomplish (if C++ had modules) by writing my entire program - except for main() - inside of a class. When it was suggested, I didn't take it very seriously, but I have recently begun wondering if it is an idea worth considering. I'm now trying to think of what fundamental differences might exist between namespace scope, and class scope. One that...
6
2501
by: Taran | last post by:
Hi All, I tried something with the C++ I know and some things just seem strange. consider: #include <iostream> using namespace std;
7
2117
by: WXS | last post by:
Vote for this idea if you like it here: http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=5fee280d-085e-4fe2-af35-254fbbe96ee9 ----------------------------------------------------------------------------- This is a consortium of ideas from another thread on topic ----------------------------------------------------------------------------- One of the big issues of organizing items within a class, is there are many...
5
2036
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope of a name declared in a class consists not only of the declarative region following the name's declarator, but also of all function bodies, default arguments, and constructor ctor-initializers in that class (including such things in nested classes).
15
7873
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
200
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 11:29:18 Cousson, Benoit, vous avez écrit : This is a language limitation. This is because nested scope is implemented for python function only since 2.3 allow late binding of free variables. the scope in class statment is not a closure, so there is only two possible scope in it : local and global. When "class C2(C1):" statment is interpreted, it is in the scope of class B for which a name C1 exists, but it...
0
154
by: Cousson, Benoit | last post by:
This is a language limitation. That was my understanding as well, but I think it is a pity to have that limitation. Don't you think that the same improvement that was done for method nested scope could be done as well for nested class? I can easily fix my current issue by doing the binding after the class declaration. My concern is more about the lack of symmetry of that approach; meaning that if both classes are in the global scope, one...
0
10457
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...
1
10176
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
10013
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...
1
7550
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
6792
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
5443
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5576
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4119
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
3
2927
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.