Writing Your First Class

Chapter 3

Writing Your First Class

In Chapter 2, "Object-Oriented Programming in Visual Basic .NET," I described the object-oriented approach to programming by using existing .NET Framework classes. I've found that using existing classes is a good way to get your mind around what you need to know about objects. But if you're like me, the best way to understand a programming concept is to write code that uses it.

In this chapter, we're going to write a pure class sans graphical interface. Even though we saw that Windows forms almost write themselves in Visual Studio .NET, it's important to strip away whatever might detract from understanding the fundamentals of classes when beginning to learn what they're all about. By writing a bare-bones class, we can cover every aspect of building a class and prepare ourselves for the chapters to come.

Following this approach, you'll also get a sense of the way in which many classes are constructed for production applications in businesses. We will write a class, named Employee, that holds information about employees. The class will be streamlined, but it will hit all the highlights of object-oriented programming (OOP), such as encapsulation, overloading, overriding, creating properties, creating a read-only property, private member fields, and using a shared data field. While we're at it, we'll add a #Region to demonstrate how you can create your own module view, making your code easy to work with by hiding and exposing various parts of the code. In this short example, we'll also create our own namespace. This simple class will cover all of the concepts needed to write sophisticated, robust Visual Basic .NET programs. Once you understand these important concepts, constructing more meaningful and robust classes will be straightforward.

note

While I stated that this class does not inherit from any predefined class, it does inherit from the System.Object class. It's impossible to do anything in Visual Basic .NET without implicitly inheriting from System.Object.



Coding Techniques for Microsoft Visual Basic. NET
Coding Techniques for Microsoft Visual Basic .NET
ISBN: 0735612544
EAN: 2147483647
Year: 2002
Pages: 123
Authors: John Connell

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net