473,320 Members | 1,951 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,320 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 14845
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: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
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: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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...

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.