Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3
This is the forum to discuss the Wrox book Beginning C# 3.0 : An Introduction to Object Oriented Programming by Jack Purdum; ISBN: 9780470261293
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning C# 3.0 : An Introduction to Object Oriented Programming ISBN: 978-0-470-26129-3 section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developersí questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old January 9th, 2009, 06:08 PM
Registered User
Points: 44, Level: 1
Points: 44, Level: 1 Points: 44, Level: 1 Points: 44, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2009
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Smile static vs. global member

Mr. Purdum,

This is the 2nd questions in a row.
I hope this should be a last one, at least for the time being, as I don't want to take to much of your time with my own questions.

Anyway, is the static and global same as a concept for the access specifier?
You said static storage class pertains to the class itself not the specific instance.
Am I right if I say static member is same as global member, with the difference that static is a "read-only" one?

Thank you in advance, Mr.Purdum.

Sincerely yours,
hk
Reply With Quote
  #2 (permalink)  
Old January 9th, 2009, 08:22 PM
Friend of Wrox
Points: 1,104, Level: 13
Points: 1,104, Level: 13 Points: 1,104, Level: 13 Points: 1,104, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 224
Thanks: 0
Thanked 30 Times in 29 Posts
Default

Hi Hkyoon:

Don't worry about asking multiple questions. I don't mind trying to help if I can.

Don't think of static data as global data...it is not. There are, however, two things that make static different. For example:


public class clsMyPhoneList()
{
private static String[] Names = new String[1000];

}

In this example, Names is private and is only visible within the clsMyPhoneList() class. I will need to write a property method if I want to use it outside the class. In that sense, a static data item is like any other class data item. But here are the differences:

1) Static data items are only defined once. If your code created 100 clsMyPhoneList() objects, you would still only have 1 Names array allocated. Why did Microsoft do it this way? Well, think about it. All 100 objects probably need access to the same list of names, so why duplicate the array 100 times. It saves memory space this way. Also, suppose you're doing some math calculation that uses pi. Do you need a 100 copies of the constant? No, so make it a static and only 1 is created, but shared within the 100 objects.

2) Static data items are defined at load time on the heap. Because Visual Studio knows that any static data item defined anywhere within the program is defined only once, it always allocates static data items in the heap. The reason is that, unlike the stack which ebbs and flows as the program runs and methods are entered and exited, a static data item must always be available to all instances of the object. By putting the item in the heap, Windows always knows where it is. Also, all static data are defined the instant the program is loaded into memory. This means that, even if you never create a clsMyPhoneList() object, you will still have one copy of the Names array allocated on the heap.

Dr. Purdum
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
Reply With Quote
  #3 (permalink)  
Old January 9th, 2009, 11:12 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

By the by, Dr. Purdum neglected to mention that static members are *NOT* readonly, at all.

Only const members are readonly.
Reply With Quote
  #4 (permalink)  
Old January 9th, 2009, 11:30 PM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default WARNING: Pedanticism, again

Again, Dr. Purdum's answer is just fine for those starting out. And if I wasn't such a [censored] I'd leave it alone. But for those who might be "up one level," once again the devil is in the details.

Don't think of static data as global data...it is not.

True, but if a static member is public, it's hard to consider that it is in any real way *not* "global data."

Why did Microsoft do it this way?

Yes. But why stop with Microsoft? Why do so many modern languages do it this way? And even some not-quite-so-modern ones, such as C.

... Because Visual Studio knows that any static data item defined anywhere...

I guess so. Sort of. But shouldn't we REALLY say "...because the C# compiler knows..."??? I mean, I doubt that the other parts of VS--the ones that show the pretty graphics and allow database connections and the editor and so on and so on have any knowledge at all of the working of any given compiled program.

Also, all static data are defined the instant the program is loaded into memory.

I know this is just a slip of the wording. Static data is of course *reserved* when the program is loaded into memory. But it won't be "defined" (that is, contain usable data) until something in your program initializes it. [The exceptions are static initialized members, of course.]

