By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,507 Members | 1,200 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,507 IT Pros & Developers. It's quick & easy.

begginer

P: n/a
HI, please forgive this question. Im new to c# and as you probably seen
from prev questions Ive got along way to go. Ive been woking my way
through a couple of books, asking questions here and Id say im
improving. After asking a couple questions yesterday I realised theres
one arae that I just dont seem to grasp, and the books dont make clear.
Basically Ive got code which reads a text file, grabs html from web
site and writes to another text file, in the meen time keeps user
informed as to how many records processed. Upto now ive used a form
with a button and texbox, and put all code in the buttonclick command.
And this is the problem were should the bulk of the code be written. Do
I add a class to my project, put code there, and call it with button.
So far ive used class files in the reference of the project. Im reading
about threading which is what made me think Im putting my code in the
wrong place. And all though the books say classes are one of the most
important areas, there not very clear on what to do when writing alot
of code. Hope this makes sense.
Regards Robert

May 24 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
"Ro********@yahoo.co.uk" <Ro********@yahoo.co.uk> wrote:
And this is the problem were should the bulk of the code be written.


For simple algorithms, a single method is fine. If you're going to reuse
it, or if it is going to be maintained separately, it should be put into
a separate method and called rather than left inline.

Where you see the code do something again and again, refactor the
repeated code as a separate method, and replace the original code with
calls to the method. Add parameters if that's necessary to get the same
effects. Similarly, if the code is doing different things that can be
logically packaged, refactor. If the code is doing too much to
understand easily, refactor. This is called functional decomposition.

Where you need to share variables among a group of functions, look for
logical entities to which these variables logically belong, and move the
variables to fields of classes, and move the methods which used these
variables to methods of these classes. This is called object-oriented
decomposition.

Both decomposition techniques require experience and a developed skill
to get right. Developing larger programs, reading existing code and
examining how well-designed frameworks such as the .NET frameworks are
put together helps develop this skill.

There are higher forms of (de)composition, involving more meta-notions
that aren't explicit language structures, such as design patterns. If
you read about these after you've seen a few frameworks, you'll start
recognizing them.

Good luck!

-- Barry

--
http://barrkel.blogspot.com/
May 24 '06 #2

P: n/a
Thanks Barry for your reply. Plenty of errors for me to read up on.
Thanks for taking the time to point these out.
Regards Robert

May 24 '06 #3

P: n/a
"Ro********@yahoo.co.uk" <Ro********@yahoo.co.uk> wrote:
Thanks Barry for your reply. Plenty of errors for me to read up on.
Thanks for taking the time to point these out.


Uh, they weren't errors as such, just things to keep in mind as you
learn. That's all!

-- Barry

--
http://barrkel.blogspot.com/
May 24 '06 #4

P: n/a
Sorry I meant Areas.
Regards Robert

May 24 '06 #5

P: n/a
Sometimes an example helps too so for your text reading thing i guess you
have something like

public void ButtonClick(object sender, e eventArgs)
{
for(int i=0; i<numRecs; i++)
{
//get record and read it
//do all your stuff with it
//inform user
//write text file
//do something else
//etc etc etc
}
}

Reulting in a big button method. As Barry said break this down into
something logical and easy to read and keep in mind scalability and
maintenance.

Now say you do a new project and think, that code i wrote for the text
reader was good lets reuse that, you will then go into that button, copy the
code and paste to a new project. Say you use it in 6 projects and along the
way you update the code or someone else wnats to use it this is a problem.
Just one problem of many from not doing this right to be honest.

So you break it down by making a new class and create get and set methods
and so on. A good starting point to understand classes is to write a basic
calculator.

At the moment i bet you'd do something like this:

Public ButtonClick()
{
switch(choice)
{
case divide:
return x/y;
case multiply:
return x*y;
}
}

etc

But a better way is a new class called say Calculator and you'd make methods
such as these in that class

public int Add(int x, int y)
{
return x +y;
}
public int Subtract(int x, int y)
{
return x-y;
}
public int Divide(int x, int y)
{
return x/y;
}

etc

Now your button click code would become

Public ButtonClick()
{
Calculator myCalc = new Calculator();

switch(choice)
{
case divide:
return myCalc.Divide(x, y);
case multiply:
return myCalc.Multiply(x, y);
}
}

I know in this case they look very similar but the second one is much more
scalable and easier to read. So now if you did a new project and wanted to
use your calculator you need only add the Calculator clas to it and voila
you can use it as above. My example above is VERY basic but better to show
that at the stage you are at. Imagine the methods were much bigger and you
can see the benefit, Easier to read, easier to manage and if you decide to
say add a handler in the Multiply method you just go to the calculator class
and change it there and everywhere the method is called is updated.

Dunno if that helps your understanding of the basics how and why's?
<Ro********@yahoo.co.uk> wrote in message
news:11**********************@38g2000cwa.googlegro ups.com...
Sorry I meant Areas.
Regards Robert

May 24 '06 #6

P: n/a
Thankyou for your reply. Sorry for delay in answering, Ive been away.
This is great and much appreciated. Ive started practessing with my
code and others. When you start t obreak down the code to smaller
pieces its alot easier to understand whats going on. Thanks for taking
the time.
Regards Robert

May 26 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.