From the course: Visual Basic Essential Training

Understand default code flow - Visual Basic Tutorial

From the course: Visual Basic Essential Training

Start my 1-month free trial

Understand default code flow

- [Instructor] Looking at code in a VB apps Sub Main method it's easy to follow the instructions from the top line of code to the bottom line of code. That's not how code is written in the real world, and it hasn't been that way for decades. The software industry learned better ways to organize code. We'll take a quick tour of the kind of program flow you can see in a Visual Basic application. In procedural programming, think old school programming concepts from the early days of coding, programs were written in a linear fashion, you start at Step A, proceed to Step B, and so on to the last step. All programming languages have ways to reorder and repeat sections of code. For example, a conditional branch provides two or more pass through the program. Sometimes when this example code runs, it is Step A, then Step B, and then Step D. When the conditions are different it might be Step A, C, and D. We look at branch statements later in the course. To repeat steps use a loop statement. In this example step B and step C are repeated until a condition is met, then step D processed. We'll see how to modify a console apps Main method to loop until the user presses a specific key stroke. We'll look at that in this chapter. We'll look at loop statements in more detail later in the course. All programming languages provide tools for isolating sections of code into named areas. Traditionally these are known as functions. Moving code in to functions is a time-tested way to build reusable bits. Refactoring like this is essential for keeping the code base readable and maintainable. VB has functions of course, it's also got a special function type called a sub. The difference is that functions return a value to the calling code, subs don't. This example shows two functions, LookupDnaSequence and CalcSequenceLength. In the Main method, step A calls the LookupDnaSequence. That function does its work, going through steps G1 and G2. Then the code returns to the Main sub. In the Main method step B calls the CalcSequenceLength function. That function does its work, going through steps H1, H2 and H3. Then the code returns back to the Main sub. Because this code is reusable it can be called again as needed. Here step D is calling the LookupDnaSequence function. That function repeats the same steps a second time. To make it more flexible we could pass input parameters to the function. Details about those are discussed later. VB is based on .NET, and .NET is an object-oriented system. That means you need to get familiar with classes and objects. Classes are the primary containers for functions and subs. Most code that you write and use in Visual Basic is encapsulated in the classes, the bedrock of OOP design. The .NET framework is a massive library of classes. You instantiate those classes into objects and use them in your applications. In this example, the code runs in Main. The code creates an object. Another way to say that is that the code creates an instance of the class. Whatever way you say it, our code now has access to the code and data that is inside the class instance. Now we can call the method in the class instance. A note about names, in .NET lingo a method is the common name for functions and subs that are contained inside a class. Now the code flow moves to the function in the class, and the code runs there. When the code is finished the call returns back to the Main method. If the function returns data we can use that in Main. As you suspect we can call the other methods in the class instance. The key point to consider when understanding program flow is that inside the Main method the code is still flowing from top to bottom, even though it's using code inside class instances. So far everything we've seen for program flow is straightforward. Concepts like these have been around for decades. In addition to what we've seen, you should know that .NET supports multiple threads. That means your Visual Basic application code could have multiple active code pass running at the same time. This is important for building responsive, scalable applications. To oversimplify, you can think of most VB applications as being single threaded. Your code is executing on the default thread. If necessary you can queue up work on another thread. Then your task can run in parallel, with each thread finishing its work independent from the other threads. We won't examine multiple threads in this course, but I feel that it's important for you to know that they are available. Most introductory language courses focus on language syntax and simple skills. As you gain experience in VB and start using modern APIs it's important to consider using asynchronous programming with threads. Thankfully Microsoft has spent years simplifying the code experience for threads. Many of those new experiences are incorporated in the asynch features in .NET and Visual Basic

Contents