21
Visual Basic Chapter 1 Lesson 3

Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Embed Size (px)

Citation preview

Page 1: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Visual Basic

Chapter 1 Lesson 3

Page 2: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

The Visual Basic language

• Visual Basic was introduced by Microsoft in 1991

• Visual Basic is often shorted to be named VB

• It was created to allow programmers to easily create applications that include a graphical user interface (GUI)

– A GUI allows user to interact with programs using a mouse or touch to point, drag, or click

Page 3: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Types of Visual Basic Programs

• Types of Visual Basic programs are– Web application – program that runs on the

World Wide Web and is available to end users on any platform

– Windows application – program such as Microsoft Word or Excel that runs on a Windows system

– Console application – a program without a GUI, that executes in a console window and produces text-based output• We will create console applications in this class

Page 4: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Microsoft’s .NET Framework

• We create our VB console applications using Microsoft’s .NET Framework

• The .NET Framework is a technology that supports building and running the next generation of applications and XML Web services.

• One way to think of a programming framework is like a framework of a house. It is much easier to build a house if it is already framed (i.e. the house has the structures and support). Similarly, we build our programs upon a framework that already exists.

• If interested, you can read more about the .NET framework at the following link:– https://msdn.microsoft.com/en-

us/library/zw4w595w(v=vs.110).aspx

Page 5: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Procedural Programming

• Procedural programs consist of statements that the computer runs or executes

• Many of those statements make calls (a request to run or execute) to groups of other statements known as procedures, modules, methods, or functions.

• The key is that these statements are known as “procedural” because they perform a sequence of procedures in order.

• An example of a procedural program is one that performs a specific task such as adding up exam scores. It is a task in which steps are performed in order.

Page 6: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Object-Oriented Programming

• Today’s computer programs need to be flexible and easy to revise

• Object-oriented languages, including Visual Basic, were introduced to meet this need.

• Object-oriented programming is often referred to as OOP.

• In 0bject-oriented programming, the programmer can focus on the data that he or she wants to manipulate, rather than on individual lines of code required to manipulate that data.– The individual lines of code will still eventually need to be

written, but the solution is organized differently. This means that even OOP still uses some procedural programming. The two are not strictly mutually exclusive.

Page 7: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Object-Oriented Terminology

• An object represents something in the real world such as a car, patient, or employee

• An object encapsulates (holds inside of it) the data related to the object as well as the tasks that the object can perform – The tasks that the object can perform are sometimes

referred to as behavior or methods• If the object is a car, a behavior may be to brake or accelerate

• If the object is an inventory, a behavior may be to total the number of items in stock

– The object’s data items are called attributes or properties

Page 8: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

OOP - Putting it Together

• So what’s the advantage of OOP?– Because you interact with objects and because each object

has methods (behavior), you can call an object’s method and that method will then do something for you. It allows you to write shorter code because you are reusing code that is already written. Sometimes you write the methods, and sometimes you use methods that are already written by another programmer, such as those that come with .NET. Either way, once it is written, it can be reused multiple times.

– As we’ll discuss at the end of the semester, OOP also allow you to abstract away details. You can call an object’s method to accomplish a task without having to know the details about how the method accomplishes the task.• Going back to our car as an object example, you can accelerate a

car without having to be concerned about the internals of a car. You just tell the car to accelerate by pushing the gas.

Page 9: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Classes

• How are objects created?– Consider that a car is built based on a blueprint. A

Ford Taurus is built based on a blueprint for this vehicle.

– Similarly, programming objects are built based on a blueprint that describes objects of the same type. The blueprint is called a class.

– The programmer specifies the data (attributes/properties) and the behaviors (methods) for all objects that belong to that class. • Once a class is created, an object, also called an instance of

a class, can be created through a process called instantiation.

Page 10: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

OOP – An Analogy

