Lest you forget it, let me remind you again: Computers are not really very smart. They only know how to do the simplest of tasks. If you want them to do anything remotely complex, you have to give precise, step-by-step instructions down to moving individual bits of dataonly 1s and 0s, rememberaround in memory. Fortunately, most of the code you would ever need at that low level has already been written for you, and incorporated into the Windows operating system and the .NET Framework. Microsoft- and third-party-supplied code libraries give you a lot of pre-written functionality that's available for use in your own programs. And that's good, because you would rather be hurtled into space on a giant bungee cord than have to write business applications at the machine code level all day long. Even though you have all of this great pre-written code in your arsenal, you still have to tell the computer precisely what you want it to do, in fine detail, or it won't do it. And that's where high-level languages like Visual Basic come in. They provide the grammar you need to communicate with the computer. For any given tasks that the computer needs to perform, your job as a programmer is to determine the individual steps to accomplish that taskthe logicand translate those steps into computer-ese using the programming language. As an example, let's say you receive a request from the sales department for a program that will reverse all of the letters in any chunk of text provided to the program. "Our customers are clamoring for this; we need it by Tuesday," they say. Okay, so first you figure out the logic, and then you implement it in Visual Basic. Using pseudo-code, an artificial programming language that you make up yourself to help you write programs, you can sketch out the basics of this task (with leading line numbers). 01 Obtain the original text (or string) from the user. 02 If the user didn't supply any content, then quit now. 03 Prepare a destination for the reversed string, empty for now. 04 Repeat the following until the original string is empty: 05 Copy the last character from the remaining original string. 06 Put that character onto the end of the destination string. 07 Shorten the original string, dropping the last character. 08 [End of repeat section] 09 Show the user the destination string. There are many ways that this logic could be written; this is just one example. This pseudo-code can now be converted into your language of choice; in this case, Visual Basic (don't worry about the syntax details for now). 01 originalText = InputBox("Enter text to reverse.") 02 If (Len(originalText) = 0) Then End 03 finalText = "" 04 Do While (originalText <> "") 05 oneCharacter = Right(originalText, 1) 06 finalText = finalText & oneCharacter 07 originalText = Left(originalText, _ Len(originalText) - 1) 08 Loop 09 MsgBox("The reverse is: " & finalText) This source code is now ready to be used in a Visual Basic program. And it also demonstrates several essential aspects of coding.
This sample code listed previously could be made a little more efficient. In fact, it's entirely possible that Microsoft obtained an early draft of this book, because they included a string-reversal feature right in Visual Basic, and called it StrReverse. originalText = InputBox("Enter text to reverse.") If (Len(originalText) = 0) Then End finalText = StrReverse(originalText) MsgBox("The reverse is: " & finalText) That's right, Visual Basic already includes a string reverse feature, some of that pre-written library code I keep taking about. Visual Basic includes many such intrinsic functions that are considered part of the language, and that bundle up useful pre-written functionality. Many of these functions appear in the Microsoft.VisualBasic namespace, which is automatically made available to your Visual Basic source code when you create a new VB project. |