I've gotten very tired of making some of my lines of code nicely indented using TAB and then having ctrl+E and D removing them all :-/
For instance.
Button btnCalculate = new Button();
EventHandler hndCalculateFinalGradeHandler;
Becomes...
Button btnCalculate = new Button();
EventHandler hndCalculateFinalGradeHandler;
B4 anyone says to just stop using ctrl+E and Dm I find it very usefull for getting a general layout when i'm in a lazy mood ;) heheh
compavalanche
July 17th, 2008, 03:33 PM
Why are you identing that line? I fail to see any rule you could give so a machine could know why you choose to ident that way and not indent
int i;
int j;
the same way.
So in short I think what your looking for is impossible. But I could be wrong.
cjard
July 17th, 2008, 07:02 PM
in essence, i agree with compavalanche.. your first code sample is much uglier than the second. if you wish to distinct Events from member variables, use regions, not indentation. indentation indicates scope.. dont violate established rules please, you might not be the only person who has to maintain your code
Zirus Blackheart
July 18th, 2008, 02:44 PM
Well, i'm learning event handlers, and being able to see at a quick glance weather the event handler declarations, registering, association with a certain event like .Click ect are in my code by simply seeing if theres an indent in the 130 lines of code makes it alot easer to read.....
I'm not sure what you mean by regions.... could you give me an example? thx :)
Arjay
July 18th, 2008, 02:58 PM
Use regions as suggested. Indenting here just goes against convention so much it's almost criminal.
#region Buttons
Button btnCalculate = new Button();
Button btnTardy = new Button();
P.S generally the end regions are usually typed as
#endregion
For readability, I typed them with the same tag as the opening region, e.g.
#endregion Buttons
I like this so much, I have a patent on it. :)
Btw, regions can be collapsed as well (or nested but that's another discussion).
Collapsed, the above regions would look something like:
+ Buttons
+ Button Event Handlers
Zirus Blackheart
July 18th, 2008, 03:59 PM
But thats not very encapsulated though (yes i know thats not the 110% correct useage of the word, but the point is u understand what i am saying), I like to keep everything for that button right next to each other, it makes editing said button alot simpler, if I was to split it up into regions as above it would mean alot more jumping up and down though the code checking back and forth if this has that and does this have that.... thats just gonna cause posible errors for a learner ;)
# region "Calculate Final Grade" Button
btnCalculate.Text = "Calculate Final Grade";
btnCalculate.Location = new Point(5, 135);
btnCalculate.Size = new Size(400, 25);
hndCalculateFinalGradeHandler = new EventHandler(BtnCalculate_Click);
btnCalculate.Click += hndCalculateFinalGradeHandler;
Controls.Add(btnCalculate);
# endregion "Calculate Final Grade" Button
The regions are gonna be damned usefull though, allways hoped there was a way to create a shrinkable area, cheers :-)
Arjay
July 18th, 2008, 04:38 PM
You know, all the code you are writing gets written for you by the forms designer.
compavalanche
July 18th, 2008, 07:23 PM
Can you just use another scope block:
# region "Calculate Final Grade" Button
btnCalculate.Text = "Calculate Final Grade";
btnCalculate.Location = new Point(5, 135);
btnCalculate.Size = new Size(400, 25);
{
hndCalculateFinalGradeHandler = new EventHandler(BtnCalculate_Click);
btnCalculate.Click += hndCalculateFinalGradeHandler;
}
Controls.Add(btnCalculate);
# endregion "Calculate Final Grade" Button
JonnyPoet
July 19th, 2008, 03:11 AM
Can you just use another scope block:
# region "Calculate Final Grade" Button
btnCalculate.Text = "Calculate Final Grade";
btnCalculate.Location = new Point(5, 135);
btnCalculate.Size = new Size(400, 25);
{
hndCalculateFinalGradeHandler = new EventHandler(BtnCalculate_Click);
btnCalculate.Click += hndCalculateFinalGradeHandler;
}
Controls.Add(btnCalculate);
# endregion "Calculate Final Grade" Button
Oh goodness NOOOO ! This would cofuse any foreign reader of your code. I would always look why the block is in brackets, looking for any condition which jumps into it or mybe ignores this part of code. If you have such a lot of code really in one form ( for example 30 buttons, 10 listboxes, 20 combos 3 TabControlers cascaded into each other for a very complex Userinterface
a) you can devide your class in partial classes
b) Overthink your design. Is this really useable without reading hundret pages of explnations before.
People in general I think dislike to read user guides. They want to sit down in fromt of the PC, opening your program and it should be in a shape people dosn't get confused by it !
If you do in that way...
There will not be any problem to loose track over your code.
Your system putting together all code of a single control, excuse me, seems me a bit of short sighted,
Think to a textbox , a listbox and a button. You may want all of them to interact with each other for example filing some text in the zextbox pressing a button and by that filling the text into the list.
Where do you plac this code together. All to the button ? to The textbox validation event ? To the listbox becuase you need Add method there ?
Organize your code by logical needs. Or maybe you will sort them by Regions like Fields, Constuctors, Properties ( suboder into public and private if needed ), Methods, delegates, overrides. permanently keeping one system in all of your code makes it easy for you to maintain your code for years. The above system -- Never, no way.
code?
July 20th, 2008, 04:26 AM
Could you just use a region, then indent the region name like this...
It will look like this...
rssView.NextArticle();
rssDescriptionView.DisplayItem = rssView.SelectedItem;
| RegionTextOR
rssView.NextArticle();
rssDescriptionView.DisplayItem = rssView.SelectedItem;
- RegionTextOR
rssView.NextArticle();
rssDescriptionView.DisplayItem = rssView.SelectedItem;
+ RegionText
Seems that spaces will just erase unless it is after another character...
There is no other way.
JonnyPoet
July 20th, 2008, 01:41 PM
Could you just use a region, then indent the region name like this... ....There is no other way. ???? :ehh:
I dont get what you want to communicate wth this.
You can use region and endregion and if you want you can Tab the regions so they dont begin in the left side something like
public class MyClass{
#region private Fields
private bool understanding = false;
private bool itsEasy = true;
//... all your fields
#endregion
#region ctors
///<summary>
/// This Class does nothing and is only to demonstrate regions
///and summary
///</summary>
public MyClass(int whatever){
//... whatever
}
// other Ctors...
#endregion
// and so on
}Simple try it and you will see how this works. Also use the class in a demo and look what the intellisense shows you. You can use summaries for getting text to all your properties and methods of a class.
Zirus Blackheart
July 22nd, 2008, 04:50 AM
Ha :-)
Thx alot for all ya help, im going with just regioning out the handlers themselves then shrinking, the gray region box is very easy to see when i scan through my code and does the job perfectly, thx alot :-)
As far as what to put where as JonnyPoet was saying, my current way of doing it is to place all the dot notation attributes that set up said object like .Text and .Size together and also have the event handler instantiaion with them which has the method name
hndResetButtonHandler = new EventHandler(BtnReset_Click);
and its registration line with the event
btnReset.Click += hndResetButtonHandler;
and the Controls.Add(btnReset); line too.
The method that runs is of course gonna be seperate and lower down in my code but the instantiation line shows me the methods name.
I havn't gotten to the stage of having multiple events per object in my form yet so it might change then, but I don't see why when they will just be a further event handler line in there, but like i said I havn't gotten that far yet so i can't really comment..... :-)
You know, all the code you are writing gets written for you by the forms designer.
Yes, but the book i'm learning from doesn't teach via visual studio (it's no biggy trust me, im using visual studio) so it's teaching me all these lines ect which i think as a learner is it's good to learn b4 lettign visual studio do that for me :-)
cjard
July 22nd, 2008, 06:34 AM
But thats not very encapsulated though (yes i know thats not the 110% correct useage of the word, but the point is u understand what i am saying)
Er, no. Actually, especially when you talk to a programmer, using a word that you assert means Y when it actually means X, is not a good idea.
For example, tell me to format your C drive, than come back and exclaim "oh no! why arent all my files and folders nicely formatted into TitleCase? Thats what I meant by format!"
I like to keep everything for that button right next to each other, it makes editing said button alot simpler,
Aside from the fact that you can never put all code that refers to your button in the same block (what happens if you say MyButton1.Text = "Hello" in the middle of a constructor? OH NO! IT'S NOT NEAR WHERE THE BUTTON WAS DEFINED! Better move it, and then add a GoTo into the constructor right? That makes for way more readable code. (i.e. trying to put all code for a button in one place is silly as well as impossible)
I was to split it up into regions as above it would mean alot more jumping up and down though the code checking back and forth if this has that and does this have that.... thats just gonna cause posible errors for a learner ;)
Oh, come on! it sounds like making arguments for arguments sake. You need to stop dedicating yourself to learning a path that NOONE else uses. It wont make you able to integrate into the world of programmers out there at all (development *teams*?)
Nothing stopping you using regions like:
#region All My Button1 Stuff
//button1 stuff
#endregion
#region All My Button2 Stuff
//button2 stuff
#endregion
But it still doesnt violate the point that indentation is universally understood to signify scope. Same scope? Same indentation. Dont break these rules because they are what we all play by. If you one day want some help and post on a forum, a real dog's dinner of a code block, then people are going to be very reluctant to help you
cjard
July 22nd, 2008, 06:48 AM
Ha :-)
Thx alot for all ya help, im going with just regioning out the handlers themselves then shrinking, the gray region box is very easy to see when i scan through my code and does the job perfectly, thx alot :-)
How you justify writing 3 lines of code where one will do, on the proviso that the 3 will shrink to 1, hence take up the same amount of code as the original line of code?What did you gain?
Button b = new Button()
#region Button b Event Handler
ButtonHandler bevt = whatever
#endregion
Button b = new Button()
[Button b Event Handler]
Seriously.. what's the point? Anyone reading the code will know it's a handler, you dont need to label it for them any more than you need to write these comments:
//this is a button called b
Button b = whatever
//this is the button handler for b
ButtonHandler bevt = whatever
100% useless comments. Your code should be self documenting enough to convey this info. If you have to explain your code, you need to write it better
As far as what to put where as JonnyPoet was saying, my current way of doing it is to place all the dot notation attributes that set up said object like .Text and .Size together and also have the event handler instantiaion with them which has the method name
as in:
Button b = new Button()
#region Button b Event Handler
ButtonHandler bevt = whatever
#endregion
#region Button b Text assignment
b.Text = "Hello"
#endregion
#region Button b Color assignment
b.Color = Color.WHite
#endregion
Confusing to read, no?
The method that runs is of course gonna be seperate and lower down in my code
Funnily enough, when you drag your button onto a form and set its properties, the designer writes all that code, neatly clumped together in a separate class file for you. I believe arjay pointed this out
Yes, but the book i'm learning from doesn't teach via visual studio (it's no biggy trust me, im using visual studio) so it's teaching me all these lines ect which i think as a learner is it's good to learn b4 lettign visual studio do that for me :-)
Do you own a bicycle? Did you build it yourself? Did you stop by the spoke manufactureres and ask them how they forged the rods, or ask the tyre makers what compound the rubber is? When you make a cup of tea, do you disassemble the kettle to understand how it works before you boil it? Closer to home, did you look up how windows draws a button, or communicates the fact that you clicked on it? Probably not. There are some things in this life that you do not do because there is no point; putting vast amounts of time into learning something low level that you will never use (because it works, ain't broken and if it was you could easily learn then how to fix it at that time) . By all means create your UIs by hand, but consider that this sort of thing was delegated to a designer long ago because:
It's easy for the designer to get it right, quickly
It's easy for a human to get ir wrong, slowly
It's tedious and repetitive and obeys simple logic, and computers excel at such tasks; humans do the higher-intelligence-requirement/varied-logic-required parts
That said.. if your goal is to create java 2 microedition apps fopr old mobile phones using notepad, go right ahead ;)
cjard
July 22nd, 2008, 07:27 AM
i DO NOT CARE! if its how it should look in the world of working as a coder!
I AM NOT WORKING AS A CODER! it is ONLY for NOW while i'm learning for this one thiong right now, in 5 days time, THEY WONT BE INDENTED!
ARGH!
So the wisdom of "start as you mean to go on" and "do it right, first time" are somewhat lost on you? Trust me, in coding especially, you should always try to get things right first time and not put in hacks.. A cornerstone of the company I work for is a seriously nasty hack of an app that I wrote 3 years ago and lost the source code to. It's so vital and has so many little quirks that we spend too much time trying to work round, thatI truly wish I'd put my foot down at the start and demanded a month's dev time for it, not the 3 days I got. Its replacement exists in beta but keeps being shelved while the next big panic is overcome and while I'm amazed that it still works 3 years on, it's the bane of several lives. If you take nothing else away from this discussion, please.. Get the notion of getting it right, from the start. (Oh, and take regular backups.. I've never quite lived down the foolishness of not preserving my work before that day my hard disk started going ker-tick, ker-tick, ker-tick.. :) )
Why the hell would I do that?
Why would you say you wanted to do that, when you didnt?
I wasn't talking about that at all,thats setting something in the button the damned place in your code when that setting of it should happoen!
I fail to see a difference. You want everything related to a button in one place; you said so. In fact you want it all so-in-one-place that you cannot tolerate even a few lines distance from the "buttons declaration region" to the "event handler region" - so you seem to be wanting every item of code pertaining to a button, in one place?
My aim here is to make you think about the logic behind the organisation of your code, and why we (professional developers) dont organise like that. There is a reason, usually championed by human laziness; we always find the easiest way of doing something, and that's often an efficient one that carries extra merit if it is defacto standard
Yes I built my first bike (same with motorbike).
Interesting. How long did it take you to true the rims by adjusting spoke tension? Must have cost quite a bit for the spoke threading tool when you lathed them up, right? (Ours was over $1200, for a widdly little thing that merely grooves a thred into the end of a spoke).. Was your bike a 2 stroke or a 4 stroke? Always preferred 2 for offroad myself; had an older one without a motorised powervalve, so it was either grunty but gutless at the top end (better for riding in the sand quarry nex tto the park; quieter), or flat as a pancake until about 5k, after which it only took a few hours to strip the rear tyre of its tread on the slag heaps where we rode..
Yes i have taken the kettle apart to see how it works.
So.. uh.. How does it work? You know.. the flicks-off-when-it-boils mechanism? Mighty handy for defrosting a fridge I might add, just set it to boil inside, lid off (with an extension lead) and shut the fridge door..
[qutoe]Yes I was curious how rubber was made.[/quote]
Howstuffworks is indeed a truly fascinating site
Just don't post replys to my posts anymore, u sicken me.
Good points to those on shaky ground usually have that effect, yes..
Now, back OT - did you follow up Arjay's notion of using the designer? Youre allowed to read the generated code, you know! I'm sure a curious george such as yourself would be satisfied with reading it, knowing where it was if you ever needed it, and then getting on with some real work.. I know I was (thoguh I have to admit I did used to do my Java GridBagLayouts by hand, and I taught my uni lecturer how they worked. Really wish I'd spent the time chasing girls and using Netbeans tho ;) )
JonnyPoet
July 22nd, 2008, 01:54 PM
So the wisdom of "start as you mean to go on" and "do it right, first time" are somewhat lost on you? Trust me, in coding especially, you should always try to get things right first time and not put in hacks.....Very interesting post, because I cannot find the post where all the quotes are from ;) . Was there any post which was deleted from Zirus Blackheart. The sound which cames up remembers me to an nordic guy we have had here approximatly two years ago. :D
In all such cases I really would want to know from these guys, why are they asking for help when they refuse to learn from the people they have asked ?
BTW I also did my first byke myself. My father has learned me all that stuff as he was a trained sewing machine technician. An we havn't had much moes and this was after the second world war.. And it learned me a lot. It learned me to do all what I want to do, as professioal as possible from the very first moment. And Cjard obviously also knows very well what he is talking about.:D
And the first time I started with C# the code reading from the designer created code speeded up my knowledge about coding in a huge amount.
In this one reading of one page of code I could learn :
a) how to create objects using C#
b) how dispose works
c) how to stop unwanted side effects during initialisation of a form using ISupportInitialize
d) How to add delegates
e) usage of region - if you would have studied the code you wouldn't have needed to ask for that Zirus
f) how to add controls to a form and to set their properties.
...
This is a small part of the full list only for getting an idea of what you CAN learn if you want to learn.
If you dont want to learn, - its easy - simple dont ask.
cjard
July 22nd, 2008, 07:25 PM
Very interesting post, because I cannot find the post where all the quotes are from ;) . Was there any post which was deleted from Zirus Blackheart.
There was.. He wrote it, I quoted a reply, he deleted his post in the interim
In all such cases I really would want to know from these guys, why are they asking for help when they refuse to learn from the people they have asked ?
Probably because of the way I put it across. I'm not particularly finessed with respect to the feelings of others (i'm blunt) nor am I overly worried about becoming so. I love a good debate, but I'm usually found emphatically arguing a point I firmly believe to be right. To save myself some embarassment I do usually ensure that I am right, but some people dont like to be told in the way that I prefer to tell and a backlash is something I've come to expect, occasionally look forward to.
To whit and in summary, I can be a smartarse and in this day and age of political correctness, it gets people's backs up. I could change the way I deliver my diatribe, but there's something about the efficiency of bluntness that appeals to me.
BTW I also did my first byke myself.
The other thing I was musing recently.. we can tell when we start living in a truly first world advanced country, twhen it is more cost effective to go out to our job and work for a day, and use the money to pay a mechanic to fix the car. he does cars all day every day and will do the job faster than we can, which means less cost. If we take a day out to fix what he can fix in half a day, and charge us a quarter of our day's earnings for it then we are 2 times better off to get him to do it. Put another way, would you rather have:
Fixed car, dirty hands, miss your afternoon appointment, $0
Fixed car, clean hands, make your afternoon appointment, $300
The one inviolable thing i've found is that the love you or I put into doing a good job can often surpss the quality of work put in by one who is being paid to do it.. but then if it fails we ahve only ourselves to blame.. whereas if someone else did the work, they have to fix it..
It all adds up to the point where being a master of everything is not needed any more - just specialise, earn, and buy in the skills you lack.. Which brings us nicely back around to "buying in" the "skills" of the forms designer to do the work for us..
But, as ever, I digress..
JonnyPoet
July 23rd, 2008, 06:22 AM
But, as ever, I digress..Hehe, yes, but its funny. I fully agree with you. But myáybe youngsters are too young to understand the worth of their own time. But you can be sure, he will learn it, at least when he grows up to 60 :D like me
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.