**************

Hope you don't mind too much my being a pain in the patootie.

And KHYOON, if you are reading this, you should ignore this level of detail. Come back in a year or so and my remarks will make more sense to you, I bet.
Reply With Quote
  #5 (permalink)  
Old January 9th, 2009, 11:51 PM
Friend of Wrox
Points: 1,104, Level: 13
Points: 1,104, Level: 13 Points: 1,104, Level: 13 Points: 1,104, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 224
Thanks: 0
Thanked 30 Times in 29 Posts
Default

"Also, all static data are defined the instant the program is loaded into memory.

I know this is just a slip of the wording. Static data is of course *reserved* when the program is loaded into memory. But it won't be "defined" (that is, contain usable data) until something in your program initializes it. [The exceptions are static initialized members, of course.]
"

Actually, it is not a "slip of the wording". Any static is defined the moment the program is loaded into memory. It has an lvalue (an allocated memory address) and it is automatically initialized to zero if it's a value type or null if it's an object, or whatever values are in the initializer list, if there is one. The words "define" and "declare" have very different meanings and statics truly are defined at load time.

I stand by what I say both here and in the book, especially for the readership of the book. I learned long ago that students ask questions for one of several reasons. One, they genuinely want to learn the answer to the question or, two, they already know the answer to the question and just want to show off for everyone else in the class. Teachers love the first type of questions. The second type...not so much.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
Reply With Quote
  #6 (permalink)  
Old January 10th, 2009, 12:52 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,652
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Yes, I clearly need to concede that point. I *did* use the word "initializes" in my answer, and I should have stuck to that instead of incorrectly using "defined." My real point was only that you can't count on the data having any usable value until you initialize it. (You may or may not be surprised by the number of newbies who don't tumble to that; they seem to assume that it magically has the value they want.)

Sorry about that. 40 lashes with a wet compiler manual for me.
Reply With Quote
  #7 (permalink)  
Old January 10th, 2009, 11:13 AM
Friend of Wrox
Points: 1,104, Level: 13
Points: 1,104, Level: 13 Points: 1,104, Level: 13 Points: 1,104, Level: 13
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2008
Location: Indianapolis, IN, USA.
Posts: 224
Thanks: 0
Thanked 30 Times in 29 Posts
Default Define vs declare

Not a problem. While you understand the difference, one of my pet peeves is that most programmers treat the words define and declare as synonyms, which they are not. From page 83 of my book:

A variable is defined if, and only if, the lvalue column for that variable in the symbol table has a memory address assigned to it. When you declare a variable, the lvalue column is the symbol table for that variable does not contain a memory address.

This is consistent with the K&R distinction of the terms in their pivotal book. A data declaration is like the C function prototype statements--the purpose of which is to give the compiler an attribute list for type-checking purposes. (An extern data type in C is a good example--the actual memory allocation is done elsewhere in the program, but the statement allows the compiler to type-check its use in the current file.) The real problem is that the distinction is lost on a huge segment of the programming community. Indeed, all of Microsoft's documentation seems to make no distinction between the two terms. If all programmers used define and declare correctly, newcomers like Mr. Hkyoon would have a much easier time of it and that's really my ultimate goal--to make Mr. Hkyoon's learning experience as easy and enjoyable as possible.
__________________
Jack Purdum, Ph.D.
Author: Beginning C# 3.0: Introduction to Object Oriented Programming (and 14 other programming texts)
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to declare the global variable in global.asax? calyn_gately ASP.NET 3.5 Basics 0 August 6th, 2008 08:06 PM
Hi.. new member here.. renai C# 1 August 26th, 2007 11:20 AM
non-static reports to static html files miamikk ASP.NET 2.0 Basics 0 June 4th, 2007 01:48 PM
Hello from a new member aehb C# 0 March 10th, 2005 04:51 PM
Static members global to client apps Jnye ASP.NET 1.0 and 1.1 Professional 0 March 16th, 2004 12:41 PM



All times are GMT -4. The time now is 07:13 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.