class vs functions...

dottedquad

New member
Joined
Jun 19, 2005
Messages
1
Programming Experience
Beginner
I picked up a book to learn VB.net and I tought my self how to code a class. I thought to myself I could also code that in a function. My question is, why would you use a class over a function in certain circumstances?

-Thanks
 
1st off, you shouldn't consider class and function like something that should be compared ... feel free to forget that right now!

The heart and soul of OOP - Object Oriented Programming is code reuse (read classes) and there are many object-oriented features that should be taken under consideration as you can find a bit sense of what you claimed couple hours ago (inheritance, access modifiers and scoping issues, abstract classes, design and implemention of interfaces and design patterns, refactoring and couple more advanced, reflection, object persistence, and serialization). Try all that with only functions :p

However, before you decide that certain code is candidate for class you need to consider many factors. Means that you don't have to make a class for everything there while it is unnecessary.

At the beggining you'll find out that reusable modules are always your best candidate for OOP.


Cheers ;)
 
I'll add my two cents. Most of the examples in beginner books are pretty simple and therefore the benefits of OOP are not immediately apparent. The first thing to note, though, is that if you want to re-use that function in another project, you would have to rewrite it in that project. Maybe that's not a big deal for a five-line function. What if your function is 1000 lines? You could still copy and paste. What if that function was written by a different developer and you didn't have the source code? With classes, you can plug them into any project you like without knowing anything about their inner workings. If you have the details of the inteface you can use them anywhere. Then there are all the benefits that kulrom listed. Believe me, for even simple projects OOP offers great benefits. All the built-in .NET objects are classes, so you're already saved months of work by their existence. If OOP doesn't save you time then you aren't doing it properly. One of the most important things is to plan properly so that you implement your objects correctly.
 
Class Or Functions

There are gr8 amount of advantages of using class rather then a function in a module.
For simple applicaton when encapsulation is not required...you can write functions in a Moudule giving it a Global access. Or Function within the Form class

Talking abt C#.net where modules are not provided coz of safe programming concept...u have to use Static Block in a class to have a similar functionality as Module.

Still if u r not satisfied with the reply.....give me a proper senario I'll explain u Y to use class and ......

Thank You
Software Engr
 
Back
Top