• The text describes the following analogy to help explain OOP:Baking a chocolate cake – The recipe is similar to a class, and the actual cake is the object. If you wanted to, you could create many chocolate cakes all based on the same recipe. For example, you could bake your mother’s birthday cake, your sister’s anniversary cake, etc. They are all based on the same recipe.

• Similarly, once a class is created, many objects of that type can be created.

Page 11: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

NOTE: Change from the Textbook

• Our second book for the course, "Microsoft Visual Basic Programs to Accompany Programming Logic and Design" by Jo Ann Smith takes us through how to create Visual Basic programs.

• In Chapter 1, there are a few sections that describe using Notepad to create a Visual Basic file and then compile it. Please disregard sections titled "Writing Visual Basic Source Code" and "Compiling a Visual Basic Program" in Chapter 1. We will not be using Notepad to create our programs.

Page 12: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Software for Our Course

• We will use Visual Studio 2012 (Premium or Professional edition) to create, build (compile) and run (execute) our programs. In the Week 1 Materials folder you will find a video for how to create, build and run a program in Visual Studio. Please use this video along with these slides to guide you navigating Visual Studio to create your own Visual Basic.NET projects.

Page 13: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Installing the Software

• To begin writing programs using Visual Studio, you will first need to install Visual Studio 2012 if using the software from home.

• The software needs to be either the Premium or the Professional edition.

• The software can be obtained free of charge from the school’s DreamSpark site or by borrowing a copy from C201 (“the cage”). If you choose to download from the school’s DreamSpark (also known as “ELMS”) site, then you can find general directions and the link to the DreamSpark site under “Start Here->How To->Steps to Get Software Online”. Once you enter the site, use the search tool to search for Visual Studio Premium 2012. If you are obtaining the copy from the cage (C201), they will likely give you the Professional edition. For this class, either edition will work well.

Page 14: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Getting Started

• Once you have installed the software, you will want to try it out!

• To open Visual Studio, click the Start button in the bottom left corner of your screen or

• Select All Programs->Microsoft Visual Studio 2012

Page 15: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Getting Started (Cont’d)

• Once the software opens, select File->New Project and the following screen will appear

Page 16: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Getting Started (Cont’d)

• Select Console Application in the center of your screen and ensure that the language (on the left) is selected as “Visual Basic”

Page 17: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Getting Started (Cont’d)

• Name your project with a meaningful name. For our purposes, we could name it HelloWorld.

• Choose the Location on your computer where you would like to save the project. You can click the Browse button to change the location.

• Note that Visual Studio will create an entire project folder with multiple files inside of the folder.

• If you do not see the Location field as shown above, please visit the Start Here->How To folder in Angel and read the resource titled “How to Save Project Location When Creating a New Project”

Page 18: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Getting Started (Cont’d)

• Our project will be very simple. We will add just one statement to display the message of “Hello World” for the user.

• Inside of the Sub Main() type the following:

• Console.WriteLine("Hello World") 'Print to the screen Hello World

• Visual Studio will automatically add the colors. Every character is important, don’t forget any of them!!

Page 19: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Getting Started (Cont’d)

• The program is now created. Let’s go ahead and Build the project.

• Building in Visual Studio is another way to describe compiling your project.

• Select from the toolbar Build->Build Solution

Page 20: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Getting Started (Cont’d)

• The final step to run your project is to select Debug->Start without Debugging or use <CTRL> F5.

• It is important to note that by starting your project in Visual Studio, you are actually runningor executing your program.

• If at this point you cannot find the menu option to Start without Debugging, please look in Angel under Start Here->How To

for directions to add this

menu option.

Page 21: Visual Basic - mrjcfhs.commrjcfhs.com/cp1/ch1/lesson_3.pdf · •Our second book for the course, "Microsoft Visual Basic Programs to Accompany ... create Visual Basic programs. •

Getting Started (Cont’d)

• You should now see a screen like this one. If you are working on the systems at Stark State you may get a lot of path information before the text: Hello World.

• The screen that you see is a type of program called a console application. This is your running program. You can press any key to close the window.