1 / 24

VB Code Basics

VB Code Basics. Visual Basic Basics.

hua
Download Presentation

VB Code Basics

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. VB Code Basics

  2. Visual Basic Basics • Visual Basic code is generally easy to write and follow. You won't need to type many semicolons or squiggly braces ({}) like you do in C++. When you have blocks of code nested inside other blocks of code, the code editor will automatically offset the inner blocks for you. Here are some basic facts about VB coding: • Lines end when they end. When you hit the Enter key to start a new line, the last line is automatically finished. • However, you can extend a line to display on two lines by adding a space and an underscore to the end (" _"). • Note: We will look at the details of VB math later, but if you know how to do formulas in Excel, there won’t be many surprises. Space and underscore

  3. Blank lines are ignored by the compiler. Many books recommend that you add lots of them. I don't. • Comments begin with a single quote mark and end at the end of the line. • Within a Sub or Function, code is executed from top to bottom.

  4. Outside of Subs and Functions, the order of code doesn't matter: You can put your Subs and Functions in any order, and declare variables at the top, in the middle, or at the bottom of the code window. • The two code examples below are identical to the VB compiler:

  5. Variable Scope • Variables declared outside of Subs and Functions are visible (have scope) to all Subs and Functions in the Class. They retain their values when procedures* end. • Variables declared inside a Sub or Function are visible only within that Sub or Function. Their values disappear when the procedure* ends. • * The word Procedure refers to both subroutines and functions.

  6. Two Equals in One • VB uses the equals sign for both assignment (putting a value in a variable), and for comparison (checking to see if two values are equal).

  7. Data Types • Variables in Visual Basic have data types. • This means that you must assign a data type to any variables that you use in your programs. • Functions, Properties, and the parameters of Functions and Subroutines also have data types. • Data types come in two varieties: Value and Reference.

  8. Value Types • The Value data types are the core types of the language. They occupy a fixed number of bytes in memory. • VB has 15 value data types. You can see a complete list here. • You should be aware that all of these types exist; however, you will only need to use these six in this class: • Boolean (True or False) • Integer • Decimal (for monetary values) • Double (for floating-point values like measurements and averages) • String (for text) • Date

  9. Reference Types • In addition to the 15 or so basic value types, there are hundreds of other data types that can be used in VB programs. • Many of these are classes built in to the .NET framework—a huge collection of ready-made classes that encapsulate powerful code. Using these classes makes writing powerful VB programs much easier. • The controls in the VB Toolbox are the most visible .NET classes you will use, but there are many more. • Any classes that you create (which includes every form that you design) are also reference types. You can declare variables of these types as well.

  10. Variable Declarations • To use a variable, you must declare it first. • Inside a subroutine or function, variables are declared using “Dim” and “As”: • Dim NumberOfApples As Integer • Outside of subs and functions, variables are declared using a “scope specifier” along with “As”: • Public StudentName As String • Private Weight As Double • Public lblAnswer As Label • In addition to Public and Private, Friend and Protected can be used as scope specifiers. • More details on this later—this is intended as just enough introduction for you to do assignment 2.

  11. Variable Names • The names of things in VB, including variables, subroutines, functions, classes, properties, and more, are referred to as “identifiers.” • The rules for all identifiers are the same: • Must begin with a letter or an underscore • Can only contain letters, underscores, or numbers • Cannot contain punctuation or spaces • Cannot be the same as a VB keyword • VB is very helpful in letting you know when you have violated these rules.

  12. Controls • The "visual" part of Visual Basic refers to how it allows you to design a user interface by placing controls onto forms. • A form is also called a "window." All Windows programs, whether written in VB or not, consist of one or more windows (forms). • Usually there is a main window which opens when you start the program; closing it usually shuts down the program as well. • Programs can have many forms; we'll see how to do this later. • For this assignment, we will use only one form.

  13. Getting Started • When you create a new VB Windows Application project, VB will give you a form, "Form1", to start with. • You can start adding controls, such as buttons, labels, and textboxes, or you can start by modifying the form itself--make it bigger or smaller, change its name, change its caption (Text), and so on. • In fact, the form itself is technically a control, and shares many of the characteristics of the standard controls.

  14. Control Properties and Events • The most important characteristics of controls are properties and events. • Properties describe the appearance and other aspects of a control that you can change, either in the designer window or by writing code. • Events are the various things that can happen to your control when the program is run--it can be clicked, double-clicked, have the mouse go over it, have the mouse leave it, have a keyboard key pressed on it, and many more. • Manipulating properties and responding to events are how you create a working user interface for your program.

  15. Control Properties • Even the simplest VB controls have dozens of properties that you can modify. The picture at the right shows the Integrated Development Environment (IDE), with a label placed on a form. The right side of the IDE is the properties window • Note the tool buttons near the top of the properties window. The one on the left displays the properties in categories; the next one to the right (with the AZ on it) displays them in alphabetical order. I generally prefer using alphabetical order, because I can generally recall the name of a property, but I'm not always sure how VB might categorize it. • Every control has a name. When you first place a control on a form, VB will assign it a default name, such as Label1, Textbox1, and so on. • This is generally done using a lowercase three-letter prefix describing the type of control: "lbl" for label, "txt" for textbox, "btn" for button, and so on. • The prefix is then followed by a descriptive name that will help you identify the control's purpose when you write your code. For example, if a label is going to display the result of a calculation, you might name it "lblResult".

  16. Control Properties • Besides name, there are several properties common to most controls. • If the control displays text (labels, textboxes, checkboxes, radio buttons, and more), it will have a Font property (actually several properties combined) and a ForeColor property, which determines the color of the text displayed. • Controls also have Location and Size properties, although you will generally set these properties visually using the mouse. • We'll learn about many of the other properties as we study individual controls.

  17. Events • VB is an event-driven language. That means that pretty much any code that you write, whether to do a calculation, open up a database table, or fire your ray gun, will begin with an event. • There are two ways to see all of the events that a control can respond to. The first is by clicking the lightning-bolt icon near the top of the properties window. The list of properties will be replaced by a list of events. Double-clicking on the name of an event will take you to the code window, where you can write code to respond to that particular event.

  18. Events • The second way to see a control's events is in the code window. Select the name of the control in the drop-down list at the upper left of the code window. The other drop-down list, in the upper right, will now display all of that control's events. Again, selecting one of these events will get you started writing the code to handle that event.

  19. Events—Caution! • A big word of warning (one of many such warnings you'll get this semester). • Just because VB ALLOWS you to do so many things doesn't mean that you SHOULD do them all. • Most controls have one or two events that are commonly handled in programs and web sites, and users are used to these. • User-friendly applications generally DO NOT respond to most of the events available from a control. • In general you should stick to using a control's common events and leave the rest alone.

  20. VB Math • All of your favorite mathematical operators are available in VB: • + Addition • - Subtraction • * Multiplication • / Division • \ Integer Division • Mod Modulo (remainder after integer division) • ^ Exponentiation

  21. Other Math Functions • Trig functions, logarithms, and other advanced math functions are available in the Math class (built in to VB). • To use these, just type the word “Math” followed by a period into your code; all of your options will appear in the intellisense drop-down list.

  22. String Functions • VB strings are powerful and easy to use. • A string variable can hold extremely long strings (millions of characters). • The basic string operator is the ampersand (&). It concatenates (puts together) two strings: Dim s As String = “Go “ Dim t As String = “Blue!” Dim u As String = s & t u will have the value “Go Blue!”

  23. Shortcut assignment operators • VB.NET supports shortcut operators (+=, -=, *=, /=, &=, etc.): • A += B is the same as A = A + B • X *= 4 is the same as X = X * 4 • S &= “!” is the same as S = S & “!”

  24. Using linefeeds and tabs in strings • You can insert carriage-return linefeed characters into your strings using “vbCrLf”. • You can insert tab characters using “vbTab”.

More Related