Exploiting the Framework

Team Fly 

Page 18

If everything in .NET is an object, then everything is—at least abstractly—a data type. Fortunately, it's not this bad; within a given class, such as Color, there are enumerations that in themselves aren't, technically, objects. However, most VB programmers are used to just assigning a simple string (or at least a built-in enum, such as VBBlue) to many different properties. For example:

 Backcolor = ''blue" 

Now, in .NET, you must either use Imports to bring in a specialized namespace, or fully qualify your value:

 Me.BackColor = Color.Blue 

The System.Drawing namespace is now included in current VB.NET projects by default, so you don't have to fully qualify this one. The underlying problem here, though, is: How can you know that there is a Color class, and that it's located in the System.Drawing namespace? How do you deal with unfamiliar parts of the .NET Framework? Color might be easy enough to imagine, or you might finally come across example code by using the Help search feature to locate entries with BackColor =. But how do you deal with more complex situations, such as the Security namespace? Most books on VB.NET avoid discussing the System.Security assembly precisely because it is both obscure and massive. This in spite of the fact that communication and security are the primary issues that distinguish the .NET world from classical VB programming. Put another way: OOP itself is fundamentally a set of rules designed to solve communication and, especially, security problems. Code reusability, the primary justification for OOP, is largely an attempt to enforce communication rules to solve security problems—though doubtless OOP theorists will consider this a reductive generalization.

Exploiting the Framework

But back to our regular programming. What are the best strategies for tapping into the tremendous power (and consequent complexity) of the .NET Framework?

Remember that it's hierarchical. .NET APIs are divided into namespaces. Namespaces contain a set of related classes. Classes contain methods (and the methods are usually overloaded—permitting you to perform different, but related, jobs based on what parameters you pass).

Let's try to solve a common problem, to see some tactics you can use to locate the solution.

Assume that you have to parse a string. You've got a comma-delimited string from a use like "Barry Morgan, 12 Dalton Ln., Akron, OH, 22022" and you want to subdivide it into its substring parts. You want to create a string array holding each part.

Start by running VB.NET Help, then click the Search tab. Search for parse string. You get 500 hits, including a Dr. GUI article that tells you about the Parse method. Unfortunately, it doesn't parse, it converts a string into other data types. Somebody incorrectly thinks that "parse" means convert. When computer languages are written, the specification committees don't include any English majors, so we get too many poorly named functions like this one, and too many nearly unreadable "help" narrative descriptions.

Trying to narrow your search using quotes to look for "parse a string" fails. Let's try the Index feature.

Click the Index tab in the Help screen and type System.Text (no luck here, unless you want to enter the complicated netherworld of Regex, which requires even more elaborate code than using InStr to loop through your string).

Team Fly 


Visual Basic  .NET Power Tools
Visual Basic .NET Power Tools
ISBN: 0782142427
EAN: 2147483647
Year: 2003
Pages: 178

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