0% found this document useful (0 votes)
19 views19 pages

Module 4

The document outlines the structure of a VB.NET program, including key components such as namespace declarations, classes or modules, procedures, and the Main procedure. It provides a sample code that demonstrates how to print a message to the console and explains the purpose of various elements within the code, such as modules and error types. Additionally, it discusses programming errors, their types, and best practices for minimizing errors during development.

Uploaded by

georgecrust93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views19 pages

Module 4

The document outlines the structure of a VB.NET program, including key components such as namespace declarations, classes or modules, procedures, and the Main procedure. It provides a sample code that demonstrates how to print a message to the console and explains the purpose of various elements within the code, such as modules and error types. Additionally, it discusses programming errors, their types, and best practices for minimizing errors during development.

Uploaded by

georgecrust93
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 19

VB.

NET - PROGRAM STRUCTURE


• A VB.NET program basically consists of the following parts:
• Namespace declaration
• A class or module
• One or more procedures
• Variables
• The Main procedure
• Statements & Expressions
• Comments
VB.NET Sample Program
• Look at a simple code that would print the words “VB.NET CLASS!":

Imports System ‘ default namespace for 2012 VB.NET


Module Module1
'This program will display VB.NET CLASS
Sub Main()
Console.WriteLine(“VB.NET CLASS!")
Console.ReadKey()
End Sub
End Module

When the above code is compiled and executed, it produces following result:
VB.NET CLASS!
VB.NET Sample cont.

• The first line of the program Imports System is used to include the
System namespace in the program.
• The next line has a Module declaration, the module Module1.
VB.Net is completely object oriented, so every program must contain
a module of a class that contains the data and procedures that your
program uses.
• Classes or Modules generally would contain more than one
procedure. Procedures contain the executable code, or in other
words, they define the behavior of the class. A procedure could be
any of the following: Function, Sub, Operator, Get, Set etc
VB.NET Sample cont.
• The next line( 'This program) will be ignored by the compiler and it
has been put to add additional comments in the program.
• The next line defines the Main procedure, which is the entry point for
all VB.Net programs. The Main procedure states what the module or
class will do when executed.
• The Main procedure specifies its behavior with the statement
• Console.WriteLine(“VB.NET CLASS")
• WriteLine is a method of the Console class defined in the System
namespace. This statement causes the message
• “VB.NET CLASS!" to be displayed on the screen.
Visual Basic Standard Modules
• The last line Console.ReadKey() is for the VS.NET Users. This will prevent
the screen from running and closing quickly when the program is
launched from Visual Studio .NET.
• A module is a global or public object where you can place program code
such as variables, functions & procedures that can be seen by all code in
the entire project.
• The key here is Global code!
• Suppose you wanted to write code that can manipulate or operate on
various forms & controls. How can you do this? Since every code you
write for a control or form is only visible within the form? Where would
you place the code? The answer is the Module!
Visual Basic Standard Modules cont.
• The Module has the following characteristics:
• Modules contain a method named Sub Main() that can be used as the starting
point of the application. Note this applies to both Console Applications and
Windows Applications.
• Modules contain variable declarations, functions & procedures or source code
ONLY! No forms!
• Modules are a good place to put program code that may be common to several
forms or other modules.
• Modules contain variables & Methods.
• Modules are like forms but without the visual!
• When you create a Console Application, Visual Basic automatically
creates a Module where you can begin entering your code:
Code Editor for Console
Application
• For a Console Application, the code editor in invoked immediately to
allow you to enter code in the Module.
• Simply begin entering code in the Module Document.
• The Code Editor screen contains two drop-down list boxes, one for
the Object you are coding and the other for the Methods & Event-
Procedures associated with the object:
Code Editor for Console
Application cont.
How Visual Basic Organizes Your
Program or Application Files
• When you create a VB program a Folder is automatically
created in you computer which contains the files of the
program.
• A Visual Basics application is called a Solution. The Solution
is composed of one or more Project.
• The table below lists several important files and their file
extensions:
How Visual Basic Organizes Your Program or
Application Files cont.
File Explanation Example
Extension
.sln Solution – File that holds information VideoManagement.sln
about all the projects in the application

.vb Object File – Contain definition and code frmLoginScreen.vb


for Forms, Modules, Class Modules etc. MainModule.vb

Resource File – This is a resource file for


.resx the Forms in the project. It contains frmLoginScreen.resx
information about all resources used by
the form.

Project File – This file describes the


project and lists the files included in
.vbproj the project. VideoManagement.vbproj
• Visual Basic has three distinct modes:
• Design Time – When you design the User Interface (GUI) and writing the
code.
• Run Time – When you execute and test the program
• Break Time – When you have Run time errors and you stop execution
Programming Errors
• There are three varieties of programming errors:
• Syntax Errors – Errors generated due to the code not following the rules of
the language
• These errors usually involve improper punctuation, spelling or format
• These problems are easier to solve since the compiler identifies the erroneous
statement and you just have to review
• the rules of the language to make sure you are writing the statement properly.
• Occurs when you mistype a command or leave out an expected phrase or argument .
VB detecys these errors as they occur and even provides help in correcting them.
• Syntax Errors: when you break VB rules from punctuation, format or spelling you
generate a syntax error.
• You cannot run a VB program until all syntax errors have been corrected
Programming Errors cont.
• Run Time Errors – Program halts during execution. The program passed
the syntax test but fails during execution. .
• These problems are usually caused by improper arithmetic execution,
attempting to access recourses that are not available etc
• These problems are difficult to solve since they only show up when the program
runs.
• Run Time Errors: if your projects halts during execution it is called Runtime Error.
VB displays a dialog box and highlights the statement causing the problem.
• Statements that cannot execute correctly cause runtime errors. It can be caused
by attempting to do impossible arithmetic operations e.g find the square root of
a negative number.
• They are usually beyond your program’s control.
• Example include when a variable takes on an unexpected value(divide
by zero) when a drive door is left open or when a file is not found. VB
allows you to trap such errors and make attempts to correct them.
Programming Errors cont.
• Logical Error – The program is not doing what is supposed to do. .
• The algorithm fails to solve the problem the program was written to resolve
• These problems are even more difficult to solve since you need to re-think
and go back to the planning phase and review the Algorithm.
• Logic errors are the most difficult to find. With logic errors, the program will
usually run but will produce incorrect or unexpected results. The VB debugger
is an aid in detecting logic errors.
• Logic Errors: when your program contain logic errors, your project runs but
produces incorrect results, perhaps the results of a calculation are incorrect
or the wrong text appears or the text is Ok but appears in the wrong location.
Some ways to minimize errors
• Design your application carefully, more design time means less
debugging time.
• Use comments where applicable to help you remember what you
were trying to do.
• Use consistent and meaningful naming conventions for your variables,
object and procedure
• Note: VB.Net is not case sensitive.
Build or Compile the Program
• The steps to build are as follows:
• 1) In the Menu Bar select Build|Build Solution, this will invoke the Output
Window
• 2) The Output Window displays the results of the Compiler process. It shows
if the results of the compiler were successful or failed. For a fully compiled
program, all compiler errors must equal to 0
Execute or Run the Program
• To run or execute the program do the following:
• Console Application where Output Window stays displayed:
• The three methods to executes are:
• 1) In the Menu Bar Debug|Start Without Debugging.
• 2) Or using the keyboard use the Crtl-F5.
• 3) Or navigate to the project folder\bin directory and double-click on the
executable file
Practical 1
Write program in console application to calculate the volume of
cylinder using . Also, in window application.

You might also like