472,958 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

What is the scope of a 'static' variable?

Gurus,

If a static variable is defined in a class what is the scope of the variable
resolved to for it to remain 'static'? For instance, lets say I create a
class library assembly that is strongly name which contains the class where
the static variable is defined. This library can be referenced by multiple
projects. I am fairly sure the static variable does not survive across the
application boundry but does it within the application boundry and/or the
domain boundry? Maybe another way to word this question is this: To what
scope is an instance object defined?

Thanks,
Gery
--
Gery D. Dorazio
Development Engineer

EnQue Corporation
1334 Queens Road
Charlotte, NC 28207
(704) 377-3327
Nov 17 '05 #1
4 14811
Hmmm... static is not a scope modifier itself. It can be used with scope
modifiers like 'public' though. The scope of the static variable is
whatever you defined it as with your scope modifier... A public static
variable would have the same scope as a public variable. Not sure if that
answers your question, but I'm not quite sure I understand the question
:( Sorry bout that, if it doesn't...

"Gery D. Dorazio" <gd******@enque.net> wrote in message
news:OM**************@TK2MSFTNGP15.phx.gbl...
Gurus,

If a static variable is defined in a class what is the scope of the
variable resolved to for it to remain 'static'? For instance, lets say I
create a class library assembly that is strongly name which contains the
class where the static variable is defined. This library can be referenced
by multiple projects. I am fairly sure the static variable does not
survive across the application boundry but does it within the application
boundry and/or the domain boundry? Maybe another way to word this question
is this: To what scope is an instance object defined?

Thanks,
Gery
--
Gery D. Dorazio
Development Engineer

EnQue Corporation
1334 Queens Road
Charlotte, NC 28207
(704) 377-3327

Nov 17 '05 #2
I think I confused the issue by not understanding the managed code
architecture. After doing some reading just now, as I understand it, an
'application domain' is what the common language runtime defines as the
boundry for object activations within the same scope. Then MS defines that
scope as the application scope. With that, they also say that multiple
application domains can exist in a single process...this was the area that
confused me with regard to this initial idea of how far 'static' variables
extend. So it looks like the old application scope/domain is the key
here....only one instance of a static variable will exist in the application
domain for all object instantiations...this is the conclusion. But what
prompted this is the following work that I am trying to make robust:

I am trying to do is create an orderly way to access XML files which really
act similarly to tables in a database. There are multiple control files in
multiple directories which are collectively related to a higher level folder
with another control file....e.g. similar to a parent-child table
relationship schema. To access these files currently I use something like
this:

private static object _lockCtrlFileHandle = new object();

and then in various methods:

lock(_lockCtrlFileHandle)
{
...do something...
}
The problem (I think but am not sure) with this approach is that I don't
think it will scale well and it may actually come to a screeching halt with
a large number of users(deadlock). But another question that comes up is how
access to the files should be handled by different applications...the
'static' object lock handle above only works for the application it is
running in. (I think...)

But the next question that comes to mind is that Windows already manages
file handles. It gives an error message when accessing the same file when it
is opened exclusively by another application. I think this is actually built
into file IO perhaps in BIOS.

So maybe the question is really how do I design a file access system that
keeps an orderly access and control for the information thats in those
files?

Thanks for your response. If you have more ideas about this I would
appreciate hearing them.

Thanks,
Gery

--
Gery D. Dorazio
Development Engineer

EnQue Corporation
1334 Queens Road
Charlotte, NC 28207
(704) 377-3327
"Michael C#" <xy*@abcdef.com> wrote in message
news:JY*******************@fe09.lga...
Hmmm... static is not a scope modifier itself. It can be used with scope
modifiers like 'public' though. The scope of the static variable is
whatever you defined it as with your scope modifier... A public static
variable would have the same scope as a public variable. Not sure if that
answers your question, but I'm not quite sure I understand the question
:( Sorry bout that, if it doesn't...

"Gery D. Dorazio" <gd******@enque.net> wrote in message
news:OM**************@TK2MSFTNGP15.phx.gbl...
Gurus,

If a static variable is defined in a class what is the scope of the
variable resolved to for it to remain 'static'? For instance, lets say I
create a class library assembly that is strongly name which contains the
class where the static variable is defined. This library can be
referenced by multiple projects. I am fairly sure the static variable
does not survive across the application boundry but does it within the
application boundry and/or the domain boundry? Maybe another way to word
this question is this: To what scope is an instance object defined?

Thanks,
Gery
--
Gery D. Dorazio
Development Engineer

EnQue Corporation
1334 Queens Road
Charlotte, NC 28207
(704) 377-3327


Nov 17 '05 #3
"Gery D. Dorazio" <gd******@enque.net> wrote in message
news:OM**************@TK2MSFTNGP15.phx.gbl...
Gurus,

If a static variable is defined in a class what is the scope of the
variable resolved to for it to remain 'static'? For instance, lets say I
create a class library assembly that is strongly name which contains the
class where the static variable is defined. This library can be referenced
by multiple projects. I am fairly sure the static variable does not
survive across the application boundry but does it within the application
boundry and/or the domain boundry? Maybe another way to word this question
is this: To what scope is an instance object defined?


Static variables are stored in the assembly data sections. Each time an
assembly is loaded into an app-domain new data and code sections are created
for it. In the case of a domain-neutral assembly, it is loaded into the
SharedDomain only. This theoretically would result in the statics being
shared across the AppDomains that reference the assembly, but the CLR does
some bookkeeping to avoid this.

So simply, the answer is that static variables's values exist in an
AppDomain.
Nov 17 '05 #4
"Gery D. Dorazio" <gd******@enque.net> wrote in message
news:eM*************@TK2MSFTNGP15.phx.gbl...
[Snip]
I am trying to do is create an orderly way to access XML files which
really act similarly to tables in a database.
Just as a note, this is a really bad use of XML, it's not designed to
replace databases at all.
But another question that comes up is how access to the files should be
handled by different applications...the 'static' object lock handle above
only works for the application it is running in. (I think...)
Correct.
But the next question that comes to mind is that Windows already manages
file handles. It gives an error message when accessing the same file when
it is opened exclusively by another application. I think this is actually
built into file IO perhaps in BIOS.
Correct.
So maybe the question is really how do I design a file access system that
keeps an orderly access and control for the information thats in those
files?


I'd suggest creating a service to provide the information. Thus, all file
I/O would be confined to the service, and thus it could control things
itself. The consumers of the service would simply indicate the data they
wanted and be provided with it.

This would also allow you to build in such things as caching, so if a
frequently requested file was read twice in a row, you'd actually be reading
it from the RAM the second time round. In such scenarios it's importrant
that if the file is written to, the cache is flushed. However if all
communication with the files goes through your service this should not be
difficult to implement.
Nov 17 '05 #5

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

Similar topics

3
by: Marcin Vorbrodt | last post by:
So I have a class Math that looks like this: Math { public: static Real PI(void); }; Real Math::PI(void) { return 4.0 * atan(1.0); }
6
by: pembed2003 | last post by:
Hi all, I am reading the book "C++ How to Program" and in the chapter where it discuss scope rule, it says there are four scopes for a variable: function scope file scope block scope...
3
by: Datta Patil | last post by:
Hi , #include<stdio.h> func(static int k) /* point2 : why this is not giving error */ { int i = 10 ; // static int j = &i ; /* point 1: this will give compile time error */ return k; } /*...
6
by: Michael B Allen | last post by:
I want to initialize a static variable to a "random" value like: static void * get_key(struct dnsreq *req) { static uint16_t next_txnid = (uint32_t)req & 0xFFFF; But gcc gives me an error: ...
10
by: Rene | last post by:
I jus realized that I can change the values of "static variables" and "instance variable" through the standard constructor This means that something like this will compile: public class...
5
by: John Kelsey | last post by:
Back in the "old" C/C++ days, I used to declare static variables inside functions. Something like... // just a silly example to demonstrate the technique int foo(void) { static int NextVal =...
18
by: Jack | last post by:
Thanks.
2
by: drmario | last post by:
Using Microsoft VC++2008 Windows XP I don't understand, for all the reading I've just done on the subject, what the difference there is. I mean if I declare a variable with global (file) scope,...
11
by: Jef Driesen | last post by:
I have the following problem in a C project (but that also needs to compile with a C++ compiler). I'm using a virtual function table, that looks like this in the header file: typedef struct...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.