|
I always specify the access modifier as well. I've always distrusted my memory when it comes to default behaviors with subtle consequences. I explicitly parenthesize as well .
Software Zen: delete this;
|
|
|
|
|
I agree with Richard Deeming's response focusing on "consistency." And, I emphatically agree with PIEBALDConsult's response, when he says/implies that one should always specify the scope with Public/Private, and that it would be a good thing for the .NET compiler to "enforce" this.
I prefer to use: public/private static variabletype.
It's interesting to me that if you write (C#, WinForms) inside the scope of a Form's class definition:
public static bool availableOutsideFormClassScope = true; That ReSharper 8 will "suggest" that the use of 'public is redundant.
But, if you write this:
static bool notAvailableOutsideFormClassScope = true; , you have, in effect, declared this variable as private.
I have never had occasion to use a Form scoped static variable, method, embedded class, etc; I only use static when I want something to be accessible outside a Form's class scope. But, perhaps if I were dealing with multiple instance of the same application, or multi-threading, there would be an "organic" need for this ?
When I write applications where there are multiple independent Forms (in WinForms this can be done by modifying the Program.cs file so it calls an initializer method to "run" the application ... and that initializer creates and "shows" all Form instances ... rather than doing the usual: Application.Run(new SomeMainForm()); ... in that circumstance I usually use a ststic class defined as an an application top-level class (i.e., added via VS's Project/Add Class menu).
I know there are people who feel an almost "religious zeal" about not using static anything/anywhere, perceiving the use of static whatever as a violation of the No-Globals-Strongly-Typed supreme being, and doomed to provoke the wrath of the Threading sub-deities I have yet to have a cause to feel such a conviction.
Bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
BillWoodruff wrote: I know there are people who feel an almost "religious zeal" about not using static anything/anywhere, perceiving the use of static whatever as a violation of the No-Globals-Strongly-Typed supreme being I have a feeling you and I are of a vintage, Bill . We are grizzled combat veterans of the FORTRAN wars. We remember when men were men,
women created compilers[^], and you knew how to handle globals safely.
Software Zen: delete this;
|
|
|
|
|
Well, I'd be pleased as punch to be of the same vintage as you ... but, I suspect the year of my exfiltration from the womb, 1943, is some ways "off" from your ship-date.
However, I only started messing with computers in 1982 (after my use-by date), and I turned to vinegar early.
I do, though, use 'static anything only in circumstances where I feel there is an absolute need to do so, although I suspect I use them more frequently than the mythical "average" .NET programmer.
bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
Hmm. Physically, I'm a mite younger than you, since I was born in 1961. Professionally, I'm older, as I've been working as a programmer since 1980, when I started part-time my sophomore year of college.
Compared to our compatriots here at CP (nice little alliteration there), we're both Jurassic .
Software Zen: delete this;
|
|
|
|
|
Joining the Jurassic crew:
Born: 1957. Wrote first FORTRAN Program: Early 1970. Managed to waste quite a few reams of greenbar paper due to a column or semicolon error. Got job while in Jr. High School punching punch cards for U. Took more FORTRAN. Included my own punch cards in stacks I'd punch on my job. Got in trouble for using computer time as unauthorized user. It worked for a while but I didn't think they would check CPU use that close. Dang.
Played Star Trek on paper terminal at U in mid-70's. Got billed for excessive use of paper (AGAIN). Got access to a Vax first running VMX later converted to Unix. Got first C compiler for it sometime in early 80's. My first C program: Output "shift up/shift down" terminal commands to random VT terminals connected to Vax. I got a little foolish and had the program fork itself about 5 times. Made quite a mess of the input terminals over in the student records department. I had the service guys swapping terminals, changing cables. I feel bad now. It was not the nicest thing to do. Punishment: Had to write professors grade book keeper in C. My grade shot to A and stayed there.
Went to west coast computer fair. Bought Apple I. Loved it. Bought Apple II. Sort of loved it. Hated BASIC so started to program 6502 assembly. Hated that fact the some registers could not be used for some things. Why not!? Got 300 baud modem. Screaming now! Lit up the BBSs. Posted little programs written in 6502 assembly. One of my first was a terminal program lots of people used to send receive files via Xmodem. Used the graphics page of the apple II as a buffer. Watching programs come in was sort of physadellic as it write binary data to the graphics page.
Got 1200 baud modem! So happy! I have speed! Could not talk to any one else for some time. No one else had 1200 baud and 300 was broken in this modem.
Got IMSAI 8080 with broken switches. Fixed switches. Wrote two programs before arthritus began to set into switch toggle fingers. Turned off IMSAI 8080. Never turned it on again.
Got job coding in COBOL. Lasted about 6 weeks. Could not hack it.
Got IBM PC. Wrote loan software in C. Beta tested first ever LAN for IBM PC. Davong or something like that. Man that was fast! I could move a 330k file to my co-workers computer in a minute! Outstanding! No more walking around with stupid 5 inch floppies. Oh yea... 330k at least initialially needed to be put on several floppies. Used Digital Research C compiler. Bug list was 298 pages. Example:
sprintf(). This function converts data from variables into a string using the standard C format string characters.
Erratta: This function does not work. Avoid the use of this function.
Had to write own sprintf, and much of the other "stdlib" in that damned compiler. As soon as compiled program went over 32K segment registers would get messed up and program would crash. Had to patch that too in compiler lib. Learned to hate segmented process architecture.
Got a contract to write a meeting manager system in Digital Research Pascal. Customer owned the compiler so had to use it. Same deal. 300+ pages of things that didn't work. Fought compiler at every turn. Got program runnig with lots of self written "standard library" functions.
One week later friend of mine sends me Turbo Pascal 1.0. Opened it, looked at it. The compiler and editor were 32k. Editor was nice. First real IDE I'd seen. Instructions said hit F "whatever" to compile. Hit it, screen blinked, nothing happened. Great another broken POS compiler. Looked on disk there was my "program" .com file. Hum.... Loaded up my meeting manager. Commented out all the self written patches. Hit compile. Screen blinked on disk was MtgMgr.com. Ran it. It worked perfectly. Every function spot on. No glitches.
Stuck with TP for a while. Did a number of successful contracts in it. Still have some "Delphi" contracts and large projects. Still one of the fastest compilers out there.
During early years of Delphi got a contract doing highspeed A/D acquisition and analysis of a drop tester. They wanted to use Borland C++. Oh man was that ever a slow POS! Every little change gave me time to walk around the building. Wrote the code, got it working using a mini desktop sized drop tester breaking popsicle sticks. Got invited to see it work for real on the "real" drop tester. Standing behind a 3 inch thick plexiglass barrier about 20 yards from a tower almost 50 feet in the air with close to 5 tons loaded on it. They hit the switch and down that damn thing came. The floor jumped about a foot when it hit. It broke a truck axel in half like it was a knife chopping a carrot. On the screen a beautiful little graph appeared of the force along with various computations. Was a good feeling to see something work first time.
Used more C to do a large number of embedded projects. Did embedded assembler as well.
Now working mostly in VS and c#. Love lots about it. Hate some things. But yes... I'm jurassic too. Been around a bunch and done quite a few things.
My motto these days:
KISS first
Then you can OOP, SOLID, DI, and framework to your hearts content.
|
|
|
|
|
MarkRHolbrook wrote: Used the graphics page of the apple II as a buffer. Watching programs come in was sort of physadellic as it write binary data to the graphics page Reminds me of the graphics machines at school. They were 64K Z-80 systems running CP/M. The lower 48K was bank-switched with the display buffer, so you had to call a routine 'up high' to draw a graphics primitive. It would switch the program memory out and display bank in, draw the point/line/whatever, and then switch the program memory back in. Occasionally the bank switch didn't work properly, and you would end up watching your program on the screen.
Kind of nifty, especially when you'd been in the lab for 40 hours straight subsisting on vending machine coffee and three packs of cigarrettes.
Software Zen: delete this;
|
|
|
|
|
Hi Mark, you are a true "veteran," a true example of technical "ontology recapitulates hardware phylogeny"
I really enjoyed following the course of your pilgrimage in the digital realm. My first computer circa 1981 was a little Radio Shack Color Computer, on which I taught myself 6809 assembly language, and, of cours, Basic. My second computer was a Mac that some very young Jamaican guy in Berkeley, who was a genius with hardware, had got from some bootlegger of rejects Apple intended to go to a recycler, or to be destroyed. Robert fixed it up, and when I bought it, for about US $600 around 1984, it had a Spanish keyboard, and all the second row keys from the bottom were somehow shifted right one character: so that when you typed an "m" you got a comma
thanks, for the memories,
bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
Many of us here have been "around". I've read your stories and love every one. It's hard to be 56, have all this experience and my current "director" of software development is about 30. He's a great guy, extremely knowledgable and good at what he does but he lacks the experience people like you and I have had. Doesn't make him a bad person. We all start somewhere. He just has a blank stare on his face when I talk about 300 baud modems. LOL.
|
|
|
|
|
Hi Gary, I am sure our souls are equally eternal, even if I do have more years on the meat-package than you. My career in software was, in the context of the "big picture" of my life, a strange diversion that unfolded by the principles of serendipity's quantum mechanics (and lots of hard work).
People who had known me before my first career, in social work, psychotherapy, and academic social science, were quite astounded that a former mad-poet, and ersatz yogi, from the 1960's cultural topsy-turvy maelstrom ever made it to back to Earth in his thirties, let alone made it into university level academia. They were even more shocked when they heard, I had been published by Addison-Wesley by age 44, and later, I worked at Adobe, at age forty-five, and they saw my name in the about-box of Adobe Illustrator 3.2, and 5.0, or heard I was one of three people who created the proof-of-concept prototype that, later, became Acrobat (under the keen eye, and subtle guidance, of John Warnock, the greatest technical mind I've ever had the privilege of working directly with).
My "true love" is literature, and writing poetry, and fiction, and I'm delighted that about age fifty-one I "re-designed" my life, and moved to Asia, and returned to the worship of the Muses. But, I still enjoy the hell out of programming, still occasionally program, or do web-related stuff, or graphic design, for $, so I have not deserted the shrine of the Goddess Techne.
Along the way: wives (virtual, and legal): six. Children: zero. Ex-wives (legal): two.
Life is as good as I allow it to be !
bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
Just to clarify:
static bool notAvailableOutsideFormClassScope = true;
is in fact NOT private . Neither is it public . The default is internal (scope=assembly). As a default this makes the most sense in my opinion (regardless if having a default makes sense at all)
I still specify the visibility for every class (and member). Just because I don't like relying on defaults
Edit:
See Access Modifiers (C# Programming Guide) (MSDN)[^] First paragraph of the section "Class and Struct Accessibility"
Edit2:
Regarding Resharper (At least in Version 6.1) you let Resharper suggest explicit access modifiers:
-> Resharper -> Options... -> Code Editing -> Formatting Style -> Other -> Modifiers -> "Use explicit private modifier" / "Use explicit internal modifier"
modified 5-Sep-13 9:35am.
|
|
|
|
|
Hi Nicholas, that's a very interesting, and appreciated, response, thanks.
I am definitely a devotee of the cult whose mantra is: "declared accessibility keeps you virgin."
I studied the MSDN page you kindly sent me a link to: the word 'static does not appear in it; the distinction between 'private and 'internal mentioned there applies to classes, and structs: "Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified." The example I showed uses variables. But, I did find that page worth studying, and bookmarking, for future reference !
Given these two declarations in a Form class' scope:
static bool notAvailableOutsideFormClassScope1 = true;
private static bool notAvailableOutsideFormClassScope2 = true;
There is no difference in accessibility/behavior outside/inside the Form class' scope.
I've actually gone to the trouble, just to make sure, of testing (code on request) if there's any difference in accessibility/behavior.
In externally declared classes, one static, the other public, neither the static class, or the public class, can access those static bool, form class' scoped, variables. As you would expect, both the static class, and an instance of the public class created in the Form's scope, where those variables are declared, can use/access (set, get) those variables.
So, I see no evidence for your assertion that a static variable declared without access-modifier is, by default, 'internal, and not 'private.
ReSharper 8: Resharper/Options/Code Editing/C#/Formatting Style/Other/Modifiers dialog ... I note that I have both options, "Use explicit internal modifier," and "Use explicit private modifier:" enabled.
The default behavior in ReSharper 8, with these options enabled, is to prepend the 'private access-modifier for a static variable, after you type it.
bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
modified 6-Sep-13 1:29am.
|
|
|
|
|
|
Hi Nicholas,
I am curious now: in what circumstances have you ever used a static class, struct, or fields, or method, defined in the scope of a Form's class, and either made it private by declaring it private, or let it become private by default when you left off the access-modifier ?
I have never found a reason to do that, myself, but I suspect there are reasons for doing it under some circumstances that I'm not aware of ... and, I'd like to be aware of what possible use a private static something-or-other could be !
fyi: I went into ReSharper 8, and changed the modifier list so 'internal was at the top of the list. Declaring a static boolean variable in a Form: ReSharper 8 prepended the 'private access-modifier as soon as I completed the declaration.
It is good to learn new things, and it is good to have cause to question what one takes for granted, so I thank you for motivating me to revisit these aspects of .NET
yours, Bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
Hey Bill
BillWoodruff wrote: I am curious now: in what circumstances have you ever used a static class, struct, or fields, or method, defined in the scope of a Form's class, and either made it private by declaring it private, or let it become private by default when you left off the access-modifier ?
There are things where it makes sense to declare something as private static. And I guess if you let it default let it become private or you explicitly declare it as private is just a preference... Where mine is to explicitly tell me and everyone else which visibility I am aiming for (private, internal, protected or public).
In our code the tracer for Log4net is declared as such. Because you want to be able to log in both static and non-static methods through it, but you don't want any other class to log through that same logger.
So in this case: The tracer instance of the form is declared as private static. So you're able to use the tracer in methods of the form to log information. Sure, as long as you don't need static methods in the form class you could also declare it without the static modifier. However the tracer is declared in every class in the same manner so why bother to change it and change it back when you happen to use the instance in a static method?
I don't know about other usages for such a thing on top of my head but I guess there might be some
BillWoodruff wrote: fyi: I went into ReSharper 8, and changed the modifier list so 'internal was at the top of the list. Declaring a static boolean variable in a Form: ReSharper 8 prepended the 'private access-modifier as soon as I completed the declaration.
Yeah, there are a lot of options for Resharper. But I like it because I don't have to think about formatting my code
yours, Nicholas
|
|
|
|
|
Well if all you're writing is front-end stuff, then you wouldn't have many public members anyway. I write mostly back-end library stuff so a great many of my members are public static methods, but not many fields.
A few examples:
public static readonly System.Type BaseType = typeof(T) ;
public static readonly Rational MaxValue = new Rational ( decimal.MaxValue , 1M ) ;
public static event ErrorDelegate OnError ;
public static ValueOf<T> operator + ( ValueOf<T> Op1 , ValueOf<T> Op2 ) ...
public static System.Reflection.PropertyInfo CommandTextAttributeProperty { get ; private set ; }
Consistency allowed me to find these easily.
|
|
|
|
|
i suggest frist one style.
|
|
|
|
|
I always like to use the access modifier first.
|
|
|
|
|
I'm with Richard Deeming here, I use the first method myself. As Richard said, consistency is the key!
|
|
|
|
|
public static is more poetic. We've been using it for a decade now. Why change it?
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.
|
|
|
|
|
Vunic wrote: public static is more poetic. Just curious: is private static any less poetic ?
bill
Google CEO, Erich Schmidt: "I keep asking for a product called Serendipity. This product would have access to everything ever written or recorded, know everything the user ever worked on and saved to his or her personal hard drive, and know a whole lot about the user's tastes, friends and predilections." 2004, USA Today interview
|
|
|
|
|
Private static is still poetic.
Just static public, static private looked reverse-poetic.
Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy.
|
|
|
|
|
|
I have this code:
public void mtdProgressbar() {
prgsBar.IsIndeterminate = false;
prgsBar.Orientation = Orientation.Horizontal;
prgsBar.Minimum = 0;
prgsBar.Maximum = short.MaxValue;
prgsBar.Value = 0;
double value = 0;
prgsBar.Visibility = Visibility;
UpdateProgressBarDelegate updatePbDelegate = new UpdateProgressBarDelegate(prgsBar.SetValue);
do
{
value += 1;
Dispatcher.Invoke(updatePbDelegate, System.Windows.Threading.DispatcherPriority.Background,new object[] { ProgressBar.ValueProperty, value });
}
while (prgsBar.Value != prgsBar.Maximum);
prgsBar.Visibility = Visibility.Hidden;
}
But i can not create the Thread do fill progressbar, do not work.
The code:
.
.
.
else{
System.Threading.Thread trd = new System.Threading.Thread(new System.Threading.ThreadStart(this.mtdProgressbar));
trd.IsBackground = true;
trd.Start();
mtdTest();
}
I'm using C# and Wpf
|
|
|
|
|
Threads can be tricky, what you are running into is a problem with access.. threads by default can not access the ui to update a progress bar because that may create a deadlock.
For the background worker there is a function built in for report progress, but it is limited to only allow for simple updates.
You can however, get around this requirement by doing
.Invoke((Action)delegate() which allows safe access to the ui through a delegate.
I have used this method to update multiple labels, etc.
That should do the trick.
=)
|
|
|
|
|