Revised on May 24, 2021
Course Overview
0.1 Course Details
i. Course title: Visual Programming
ii. Course code: TCC123/05
iii. Credit hours: 5
iv. Learning hours: 200 hours
v. Course type: core
vi. Offer in academic year:
vii. Course coordinator/Lead: Respective appointed lecturer from School of Science and Technol-
ogy (SST)
viii. Tutor(s): Respective appointed tutor(s) from SST
1
No. Activities - ODL / ODLi No. of Hours
1 Guided learning (F2F), e.g., 42
participate online guided learning: discussion and presentation,
participate scheduled online lecture / tutorials,
and participate online practical: hands-on and lab activities
2 Independent learning (Non-F2F), e.g., 108
study learning materials - online materials, video clips, podcast,
attempt learning activities - online forum discussion, presentation, case studies
attempt supplementary assessment: self-tests, quizzes, exercises, practical activities,
and perform online library search for supplementary learning materials
3 Continuous Assessments, e.g., 50
online assignment,
online hands-on and laboratory activity (if any),
online laboratory test (if any),
online quiz (if any),
online mid-semester test (if any),
and final examination (if any or alternative assessment)
Total 200
Table 1: Allocation of Student Learning Time (SLT)
0.2 Course Synopsis
’Coding’ or ’Programming’ for you probably mean ’software engineering’ - i.e. the science of building
software applications. Persons new to the field often confuse knowledge of a specific programming
language (e.g. Java, C++, PHP, Python) with the skill of software engineering. A programming lan-
guage is a simply a means of achieving the goal of building an application. This course can help you
design and develop these systems via the .Net technologies.
This course aims to introduce students to the basic concepts of Visual Programming. In this course the
students will be involved in the development of graphical user interfaces (GUI), designing complete
windows applications, creating and using custom controls, developing web database applications,
learning some of the new and more advanced programming concepts introduced in Visual Basic (VB)
and the .NET framework. The skills that you gain from this course can be applied at daily life and your
tomorrow workplace to solve related programming problems or for setting up your own software-based
business.
2
0.3 Course Learning Outcomes
You should be able to:
i. Extends theoretical and principles of object-oriented Programming with elements of the Visual
Basic .Net programming for software design and development. (C1, PLO1)
ii. Demonstrate ideas into Graphical User Interface (GUI) for both Windows-based and Web-based
applications with database under guided supervision. (P3, PLO3)
iii. Explain data structures with object-oriented programming approaches for the software con-
struction and implementation. (C2, PLO2)
0.4 Study Schedule
Topic Week ODL and ODLi Guided learning Independent learning
1 1 Topic and Focus F2F Non-F2F Non-F2F
2 1 Overview of Programming Languages
3 2 Review of Object-Oriented Principles
4 3 Introduction to Visual Basic (VB)
5 4 Elements of VB
6 5 Manipulation with VB
7 6 Windows-based Application Development
8 7 Advanced Windows-based Application Development
9 8 Web-based Application Development
10 9 Advanced Web-based Application Development
11 9 Object-Oriented Programming (OOP)
12 10 Advanced VB Programming with OOP
13 11 Exception Handing with VB Programming
Multithreading with VB Programming
0.5 Assessment Methods
Assessment - ODL Weightage
Online assignment 1 (A1) 100%
Online assignment 2 (A2) 100%
Final examination (if any or alternative assessment)
Total
Assessment - ODLi Weightage
Online assignment 100%
Online hands-on (if any) 100%
Online laboratory test (if any)
Online quizzes (if any)
Online mid-semester test
Final examination (if any or alternative assessment)
Total
3
0.6 References
i. David I. Schneider. (2019). Introduction to Programming Using Visual Basic (11th Edition).
Pearson.
ii. Philip Conrod. (2019). Visual Basic and Databases 2019 Edition: A Step-By-Step Database
Programming Tutorial. Kidware Software LLC.
iii. Mike McGrath. (2019). Visual Basic in easy steps, 6th edition: Updated for Visual Basic 2019.
In Easy Steps Limited.
iv. Philip Conrod. (2019). Learn Visual Basic: 2019 Edition. Kidware Software.
v. Corinne Hoisington. (2017). Microsoft Visual Basic 2017 for Windows, Web, and Database
Applications: Comprehensive (Shelly Cashman). Cengage Learning
vi. David I. Schneider. (2017) An introduction to programming using Visual Basic. Tenth edition.
Boston : Pearson.
vii. Mark J. Collins. (2015) Pro HTML5 with Visual Studio 2015, SpringerLink.
viii. P. Desjardins, Visual Studio Condensed. Apress, 2014.
ix. B. Johnson, ”Professional visual studio 2012.” Indianapolis, Indiana: John Wiley & Sons, 2013.
x. Deitel, H M and Deitel, P J (2009) Visual Basic 2008 How to Program, Pearson Prentice Hall.
xi. Zak, D, Microsoft Visual Basic 2008 Reloaded, 3rd ed, Cengage Learning, 2009.
xii. Shelly, G B and Hoisington, C, Microsoft Visula Basic 2008 for Windows, Mobile, Web, Ofiice
and Database Applications, Cengage Learning, 2009.
0.7 Copyright
Copyright protection in Malaysia is governed by the Malaysia Copyright Act of 1987. Under the Act,
the copyright in a work is infringed when a person who is not the owner of the copyright, and without
consent of the owner of the copyright, makes a reproduction of the whole work or a substantial part
there of either in its original or derivative form for distribution to the public. Under certain conditions
specified in the law, libraries and archives are authorised to furnish a photocopy or other reproduction.
One of these specified conditions is that the photocopy or reproduction is not to be used for any
purpose other than for private study, scholarship or research. The shared materials here work around
this fair use or fair dealing principle. Please take note.
4
0.8 Welcome notes
Welcome to TCC123/05 Visual Programming. This course contains five study units. Our course devel-
opment team developed this course on the basis that you already have prior knowledge or experience
in programming obtained from TCC 121/05 Programming Fundamentals with Java (the advisory pre-
requisite course). The materials in TCC 123/05 are presented in a simple step-by-step format, with
many examples and programming exercises.
This course will equip you with Microsoft Visual Basic (VB) concepts and programming skills. It will
guide you in developing small Windows and Web application programs. It starts with a review of the
principles of object-oriented programming and illustrates VB events using examples. With a basic
knowledge of VB elements, you can immediately write simple programs before moving on, in the
later units, to develop Windows and Web application programs that have a graphical user interface
(GUI).
You will be directed to read specific parts of additional reading materials (available in the WOU Library)
as you work your way through the units. Also, websites and online videos provide another way of
explaining the information covered in this course, so you can always get a second description of the
concepts explained in the learning materials.
0.8.1 What is visual programming?
A visual programming tool, e.g. VB via the Microsoft Visual Studio, allows programmers to develop
applications using diagrams or forms on-screen with pointing devices. The benefits of visual program-
ming include:
i. Allowing users to visualise the complexity of the program.
ii. Increasing the productivity such as rapid prototyping. The prototype can later be refined to
produce final application.
iii. Speeding up development work because changes are made immediately and displayed visually.
Visual programming is used to develop many business application areas, which are related to our real
life environment. For example, it can be used to develop Online Banking Systems, Online Bill Payment
Systems and Online Submission of Income Tax.
From user’s point of view, they do not have to be physically present to perform their transactions
in the office. All the transactions can be done anytime, anywhere, as long as an Internet service
is available. Applications systems written by visual programming were enhanced with security and
encryption to protect sensitive data. Users will find it very convenient using the online system. From
developer’s perspective, visual programming will speed up the development time: The end user gets
to visualise the interface before the application gets into production, and the interface designer can
participate in development process alongside with the programmer. It helps to develop a team work
environment.
5
Selected examples of visual programming, which serve specific domain area and industry with unique
purposes, are listed as follows.
i. Simulink 1: it allows developer to design and simulate system before moving to related hard-
ware. It also allows developer explore and implement designs, which developer wouldn’t other-
wise consider – without having to write C, C++, or HDL code.
ii. LabVIEW 2: it is systems engineering software for applications that require test, measurement,
and control with rapid access to hardware and data insights. It offers a graphical programming
approach that helps developer visualize every aspect of application, including hardware con-
figuration, measurement data, and debugging. Their visualization makes it simple to integrate
measurement hardware from any vendor, represent complex logic on the diagram, develop data
analysis algorithms, and design custom engineering user interfaces.
iii. Max 3: it is a visual programming language, which is used to create interactive media software
with music and multimedia. With in-depth tools for audio, graphics, interaction, and communica-
tion, it has been used by composers, performers, software designers, researchers, and artists
to create recordings, performances, and installations.
iv. Pure Data 4: it is an open source visual programming that runs on personal computers to
embedded devices, i.e. Raspberry Pi and smartphones (required libpd & DroidParty - Android,
PdParty - iOS). it allows musicians, visual artists, performers, researchers, and developers to
create software graphically without writing lines of code.
v. Scicos 5: it is a graphical dynamical system modeler and simulator. With Scicos, developer
allows to create block diagrams to model and simulate the dynamics of hybrid dynamical sys-
tems, and compile models into executable code. It is used for signal processing, systems con-
trol, queuing systems, and to study physical and biological systems. It also allows generation of
component based modelling of electrical and hydraulic circuits using the Modelica 6 language.
vi. GNU Radio 7: it is a free and open-source software development toolkit that provides signal
processing blocks to implement software radios. It can be used with readily-available low-cost
external RF hardware to create software-defined radios, or without hardware in a simulation-like
environment. It is widely used to support both wireless communications research and real-world
radio systems.
vii. Ai-Board 8: it is a mobile application, which includes a unique board game with a built-in
Machine-Learning Engine and a proprietary visual-programming language.
1https://www.mathworks.com/products/simulink.html
2https://www.ni.com/en-my/shop/labview.html
3https://cycling74.com/products/max/
4https://puredata.info/
5http://www.scicos.org/
6https://www.modelica.org/
7https://www.gnuradio.org/about/
8https://aiboard.blog/, watch more at its YouTube at URL https://www.youtube.com/channel/
UCdQUbt_HS2C-1Z99aUO_T2g/
6
viii. MIT APP Inventor 9: it is an intuitive, visual programming environment that allows everyone
- even children – to build fully functional applications for smartphones and tablets. It offers
blocks-based coding programs, which claims to inspire intellectual and creative empowerment.
ix. Flowgorithm 10: it is based on simple graphical flowcharts. By using flowcharts, developer
can concentrate on programming concepts. Developer allows to run programs directly in Flow-
gorithm. It has the capability to convert the developed flowchart to over 18 languages, including
C#, C++, Java, JavaScript, Lua, Perl, Python, Ruby, Swift, Visual Basic .NET, and VBA (used in
MSOffice).
0.8.2 Questions, exercises and hands-on provided in this unit
Self-tests and activities are included in each study week, but you should regard these as the basic
ways to test your understanding. Please attempt all review questions to have a better understanding
of this course. You can find the programming solutions and example source codes in a cloud-based
source code repository, i.e. GitHub - Microsoft friendly supported version control solution at URL
https://github.com/choojun/TCC123-VB.
0.8.3 Before you begin
Download Microsoft Visual Studio (VS) Community edition for free at http://www.visualstudio.
com/en-US/products/visual-studio-community-vs. Note that Microsoft Visual Studio is used
for program development in this course. The VS Community edition is a full-featured Integrated De-
velopment Environment (IDE), and is also an easy-to-use and learn tool for hobbyists and student
developers who wish to develop Windows desktop, Windows Web, Windows mobile, Android and iOS
applications.
When you install this software, you need to also install the Help documentation and SQL Server
Express. Microsoft provides a dedicated section for help in using the Community edition at https:
//visualstudio.microsoft.com/support/. For those of you using VS editions other than the
VS Community edition, take note that your Start Page screen may look different from the screen in
this course module. All the programs and exercises presented in this course are tested with the VS
2019 and Windows 10.
9http://appinventor.mit.edu/
10http://www.flowgorithm.org/
7
Contents
0.1 Course Details . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
0.2 Course Synopsis . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
0.3 Course Learning Outcomes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
0.4 Study Schedule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
0.5 Assessment Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
0.6 References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
0.7 Copyright . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
0.8 Welcome notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
5
0.8.1 What is visual programming? . . . . . . . . . . . . . . . . . . . . . . . . . . 7
0.8.2 Questions, exercises and hands-on provided in this unit . . . . . . . . . . . . 7
0.8.3 Before you begin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1 Topic 1: Overview of Programming Languages 15
1.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.3 Categories of programming languages . . . . . . . . . . . . . . . . . . . . . . . . . 15
1.3.1 Low-level languages . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.3.2 High-level languages: Procedural languages and object-oriented languages . . 16
1.4 Advantages of high-level languages . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.5 Disadvantages of high-level languages . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.5.1 Example of high-level language/procedural language . . . . . . . . . . . . . . 18
1.6 Object-oriented programming (OOP) . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.6.1 Fourth-generation languages (4GLs) . . . . . . . . . . . . . . . . . . . . . . 19
1.6.2 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.6.3 Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
1.7 Procedural programming vs. object-oriented programming . . . . . . . . . . . . . . . 21
1.7.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
1.8 Event-driven programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
1.8.1 Procedural programming and event-driven programming . . . . . . . . . . . . 22
1.8.2 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
1.9 Event-handlers design pattern . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
1.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
1.10.1 Self-test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2 Topic 2: Review of Object-Oriented Principles 30
2.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.3 What is object? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.3.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.4 Object properties and behaviour . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.4.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
8
2.5 Methods . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.5.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
33
2.6 Encapsulation and information hiding . . . . . . . . . . . . . . . . . . . . . . . . . . 34
34
2.6.1 Saving Account class . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
36
2.6.2 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.7 Object associations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
2.8 Class and instance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
39
2.8.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
2.9 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
40
2.9.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.10 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.10.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.11 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2.11.1 Self-test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3 Topic 3: Introduction to Visual Basic (VB) 42
3.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.3 System requirements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.4 Visual Studio software support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.5 Installing Visual Studio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.6 .Net framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.6.1 .Net Common Language Runtime (CLR) . . . . . . . . . . . . . . . . . . . . 44
3.6.2 .Net and Object-Oriented Development Environment . . . . . . . . . . . . . . 45
3.6.3 Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46
3.7 Programming activity - Starting Visual Studio . . . . . . . . . . . . . . . . . . . . . . 46
3.7.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
3.7.2 Hands-on: Hello World program . . . . . . . . . . . . . . . . . . . . . . . . . 48
49
3.7.3 Hands-on: TextBox control in Windows Forms App with .Net framework . . . 50
3.7.4 Hands-on: Button control in Windows Forms App with .Net framework . . . .
3.7.5 Hands-on: Label and PictureBox controls in Windows Forms App with .Net 51
51
framework . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
53
3.7.6 Hands-on: ListBox control in Windows Forms App with .Net framework . . . 53
54
3.7.7 Hands-on: Open a project from the GitHub repository . . . . . . . . . . . . . 56
3.7.8 Hands-on: To close the current solution . . . . . . . . . . . . . . . . . . . . . 56
3.7.9 Hands-on: To open an existing solution . . . . . . . . . . . . . . . . . . . . . 57
3.7.10 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
3.8 Naming convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.9 Event . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58
3.9.1 To open the Code Editor window . . . . . . . . . . . . . . . . . . . . . . . .
3.9.2 Hands-on: Demonstrate event procedures . . . . . . . . . . . . . . . . . . .
3.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
3.10.1 Self-test . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
9
3.11 Practice . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.11.1 Exercise 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
3.11.2 Exercise 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62
3.11.3 Exercise 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
3.11.4 Exercise 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
3.11.5 Exercise 5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
3.11.6 Exercise 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
4 Topic 4: Elements of VB 69
4.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.3 Declaring a variable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69
4.4 Naming convention . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
4.4.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
4.5 Data type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 72
4.5.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
4.5.2 Hands-on: Displaying a single line of text with single statement . . . . . . . . . 74
4.5.3 Hands-on: Rename class . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
4.5.4 Hands-on: Displaying a single line of text with multiple statements . . . . . . . 76
4.5.5 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 77
78
4.6 Working with the int data type . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
80
4.6.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
4.7 Arithmetic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82
83
4.7.1 Rules of operator precedence . . . . . . . . . . . . . . . . . . . . . . . . . . 84
4.7.2 Program to demonstrate arithmetic operation . . . . . . . . . . . . . . . . . . 85
4.7.3 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
4.8 Using a message dialogue to display a message . . . . . . . . . . . . . . . . . . . . 87
4.8.1 Adding a Reference to an assembly . . . . . . . . . . . . . . . . . . . . . . . 88
4.9 Rounding value . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
4.9.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
91
4.10 Working with the string data type . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
93
4.10.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
94
4.11 Manipulating with string data . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.11.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.12 Working with the DateTime data type . . . . . . . . . . . . . . . . . . . . . . . . .
4.12.1 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.13 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
4.13.1 Self-test 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
5 Topic 5: Manipulation with VB 96
5.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
5.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
5.3 Decision Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
10
5.3.1 Hands-on: If structure - Even number . . . . . . . . . . . . . . . . . . . . . 97
5.3.2 Hands-on: If Else structure . . . . . . . . . . . . . . . . . . . . . . . . . . 98
5.3.3 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98
5.3.4 Hands-on: If ElseIf structure - Type of person . . . . . . . . . . . . . . . 100
5.3.5 Hands-on: If ElseIf structure - Marks and grade . . . . . . . . . . . . . . 100
5.3.6 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
5.3.7 Hands-on: Select structure - Even and odd numbers . . . . . . . . . . . . . 102
5.3.8 Hands-on: Select structure - with numerical value . . . . . . . . . . . . . . . 103
5.3.9 Hands-on: Select structure - with string and other values . . . . . . . . . . . 104
5.3.10 Hands-on: Select structure - multiple case statements with same result . . . 105
5.3.11 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
5.4 Loops Structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
5.4.1 while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 108
5.4.2 do-while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
5.4.3 do-until loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
5.4.4 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
5.4.5 for-next loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
5.4.6 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112
5.4.7 do-loop while loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
5.4.8 do-loop until loop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
5.4.9 exit statement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113
5.4.10 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 114
5.5 Logical operator . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116
5.6 Method . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
5.6.1 Optional Argument . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
5.6.2 Hands-on: Optional Argument - Log data . . . . . . . . . . . . . . . . . . . . 121
5.6.3 (Default) Pass by Value - Passing Behaviour . . . . . . . . . . . . . . . . . . 122
5.6.4 Pass by Reference with ByRef Keyword . . . . . . . . . . . . . . . . . . . . 122
5.6.5 Use Array to Pass Unlimited Method Argument . . . . . . . . . . . . . . . . . 123
5.6.6 Hands-on: Pass by Array - Calculate average . . . . . . . . . . . . . . . . . . 124
5.6.7 Method Overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
5.6.8 Lambda Expression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125
5.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
6 Topic 6: Windows-based Application Development 127
6.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
6.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
6.3 Graphical User Interface with Windows Forms App . . . . . . . . . . . . . . . . . . . 127
6.3.1 Label, TextBox and Button . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
6.3.2 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
6.3.3 GroupBox and Panel . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
6.3.4 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
6.3.5 CheckBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
11
6.3.6 RadioButton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130
6.3.7 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
6.3.8 Tooltips . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
6.3.9 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
6.3.10 NumberUpDown . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
6.3.11 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134
6.3.12 Menus . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
6.3.13 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
6.3.14 DateTimePicker . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
6.3.15 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
6.3.16 ListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
6.3.17 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
6.3.18 CheckedListBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
6.3.19 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
6.3.20 ComboBox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 139
6.3.21 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140
6.3.22 Mouse-event handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
6.3.23 Activity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
6.3.24 User Control . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
6.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
7 Topic 7: Advanced Windows-based Application Development 144
7.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
7.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144
7.3 Windows Presentation Foundation (WPF) . . . . . . . . . . . . . . . . . . . . . . . 144
7.3.1 WPF and DirectX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
7.3.2 WPF and Hardware Acceleration . . . . . . . . . . . . . . . . . . . . . . . . 145
7.3.3 WPF and Resolution Independence . . . . . . . . . . . . . . . . . . . . . . . 146
7.3.4 WPF Units . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
7.3.5 WPF and System DPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
7.3.6 WPF and XAML . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 148
7.3.7 Loading and Compiling XAML . . . . . . . . . . . . . . . . . . . . . . . . . . 149
7.3.8 Performance of XAML: Usefulness of BAML . . . . . . . . . . . . . . . . . . 151
7.3.9 Separation of User Interface Concerns . . . . . . . . . . . . . . . . . . . . . 152
7.3.10 Declarative vs. Imperative Programming . . . . . . . . . . . . . . . . . . . . 152
7.3.11 WPF as A Higher-Level Application Programming Interface (API) . . . . . . . . 155
7.4 Graphical User Interface with WPF . . . . . . . . . . . . . . . . . . . . . . . . . . . 157
7.5 Database and LINQ to SQL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160
7.5.1 Relational database . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
7.5.2 Step-by-step guide: Define a Data Source . . . . . . . . . . . . . . . . . . . 161
7.5.3 Step-by-step guide: Manipulate records . . . . . . . . . . . . . . . . . . . . . 165
7.5.4 Changing the database’s copy to Output Directory property . . . . . . . . . . 167
7.5.5 Step-by-step guide: Visualize Database Design . . . . . . . . . . . . . . . . 168
12
7.5.6 Step-by-step guide: Establish the connection to WPF . . . . . . . . . . . . . . 170
7.5.7 Reading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 171
7.6 Useful codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 172
7.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 176
8 Topic 8: Wed-based Application Development 177
8.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
8.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 177
8.3 Building an ASP.NET Web Application . . . . . . . . . . . . . . . . . . . . . . . . . 178
8.4 Practice: Dynamic web time . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 178
8.5 Interface of Web application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 183
8.6 Validation controls . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 184
8.7 Postback . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 186
8.8 Session tracking . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
8.9 Cookies . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 187
8.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 188
9 Topic 9: Advanced Wed-based Application Development 189
9.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
9.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 189
9.3 Display data from a database using the Web Form . . . . . . . . . . . . . . . . . . . 189
9.4 Create the Web Form with database connection . . . . . . . . . . . . . . . . . . . . 193
9.5 Useful codes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 194
9.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 196
10 Topic 10: Object-Oriented Programming (OOP) 197
10.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
10.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197
10.3 Classes, objects, methods and instance variables . . . . . . . . . . . . . . . . . . . 197
10.4 Variable types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 199
10.5 Constructor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 200
10.6 Inheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
10.7 Method overloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
10.8 Encapsulation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 201
10.9 Polymorphism . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 202
10.10Method overriding . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
10.11Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 203
10.12Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 204
11 Topic 11: Advanced VB Programming with OOP 205
11.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
11.2 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
11.3 Arrays . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 205
11.4 Linked list . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 206
13
11.5 Stacks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
11.6 Queues . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 208
11.7 Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 209
11.8 Collections . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 210
11.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211
12 Topic 12: Exception Handing with VB Programming 212
12.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
12.2 Exception Handling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 212
12.3 Debugger - Breakpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
12.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 215
13 Topic 13: Multithreading with VB Programming 216
13.1 Objectives . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
13.2 Concurrent programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 216
13.3 Thread life cycle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 217
13.4 Thread-priority scheduling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
13.5 Thread Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 218
13.6 Producer/Consumer Relationship . . . . . . . . . . . . . . . . . . . . . . . . . . . . 219
13.7 When without Thread Synchronization . . . . . . . . . . . . . . . . . . . . . . . . . 219
13.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 220
14
1 Topic 1: Overview of Programming Languages
1.1 Objectives
By the end of this section, you should be able to:
i. Explain the characteristics for each category of programming languages.
ii. Discuss the advantages and disadvantages for each category of programming languages.
iii. Compare between procedural, object-oriented and event-driven programming.
iv. Summarise the types of event-handler design patterns.
1.2 Introduction
We communicate with each other through the use of languages such as English, Malay, Hindi, Can-
tonese and Mandarin daily. Similarly, programmers use a variety of languages, called programming
languages to communicate with the computer. Some of the popular programming languages are VB,
Visual C++, C, C#, Java, Ada, COBOL and so on. This section provides you with an overview of
low-level to high-level programming languages. The brief introduction given to each programming lan-
guage here is not intended to teach you how to program, but to show you important characteristics and
differentiating features among languages. This section emphasises on the factors to be considered
when choosing a programming language.
1.3 Categories of programming languages
Programmers use various programming languages to write instructions. Programming language is
a set of words, symbols, and codes that are used to write instructions to a computer. There are
dozens of programming languages and tools available to write a program. Each language is designed
for specific purposes, such as for engineering, business and scientific applications. Programming
languages are commonly divided into three general types: low-level languages, high-level languages,
and very-high-level (fourth-generation) languages.
15
1.3.1 Low-level languages
Machine and assembly languages are called low-level languages. Machine languages are directly
understood by computers because of its use of binary digits (1s and 0s), also known as first gener-
ation programming language. Machine languages are machine dependent (i.e., each given machine
language can be used on only one type of computer). Nowadays, no one writes machine-language
programs anymore because it is difficult to learn. All programs written in a high-level language must
be translated by a compiler or interpreter into machine code before they are executed. The main
drawback of machine languages is that the coding is tedious, time consuming and error prone.
Because machine language was simply too tedious for programmers, assembly language was devel-
oped and also known as second generation programming language,. Assembly language instructions
are made up of symbolic instruction codes (meaningful abbreviations) and symbolic addresses (mean-
ingful names). Examples of instruction codes are A for addition and M for multiplication. An example
of memory addresses can be FEE, which may be used to refer to the storage location that contains a
student course fees. An assembler is used to translate the assembly source code to machine code.
Although assembly languages are fast and require little storage space compared with higher-level
languages, it takes longer time to write and maintain, and is still difficult to learn.
1.3.2 High-level languages: Procedural languages and object-oriented languages
High-level languages are categorised into procedural language and object-oriented language. High-
level language requires less coding and makes programs easier to write compared to assembly
language because it uses a series of English-like statements to write instructions (e.g., Total =
number1 + number2) and contain commonly used mathematical notations. Examples of procedural
languages are COBOL, Pascal, C, and FORTRAN. Examples of object-oriented languages are VB,
C++, C# and Java. Programmers must use a compiler or interpreter to translate the entire program
into machine code before they can be executed. Figure below compares machine, assembly and
high-level languages.
16
1.4 Advantages of high-level languages
The following are some of the advantages of programming in a high-level language.
i. Easy to write: All high-level languages provide structure (sequence, selection and iterations)
that makes the program easy to write and understand. The programs are designed in modular-
ity, which increases the programmer’s productivity. Programs are relatively small compared to
the equivalent programs written in assembly language.
ii. Easy to debug and maintain: Programs written in high-level languages are easy to debug and
maintain. For example, each procedure is specialised to perform one task or several related
tasks. Assembly languages (unstructured programs) tend to be lengthy and take more time to
code and debug.
iii. Easy to understand: In a high-level language, we can use meaningful procedure names with
relevant remarks. The meaningful variable names help the programmers to remember the
purpose of each variable.
1.5 Disadvantages of high-level languages
The following are some of the disadvantages of programming in a high-level language.
17
i. Programs run slowly: Programs written in high-level language can be very large and require ex-
tra space and memory. The execution is slower than equivalent assembly language programs.
ii. Difficult to design graphical user interface (GUI) program. GUI programs can be very difficult to
design using C language, Pascal or COBOL.
iii. Programs need translation: Programs have to be converted to machine language and this is
time consuming. Programmers have no control over the hardware as compared to low-level
languages.
1.5.1 Example of high-level language/procedural language
This section introduces the procedural language structure of the C programming language to you. It
looks similar to Java programs but the syntax is different. Do not worry if you cannot understand the
following program, you will learn it in the next course Structured Programming. The C programming
language effectively enforce the structures procedures, thus it is referred to as a structured language.
We often use the term structured language to represent any high-level procedural languages, such
as C. The following program written in C language is to calculate and print the sum of three numbers
input by a user.
1 /* This program is to calculates and prints
2 the sum of three numbers input by the user
3 */
4 #include <stdafx.h>
5 #include <stdio.h>
6 int main (void)
7{
8 // Local Declarations
9 int num1;
10 int num2 ;
11 int num3 ;
12 int sum ;
13
14 // Statements
15 printf ( " This program is designed to add \ n " ) ;
16 printf ( " three numbers . Please enter three numbers \ n " ) ;
17 printf ( " in the form : nnn nnn nnn < return >\ n " ) ;
18 scanf ( " % d % d % d " , & num1 , & num2 , & num3 ) ;
19
20 // Numbers are now in num1 , num2 , and num3 . Add them . */
21 sum = num1 + num2 + num3 ;
22 printf ( " The total is : % d \ n \ n " , sum ) ;
23 printf ( " Thank you . Enjoy your programming .\ n " ) ;
24 return 0;
25 }
Observe the above C code. First, it reads three numbers using the scanf statement. %d refers to
an integer format. The second process adds the three numbers: total = num1 + num2 + num3.
Finally, we print the result using (printf ) statement.
18
1.6 Object-oriented programming (OOP)
There are more advanced high-level languages available for object-oriented programs, such as C++,
C#, VB, and Java. The development of GUI application programs that can display multiple windows
containing graphical forms, texts, menu and buttons contribute to the popularity of object-oriented
programming. In a windows environment, each form, CheckBox, ListBox, and button on the screen is
an object associated with characteristics such as colour, position, and size.
An advantage of using objects is that it can be reused in more than one program with little or no modi-
fication to the code, because each object is viewed as an independent unit. So, it saves programming
time and development cost. However, object-oriented languages still retain structured features in their
procedures. Thus, C++, C#, Java, and VB implements the basic structured procedure types. In this
course, you will learn object-oriented programming using the VB programming language. You are
already familiar with many of the object-oriented concepts and programming techniques that were
presented in course TCC128/05 Programming Fundamentals. We hope that you will have a clearer
picture of OOP in VB upon completion of this course.
1.6.1 Fourth-generation languages (4GLs)
4GL or very-high-level languages are much easier to use because they are declarative rather than
procedural languages. An 4GL is a non-procedural language that uses English-like statements and
interacts with a graphical environment to retrieve data from database. For example, if you want to
draw a pie chart in a procedural language, you must tell the computer how to draw a pie and where
to place the dividers. In 4GL, you just point to the data you want graphed and click several menu
choices. The most common 4GL used to access databases is Structured Query Language (SQL).
The drawback of a 4GL is that the programs run slower than third generation languages (high-level
languages) because their machine code equivalent is longer and more complicated to execute. You
will learn SQL in course TCC233/05 Database Management Systems.
1.6.2 Activity
Answer the following questions.
1. C++ and Visual Basic are examples of a(n) .
(A) an assembly language
(B) object-oriented programming language
(C) structured programming language
(D) Visual Studio language
2. Which of the following is considered as the most popular visual programming language?
(A) Visual Basic
(B) COBOL
19
(C) C++
(D) Pascal
3. Computers can directly understand language, which is composed only of 1s and 0s.
4. Programs that translate high-level language programs into machine language are called .
5. VB is an object-oriented language. True/False?
6. Why is 4GL easier to use than high-level language?
Activity - Suggested answers
1. B
2. A
3. machine
4. Compiler
5. True
6. 4GL or very-high-level languages are much easier to use than the high-level languages, be-
cause they are declarative rather than procedural.
1.6.3 Reading
To get more information about programming languages, please read the following:
i. He, H (2012) ’Programming languages’ in Coding Interviews, New York: Apress, pp. 13 - 32.
ii. Lee, K (2008) ’Introduction’ in Programming Languages, Springer US, pp. 1 - 20.
iii. Chivers, I (2001) ’Programming languages’ in Essential Linux fast, London, Springer, pp. 197 -
206.
iv. Rajaraman, V (1998) ’Programming languages’, Resonance, vol. 3, no. 12: pp. 43 - 4.
v. Teufel, B (1991) ’Principles of programming languages’ in Organization of Programming Lan-
guages, Springer Vienna, pp. 1 - 21.
vi. Horowitz, E (1983) ’The challenge of programming language design’ in Fundamentals of Pro-
gramming Languages, Springer Berlin Heidelberg, pp. 33 - 49.
vii. Dembinski, P (1981) ’Programming languages,’ in Marciszewski, W (ed) Dictionary of Logic as
Applied in the Study of Language, Vol. 9, Springer Netherlands, pp. 300 - 302.
20
1.7 Procedural programming vs. object-oriented programming
The following table shows a comparison between procedural programming and object-oriented pro-
gramming.
Table 2: Comparison between procedural programming and object-oriented programming.
Procedural programming Object-oriented programming
i Procedural programming is focused on creating Object-oriented programming is focused on
functions. creating objects and classes.
Procedural programming separates the data from Object-oriented programming is focused on both
ii the operations that manipulate the data. data and operations.
Procedural programming is self-contained, i.e.
most of the code exists within the program. If the Object-oriented programming focuses on the
code is used in another program, you must copy all creation of reusable objects that can be easily
iii of the code into the new program or re-create it incorporated into another program. You could even
from scratch. However, we still can solve this reuse the objects in the programs that were
problem by including the header files in C. The task created in different programming languages.
is tedious as compared to OOP.
Procedural programming does not contain object
and attributes. There will be additional variables Code and attributes are encapsulated within the
iv extended to the program as the program grows and object. It does not affect the program as the
it may affect the program in terms of information program grows.
hiding.
Object-oriented programming encapsulates those
v As the internal structure of the functions change, attributes such that a change to one part of a
they may affect other parts of the program as well. program would not affect the rest of the program at
all.
Procedural programs become difficult to maintain
as programs become more complicated. We need Object-oriented programming is more complex
vi to manipulate large amounts of variables and there than procedural programming but easier to
are no inherent connections between the variables maintain by using inheritance concepts.
and functions.
1.7.1 Activity
Answer the following questions.
1. In procedural programming, if you add attributes to the class or change the internal structure of the
attributes, you will need to change all of the functions. True/False?
2. Object-oriented programming is focused on creating objects and classes. True/False?
Activity - Suggested answers
1. False
2. False
21
1.8 Event-driven programming
GUI-based application programs written in VB are mostly event-driven. You will have a better under-
standing of event-driven programming by contrasting it with procedural programming.
1.8.1 Procedural programming and event-driven programming
Let us look at the execution path of procedural and event-driven programming. Applications written
in procedural languages fully control the execution code. The program code is executed one line at
a time and follows a predefined pathway. The program interacts with the data through the control
structures, such as IF...THEN, WHILE..., FOR..., and the execution path is completely controlled
by the program. We would say that, a program was restricted in how it could respond to the user.
For example, the program expected text to be entered into a control box in a predetermined order.
Flowcharts are usually used by the developers to structure the programs.
Operation of a procedural program in a predefined pathway
Above figure illustrates an execution of a simple program using a sequence structure. The program
executes the statements to perform the required tasks and then the program ends.
In event-driven programming, the sequence of operations for a program is determined by the user’s
interaction with the program’s interface (forms, menus, buttons, and etc). We could say that the user
can choose any order while interacting with different parts of the interface. As the user interacts with
the program, events are triggered by Windows. Thus, the execution path of a program is controlled
by events, such as clicking a button. A program cannot predict when the user will perform a particular
action, thereby causing Windows to trigger the corresponding event. It is because event-driven pro-
gramming does not have a predefined pathway in the execution of the code (Watson, 2002). Figure
below shows you an example of a GUI-based event-driven program, where users can choose to click
any button.
22
The benefit is that no pre-structure for the code is needed and the users have the flexibility to modify
parts of the code easily without interfering with the rest of the code. We summarise the characteristics
of procedural and event-driven programming in the table below.
Table 3: Characteristics of procedural and event-driven programming.
Characteristics Procedural programming Event-driven programming
Users can choose any order while
Execution path Program code is executed one line at a time interacting with different parts of the
and follows a predefined order. interface.
User interaction Multi-windows
Simultaneous users Low, in batch files One to many
Complexity, difficulty One Medium to high difficulty
Easier to program
The most common types of event-driven program are graphical Windows-driven user interface and
Web-based applications. The desktop and laptop computers you are using today are graphical
Windows-driven. Examples of Web-based applications are online movie ticket reservation systems
and online banking systems.
Event: An event is an action to which the program responds. For example, pressing a key on the
keyboard, clicking a button, double-clicking an icon, scrolling Windows and moving a mouse are called
events. Event-driven programming is when the program checks for, and responds to events. Object-
oriented programming is event-driven. An example of an object-oriented programming language is
Visual Basic.
1.8.2 Activity
Answer the following questions.
1. What is an event?
2. What is event-driven programming?
23
3. In event-driven programming, the sequence of operations for a program is determined by the user’s
interaction with the program’s interface. True/False?
Activity - Suggested answers
1. An event is an action to which the program responds. For example, pressing a key on the
keyboard.
2. Event-driven programming is about a program checking and responding to events.
3. True
1.9 Event-handlers design pattern
Figure below shows a diagram of event-handlers. You can see:
i. A stream of data items called events.
ii. A dispatcher.
iii. A set of handlers.
Event-handlers
The following is a pseudo-code for a typical dispatcher that shows all of these features discussed
above.
i. The event loop.
ii. The quit operation.
iii. The determination of event type and the selection of an appropriate handler.
iv. The treatment of events without handlers.
Event loop
1 do forever: # the event loop
2 // get an event from the input stream
24
3
4 if event type == EndOfEventStream :
5 quit # break out of event loop
6
7 if event type == ... :
8 call the appropriate handler subroutine , passing it event
information as an argument
9
10 else if event type == ... :
11 call the appropriate handler subroutine , passing it event
information as an argument
12
13 else : # handle an unrecognised type of event ignore the event , or
throw an exception
The function of a dispatcher is to analyse each event to determine its event type and then send each
event to a handler that can handle the event type. The dispatcher must process a stream of input
events, the logic must include an event loop so that it can get an event, dispatch it, and then loop
back to obtain and process the next event in the input stream. The event stream for most of the event-
handling applications is finite, with an end-of-file marker, or ESCAPE key, or CLOSE button in a GUI.
In those applications, the dispatcher logic must include a quit capability to break out the event loop
when end-of-stream event is detected.
Suppose that the dispatcher determines that it has no appropriate handlers for the event, it either
discards (ignore) the event or throws an exception. If the dispatcher cannot recognise an event, it
constitutes an error in the input stream and the appropriate action is to throw an exception. An excep-
tion is an indication of a problem that occurs during a program’s execution. Throws and exception
refers to the handling of errors by displaying a message such as ’Division by Zero’.
There are a few types of event-handlers:
i. Headless handler: One of them is the headless handler where the dispatcher is either missing
or invisible. Taking away the dispatcher, all that remains is a collection of event handlers.
Event-handlers
ii. Extended handler: It is an extended handler which includes an events generator component
that generates the stream of events that the dispatcher processes.
Extended-handlers
25
iii. Event queue: Sometimes, the dispatcher and its handlers may not be able to handle events
as quickly as they arrive. In this situation, event queue was introduced into the events stream
to buffer the input stream of events, between the events generator and dispatcher. Events are
added to the end of the queue as fast as they arrive, and the dispatcher takes them off the front
of the queue as fast as possible.
Event queue
Graphical user interface programs typically include an event queue. Events such as mouse
clicks may require some time to be handled. While that is happening, other events such as
mouse-movement events may accumulate in the buffer. When the dispatcher becomes free
again, it can rapidly discard the mouse-movement events that can be ignored and quickly empty
the event queue.
Note that a design pattern is a general, reusable solution for commonly occurring problems. The basic
26
idea is that software developer can solve similar kinds of problems with similar kinds of solutions [1].
Patterns generally provide a template of how to solve a problem and can be used in many different
situations. At the same time, they help software developer to achieve the best possible design much
faster.
Design pattern for satisfying the requirement of event occurring and notifying is called the publisher/-
subscriber pattern. In the design pattern, a class, called the publisher, defines a set of events that
other parts of the program might be interested in. Other classes can then ’sign up’ to be notified by the
publisher when these events occur. These subscriber classes ’sign up’ for notification by supplying a
method to the publisher. When the event occurs, the publisher ’raises the event’, and all the methods
submitted by the subscribers are executed. The methods supplied by the subscribers are known as
callback methods because the publisher ’calls the subscribers back’ by executing their methods.
They are also known as event handlers because they are the code that is called to handle the event.
You have learned the following items related to events under the design pattern:
i. Publisher: A class or struct that publishes an event so that other classes can be notified when
the event occurs.
ii. Subscriber: A class or struct that registers to be notified when the event occurs.
iii. Event handler: A method that is registered with the publisher, by the subscriber, and is executed
when the publisher raises the event. The event handler method can be declared in the same
class or struct as the event or in a different class or struct.
iv. Raising an event: The term for invoking or firing an event. When an event is raised, all the
methods registered with that event are invoked.
There are five pieces of code that need to be in place to use events, as follows.
i. Delegate type declaration: The event and the event handlers must have a common signature
and return type, which are specified by the delegate type. described by a delegate type.
ii. Event handler declarations: These are the declarations, in the subscriber classes, of the meth-
ods to be executed when the event is raised. These do not have to be explicitly named methods;
they can also be anonymous methods or lambda expressions.
iii. Event declaration: The publisher class must declare an event member that subscribers can
register with. When a class declares a public event, it is said to have published the event.
iv. Event registration: The subscribers must register with an event in order to be notified when it
has been raised. This is the code that connects the event handlers to the event.
v. Code that raises the event: This is the code in the publisher that ’fires’ the event, causing it to
invoke all the event handlers registered with it.
27
1.10 Summary
There are many object-oriented programming languages available; some of the most popular lan-
guages include C#, VB, Java, C++. Some old languages such as COBOL have been modified to
support the object-oriented approach. However, selecting what programming language to use for a
particular program involves balancing the functional characteristics of the language. The following are
some of the factors to be considered when selecting a programming language such as:
i. The ability of the language to interface with other programs.
ii. The suitability of the language to the application.
iii. The portability of the language to other systems.
iv. The language’s support for either structured/object-oriented programming concepts.
v. The speed of program execution.
vi. The likelihood that changes will have to be made (modifications are easier to make in some
languages than in others).
You have learned about event-driven programming including the comparison with procedural pro-
gramming and different types of event-handlers. In the next section, we will review the concepts of
object-oriented principles. We help you to familiarize with the object-oriented programming terms. Do
not worry if you do not understand everything right now; you will see further examples of these terms
throughout the course.
1.10.1 Self-test
Answer the following questions.
1. A disadvantage of object-oriented programming is that you are unable to reuse and modify existing
objects. True/False?
2. VB language program must be converted into machine language before the computer can run the
program. True/False?
3. An object-oriented programming (OOP) language is , which means an OOP language program
responds to events such as pressing a button.
(A) action driven
(B) event driven
(C) respond based
(D) object-oriented
4. To convert VB source program into machine language, you use a program called a(n) .
(A) assembler
28
(B) machine converter
(C) compiler
(D) converter
5. Give one advantage of OOP.
6. What are some examples of events in OOP?
7. Assembly languages are completely difficult to understand. True/False?
8. What is a database?
9. SQL is an example of .
(A) assembly language
(B) English language
(C) high level language
(D) human language
10. The benefit of very-high-level language is that you do not need to specify how the data should be
processed. True/False?
Self-test - Suggested answers
1. False
2. True
3. B
4. C
5. A major benefit of OOP is the ability to reuse and modify existing objects.
6. Pressing a key on the keyboard, clicking a button, moving a mouse, typing a value in a textbox.
7. False. Assembly languages consist of English abbreviations that allow humans to read code,
although the code often remains rather complicated.
8. A database is a collection of data that is used by a system.
9. D
10. True
29
2 Topic 2: Review of Object-Oriented Principles
2.1 Objectives
By the end of this section, you should be able to:
i. Define and recognise the relationship between objects, attributes and behaviours.
ii. Recognise a method.
iii. Distinguish characteristics of objects and classes and how they communicate with each other.
iv. Differentiate between objects, instance and class.
v. Illustrate subclass and superclass inheritance using a Unified Modelling Language (UML) dia-
gram.
2.2 Introduction
This section introduces you to the principal characteristics of object-oriented programming. You will
be familiar with the basic terminology presented in this section if you can recall what you learnt in
Java programming using object-oriented approach. Understanding object technology in general is
essential to the development of VB programming for beginner. We hope that you will enjoy learning
and embarking on a challenging path.
2.3 What is object?
In general term, an object is anything tangible that can be seen, touched or used. Example of objects
in real life are: students, cars, chairs, and tables. The Menu, Label, Textbox, PictureBox and Form
in Windows programs are also objects. As you already know, all these objects can communicate with
each other by sending messages. Form object and control object are commonly used objects in
VB. You will learn how to create Form and controls at the end of Unit 1. Button and TextBox are
examples of control objects.
We use rectangles with an underlined name to represent an object in a Unified Modelling Lan-
guage (UML) 11 diagram. Figure below shows a graphical UML representation of an object named
TCC123.
Graphical UML representation of an object.
11Read on more at URL https://en.wikipedia.org/wiki/Unified_Modeling_Language
30
2.3.1 Activity
Answer the following questions.
1. Give two examples of real-world objects that we did not mention in this unit.
2. Almost any adjective can be reasonably represented as an object. True/False?
Activity - Suggested answers
1. Dog, Radio. (Answers may vary).
2. False. Almost any noun can be reasonably represented as an object.
2.4 Object properties and behaviour
All objects have some things in common. They all have attributes (e.g., length, height, colour, size)
and behaviours (e.g., a person runs, a baby plays, a ball rolls). An object ’has state, behaviour and
identity’. Every object can remember its current state and will hold the data within the program. This
data is called an attribute and each attribute has a name, a type and a value. The following table lists
some characteristics of a pencil and a person object.
Table 4: Characteristics of some objects.
Object Identity Behaviour Attributes States
A pencil ’2B’ Write Length Unsold
A person ’John’ Walk Height, Weight Studying
More specifically, we can conclude that an object is an entity that contains related attributes (proper-
ties) and behaviours (operations). These attributes are the characteristics that describe the object.
A cat is an object. This cat object has certain properties common to all cats. These properties can
include attributes such as the cat’s name and the colour of its hair.
The behaviours (operations) are implemented in methods, which are the actions that an object can
performs. Operations are defined at the class and different objects perform different operations. An
Automatic Teller Machine (ATM) is an object that can perform operations called methods in VB, such
as withdrawals, balance enquiry, fund-transfers, and so on. On the other hand, a car is an object
that can perform operations such as accelerate, brake, turn and so on. A cat can perform tail wag-
ging.
In VB, attributes are properties and you will use Properties window in the Microsoft Visual Studio later
on. We will show you how to read and change properties using the Properties window.
2.4.1 Activity
Answer the following questions.
31
1. Refer to Activity 2.3.1 for those real-world objects that you mentioned. For each object, list some
attributes and behaviours.
2. The attributes or characteristics of an object are called .
Activity - Suggested answers
1. Dog: Attributes (Age, Size). Behaviours (A dog barking, tail wagging); Radio: Attributes (chan-
nel, volume). Behaviours (A radio turns on and off, changes channel).
2. Properties.
2.5 Methods
In an object-oriented programming, objects communicate with each other by sending messages. In
our real life examples, people communicate with each other by sending messages too. The SMSes
we send to our friends, the magazines we read, and the movies we watch on TV are all messages.
Similarly in the programming, the sender object sends messages (methods) only to the classes and
objects. The receiver class or object must possess a matching method in order to handle the received
message. In return, the receiver object executes a corresponding method.
Message passing between two objects.
In above figure, the Person object consists of methods (getAge, setDOB, getStudName, setStudName)
and its instance variables (Studname and DOB), which are private and hidden inside. The sender ob-
ject communicates with the Person object by sending message (getAge) to it. The sender does not
know how the getAge method is implemented. However, we should know that there would be an
age variable or constant value to calculate the age based on the date of birth variable. It is critical
that an object knows only its own data, and its own operations. In order for collaboration, a Sender
object knows which other object to ask, and how to formulate the question. Suppose that a sender
object is required to know about another object delivering the service, it would definitely produce an
undesirable degree of coupling in the relationship between these two objects.
When a Person object receives a message, it knows whether the message is relevant or not. Sup-
pose that the message includes a valid signature to one of its operations, the Person object can
32
respond. Otherwise it does not, the Person object cannot respond.
2.5.1 Activity .
Answer the following questions.
1. The behavioural operations of a class are implemented as
(A) methods
(B) attributes
(C) activities
(D) collaborations
2. Which of the following is an attribute for a Student class?
(A) principal
(B) advisor
(C) age
(D) parent
Activity - Suggested answers
1. A
2. C. Principal is a component, advisor and parent are peer objects, age is a value to distinguish
between each student.
2.6 Encapsulation and information hiding
Encapsulation is one of the three fundamental pillars of an object oriented program. In an object-
oriented programming, class is used to encapsulate the attribute and behaviour of an entity. As data
(attributes) and methods (behaviours) are defined in a single entity, it is called Encapsulation. It also
known as Data Hiding or Information Hiding referring to the object-oriented design principle, which
restricts the accessibility of data (attribute) and method (behaviour) of an entity that are not necessary
to its user. In layman’s terms, we could express that each object has a shield around it so that objects
cannot see the internal details of each other. Encapsulation can enhance maintainability because the
implementation details are invisible to other objects.
We use a Saving Account class example to illustrate the concepts behind encapsulation.
33
2.6.1 Saving Account class
Attribute accBalance can be implemented in many ways: It can be as a total amount (e.g. RM20) or
with two decimal places for the cents (e.g. RM20.00). With encapsulation, we can make the details
invisible by describing an accBalance attribute as private. The Saving Account class’s operation is
deposit. We can modify accBalance of a Saving account object by sending message to deposit
method.
Saving Account class
Objects can exchange information if they are associated with each other. For example, an Automatic
Teller Machine (ATM) is an object which provides you with cash. The ATM encapsulates (wraps)
all its attributes and operations. If some objects bypass the encapsulation, it is considered as intruding
bank security.
Consider another example such as printing a payslip. Each PaySlip object must know an employee
salary. Each PaySlip object sends a message to the Employee object, asking how much salary
to pay. The PaySlip object does not need to know how the Employee object works out the salary;
it just needs to ask the Employee object for a salary figure. An appropriate response is given if
the salary matches the Employee object. This message passing allows objects to encapsulate their
internal details from other parts of the system, thus minimising any changes made to the design or
implementation.
The benefits of the encapsulation allow you to:
i. Build flexible systems. The systems can extend as your business grows.
ii. Change the modules of the system independently without affecting other parts of the modules.
iii. Ignore the low level details of how work is implemented internally.
With encapsulation, we can conclude that:
i. There are no regression faults. A regression fault is a change in one part of unrelated code that
will cause a fault in other parts of code.
ii. A system consists of independent classes. They communicate by sending messages.
2.6.2 Activity
Answer the following questions.
34
1. What is the main characteristic of encapsulation?
(A) A base class derived from a child class.
(B) Objects are grouped into different hierarchies.
(C) Implementation details of attributes and methods are hidden.
(D) Sending messages from one to another.
2. Designing a class so that it shields certain parts of an object from other objects is an example of
the principle.
Activity - Suggested answers
1. C
2. Information hiding
2.7 Object associations
Association is a relationship between two classes. In order for an object to send a message to another,
it needs to be associated with other objects. An object needs to know the recipient of the message
via a link. A link is a logical connection between two or more objects. This link is called association.
One example of association is ’Many instructors teach many students’. In this case, teach is a link
between the instructor and student.
Saving Account class
An association can be implemented in three ways:
i. One-to-one association. Example: An owner owns a car. The owns association is an example
of a one-way association.
ii. One-to-many association. Example: A staff manages many projects. The manages association
is an example of one-to-many associations.
iii. Many-to-many association. Example: Many staffs work on many projects. The work on is an
example of many-to-many associations.
35
2.8 Class and instance
Let us define the differences between the object, instance and class. The UML specification gives a
definition between the three concepts. An object is an instance (member) of a class. Another way, we
say that a class consists of a set of related objects. For example, in the table below, customers have
something in common, such as name, address and telephone number.
Table 5: Common Customer information.
Name Mr Baba
Address No. 54, Jalan Sultan Ahmad Shah, 10050 George Town, Penang.
Handset No. 0181180488
Name Ms Chong
Address No. 10, Jalan Sultanah Bahiyah, 06010 Sintok, Kedah.
Handset No. 0121120422
Let the set of all customers be called Customer class. Figure below shows the Customer class in
UML diagram. We conclude that: Baba is an instance of Customer class, and so is Chong. Customer
class is a set of customers.
Customer class
A Customer class specifies the attributes and operations for all instances of that class. The objects
produced by a Customer class have identical structures such as same instance variable and methods.
For example in table below, a company records a customer’s name, address and telephone number
and so on. The value of each item (customer name) is different from one customer to another, but this
information structure is the same for every customer.
Table 6: Information structure for Customer class.
Class Characteristic
Name
Customer Address
Telephone Number
In order to describe the characteristics of all customers, you might define a Customer class and
specify operations, such as Purchase, and attributes, such as Name in Figure below. You can then
36
create individual Customer objects to represent walk-in customers or existing customers.
Customer class
2.8.1 Activity
Answer the following questions.
1. An object is also known as an instance of a method. True/False?
2. A is a template for creating an object.
Activity - Suggested answers
1. False
2. Class
2.9 Inheritance
Inheritance is a relationship between classes. When an object of the child class is created, it inherits
all the attributes and behaviours of the parent class, and together with those defined in the child class
itself. The parent class is called a superclass and the child class is called a subclass.
For example, cars, airplanes, and ships are all types of vehicles. In object-oriented approach, cars,
airplanes and ships are all subclasses of the Vehicle class. Similarly, the Vehicle class is the super-
class of cars, airplanes, and ships. This relationship is shown in the following UML diagram.
Vehicles
37
When you define two or more classes, you may find that these classes share a lot of common attributes
and operations. Defining common attributes and operations in a superclass can improve productivity.
Refer to the Vehicle class example in above figure. Vehicle would contain the variable declarations
and methods, such as vehicle model and vehicle make that are common to car, airplane and ship.
This means that:
i. The Car, Airplane and Ship classes have all the features of the Vehicle class.
ii. Each child class also has its own features that are specific to itself.
Take note: The following statements are equivalent:
i. Car class is a subclass of Vehicle class.
ii. Vehicle class is a superclass of Car class.
iii. Car class is a specialisation of Vehicle class.
iv. Vehicle class is a generalisation of Car class.
v. Car class is a Vehicle class.
vi. Vehicle class is the base class, Car class is the derived class.
vii. Vehicle class is the parent class, Car class is the child class.
viii. Car class inherits from Vehicle class.
The benefit of inheritance is to make the class code reusable. This can save time and effort, by
re-using existing code that has already been tested.
2.9.1 Activity
Answer the following questions.
1. A superclass inherits classes from a subclass. True/False?
2. A subclass can itself be a superclass of other classes, therefore forming an inheritance hierarchy.
True/False?
3. A subclass can inherit only part of its superclass. True/False?
Activity - Suggested answers
1. False
2. True
3. False
38
2.10 Polymorphism
Polymorphism means ’an ability to appear in many forms’. Suppose that an identical message is
being sent to objects of different classes, each object will respond to the message in a different way.
In short, we say that objects of different types can respond differently to the same message. Take,
for example, sending move messages to instances of Fish and Bird. Each instance has its own
move method to implement the move operation. To simulate the animals’ movements, the program
sends each object the same message - move. However, each specific type of animal responds to
the move message in a unique way - a Fish might swim two feet and a Bird might fly ten feet. The
program issues the same move message to each animal object generically, but each object knows
appropriately the specific type of movement.
An example of polymorphism
Also consider the sending out of a speak message. The sender object does not need to know which
class is going to receive the speak message. The receiving object is responsible for knowing how to
respond to the message. The advantage of polymorphism is that the speak operation can be invoked
on an object of any of the two classes, and the correct method is called automatically.
2.10.1 Activity
Answer the following questions.
1. Polymorphism operations have identical signatures. True/False?
2. A method performs different things depending upon the object that invokes it.
Activity - Suggested answers
1. True
2. Polymorphic
39
2.11 Summary
This section introduced object-oriented principles to you, in particular, object and class, methods,
associations, encapsulation, inheritance and polymorphism. These will give you an essential foun-
dation of VB programming in the later section. You are already familiar with many object-oriented
terminologies and concepts, which you already studied in semester 1. Do not be worried if you do
not understand everything right now; you will see further examples and explanations of these terms
throughout the units.
In the next section, you will use the Visual Studio IDE (Integrated Development Environment) to create
some of the basic controls such as Button, TextBox, Label, PictureBox and ListBox. You will
also learn about managing and configuring the IDE. Finally, you learn to write a code in response to
an event.
2.11.1 Self-test
Answer the following questions.
1. What is an object? Give some examples.
2. If we have Animal, Insect, Mammal, and Fish classes, which one will be the superclass?
(A) Animal
(B) Insect
(C) Mammal
(D) Fish
3. are the operations that an object is capable of performing.
(A) Activities
(B) Attributes
(C) Objects
(D) Methods
4. An object is also called as instance of a class. True/False?
5. A subclass inherits classes from a superclass. True/False?
6. The hiding of a class’s attributes and behaviours is called .
7. The major benefit of programming is that it produces a program that is more understandable
and better organised than a program produced with structured programming techniques.
(A) object-oriented
(B) procedural
40
(C) JavaScript
(D) C
8. Object-oriented programming produces software that requires less maintenance than software
created using other programming methods. True/False?
9. What are the two most essential concepts in object-oriented programming?
(A) Objects and Java.
(B) Objects and C++.
(C) Objects and classes.
(D) Objects and VB
10. The name getName represents what type of construct on object-oriented programming?
(A) A. An argument.
(B) A method.
(C) A variable.
(D) A parameter.
Self-test - Suggested answers
1. An object is anything tangible that can be seen, touched or used. Example of objects in real life
may include: a student, an employee, a table, a chair, a table, an engine, a cup. The menus,
buttons, and so on in Windows programs are objects. These objects can communicate with
each other by sending messages from one object to another.
2. A
3. D
4. True
5. True
6. encapsulation
7. A
8. True
9. C
10. B
41
3 Topic 3: Introduction to Visual Basic (VB)
3.1 Objectives
By the end of this section, you should be able to:
i. Perform the installation for Microsoft Visual Studio (VS).
ii. Manage the VS Integrated Development Environment (IDE) for VB programming.
iii. Set object properties.
iv. Choose a proper name for a control according to the naming convention.
v. Create the TextBox, Button, Label, PictureBox and ListBox controls using the Toolbox.
vi. Close and open an existing solution file.
vii. Create, compile, execute and debug a simple VB program.
viii. Write event procedure codes in the Code Editor window.
3.2 Introduction
Microsoft Visual Basic, also known as Visual Basic (VB), is a programming language that offers pow-
erful features such as graphical user interface (GUI), event handling, object-oriented programming
and exception handling. VB is an event-driven language (i.e., the programs respond to user-initiated
events such as mouse clicks or key presses), and it is also a visual programming language in which
programs are created using an Integrated Development Environment (IDE). With the IDE, a program-
mer can create Windows, Web and mobile (e.g., tablet PC, smart phone) applications. In this course,
you will find that VB can increase your productivity in software development work.
In this section, you will learn how to install and start VS IDE, and how to use the VS IDE to open and
run a simple Visual Basic program. You will also learn some of the essential command and change
window properties. Finally, you will write some code to demonstrate an event.
3.3 System requirements
We recommend that you have hardware and software that are equivalent or better than the re-
quirements of VS IDE to complete the exercises and example programs in this course. The de-
tails of the requirements can be obtained from: https://visualstudio.microsoft.com/vs/
community/
3.4 Visual Studio software support
The following two websites enable you to communicate with professional Visual Studio developers
and Microsoft employees, as well as to access relevant newsgroups, technical chats and so on:
42
i. Microsoft developer resources for building application: http://msdn.microsoft.com/en-my/
vstudio/
ii. Microsoft supports for Visual Studio https://visualstudio.microsoft.com/vs/support/
3.5 Installing Visual Studio
There are various editions of Visual Studio, and the details of each edition are provided at https://
visualstudio.microsoft.com/products/. Visual Studio (VS) Community edition is the choice
for this course. Note that VS Community edition is a full-featured IDE from Microsoft, which was
released in 2013. You may obtain a free copy from: https://visualstudio.microsoft.com/
vs/community/
During the installation of Visual Studio (after completed the Windows updates), we recommend that
you choose all available options. The installation procedure is straightforward and the process takes
about an hour or more to complete (depending on your Internet access speed). For example, ex-
plore more details on the installation of VS 2019 either from book [3] or web site https://docs.
microsoft.com/en-us/visualstudio/install/install-visual-studio?view=vs-2019.
The features of Visual Studio are listed at https://visualstudio.microsoft.com/vs/. Visual
Studio offers .NET Framework, which incorporates many features that enable Windows and Web
application developers to develop more interactive and powerful applications.
The selected features of Visual Studio covered in this course are as follows:
i. LINQ (Language Integrated Query) - LINQ provides a uniform syntax for querying data and for
performing INSERT, UPDATE and DELETE operations in a database. You will learn more about
LINQ in Week 4.
ii. WPF (Windows Presentation Foundation) - This is a framework introduced by Microsoft which
integrates GUI, graphics and multimedia. Through the use of the WPF, you will be able to design
richer GUIs and build in a lot more interaction into your application. You will learn more about
the WPF in Week 4.
iii. ASP.NET - The .NET platform enables the development of much more robust server-side ap-
plications which enhance the capabilities of a website. You will learn more about ASP.NET in
Weeks 5 and 6.
Do not worry if you do not understand all these features right now. You will find that they are very easy
to understand as you go through this course. We will introduce the Microsoft .NET framework and its
history next.
3.6 .Net framework
In the late 1990s, Windows programming using the Microsoft platform had fractured into a number of
branches. Most software developer were using VB, C, or C++. Some C and C++ developers were us-
ing the raw Win32 API, but most were using Microsoft Foundation Classes (MFC). Others had moved
43
to the Component Object Model (COM). All these technologies had their own problems. For example,
the raw Win32 API was not designed in object-oriented, and it required tremendous development ef-
forts as compared with MFC. MFC was designed in object-oriented, but it was inconsistent and getting
old. COM, although conceptually simple in design paradigm, was complex in its actual coding and
required lots of maintenance. Moreover, all these programming technologies were aimed primarily at
developing code for the desktop-oriented rather than the Internet-oriented.
Note that an integrated, object-oriented development framework bring consistency back to today’s
programming exercises. To meet this need, Microsoft set out to develop a code execution environment
and a code development environment, via the Visual Studio integrated development environment
(IDE), that met these goals. Microsoft released the first version of the .NET Framework in year 2002.
Microsoft promised to address the old problems and meet the goals for the next-generation systems.
The .NET Framework is a much more consistent and object-oriented environment than either the MFC
or COM programming technologies. The .NET Framework consists of three components, as shown in
Figure below.
Components of the .Net framework [2].
3.6.1 .Net Common Language Runtime (CLR)
Suppose that you have studied Java programming in previous semester/s. Java is using a ’one lan-
guage, many platforms’ approach. On the other hand, .NET is using a ’many languages, one platform’
approach. Software developers may develop these programs, either in desktop, mobile and web devel-
opment technologies, using the Visual Studio integrated development environment (IDE). It contains
.NET-compliant compilers, e.g., C#, Visual Basic .NET, F#, managed C++ and so on, with respective
debuggers.
The Microsoft.NET framework is at the heart of the .NET strategy. This framework manages and
executes applications and Web services, contains a class library (also knowns as .NET Base Class
Library (BCL) or Framework Class Library (FCL)) to enforce security and provides many other pro-
gramming capabilities. BCL is a large class library used by the .NET Framework and available for
software developers to use in developing programs too!
The Common Language Runtime (CLR) is another central part of the .NET Framework - it executes
.NET programs. mainly, CLR manages program execution at run time. A .Net program is compiled
44
into machine-specific instructions in two steps:
i. The program is compiled into Microsoft Intermediate Language (MSIL) which defines instruc-
tions for the CLR. The MSIL for an application’s components is placed into the application’s
executable file.
ii. When the application executes, another compiler (known as a just-in-time compiler or JIT com-
piler) in the CLR translates the MSIL in the executable file into machine language code, then
the machine language code executes on that platform.
In other words, the primary role of the CLR is to locate, load, and manage .NET objects on your
behalf. The CLR also takes care of a number of low-level details, such as memory management,
application hosting, coordinating threads, and performing basic security checks (among other low-
level details).
Another important building block of the .NET, besides CLR, is the Common Type System (CTS). The
CTS specification fully describes all possible data types and all programming constructs supported
by the runtime, specifies how these entities can interact with each other, and details how they are
represented in the .NET metadata format.
Suppose that a given .NET-aware language might not support every feature defined by the CTS. The
Common Language Specification (CLS), another building block of the .Net, is a related specification
that defines a subset of common types and programming constructs that all .NET programming lan-
guages can agree on. Thus, if software developer build .NET types that expose only CLS-compliant
features, developer can rest assured that all .NET-aware languages can consume them. Conversely,
if developer make use of a data type or programming construct that is outside of the bounds of the
CLS, developer cannot guarantee that every .NET programming language can interact with his/her
.NET code library.
3.6.2 .Net and Object-Oriented Development Environment
A integrated environment of CLR, BCL and .Net languages forms a consistent, object-oriented model
of programming for both local programs and distributed systems. It is not limited to the software
development interface for desktop, mobile, and web application programming, consistent across a
broad range of targets, from servers to mobile devices. The object-oriented development environment
offers unique services as follows.
i. Automatic Garbage Collection (GC) - it is an automatic memory management from CLR. The
GC automatically removes objects from memory that your program will no longer access. With
only a few exceptions, the GC relieves programmers of tasks they have traditionally had to per-
form, such as deallocating memory and hunting for memory leaks. This is a huge improvement
since hunting for memory leaks can be difficult and time-consuming.
ii. Interoperability - The .NET Framework was designed for interoperability between different .NET
languages, the operating system or Win32 DLLs, and COM. As such, a program written in one
45
.NET language can use and even inherit from a class written in another .NET language, as long
as certain rules are followed.
iii. No COM Required - The .NET Framework frees the programmer from the COM legacy. The
.Net developers allow to write code that interfaces with COM components. For example, there
are several new features that make that task easier.
iv. Simplified Deployment - Deploying programs written for the .NET Framework are be much eas-
ier than it was before. For example, .NET programs do not need to be registered with the
registry (at Operating System level). A program just needs to be copied to the target machine
and it is ready to run. The .NET offers a feature called side-by-side execution, which allows
different versions of a DLL to exist on the same machine. This means that every executable
can have access to the version of the DLL for which it was built.
v. Type Safety - The CLR checks and ensures the type safety of parameters and other data objects
- even between components written in different programming languages.
vi. The Base Class Library (BCL) - The .NET developers allow to use this extensive set of available
code when writing their own programs. The idea of the BCL is to provide developer with already
built functionality for most generalized tasks. Developer responsibility is limited to piecing to-
gether this functionality, and writing those specialized code is required for their application.
Suppose that there is a .NET Framework installed for a particular platform, that platform can run any
.NET program. The ability to run across multiple platforms is called platform independence. The .NET
Framework also provides a high level of language interoperability, i.e., program written in different
languages are all compiled into MSIL. In short, the .NET Framework offers programmers considerable
improvements over previous Windows programming environments.
This section and onwards explains and teaches you how to develop .NET application program in VB
programming with Microsoft Visual Studio.
3.6.3 Reading
To get more information about .Net, please read the following materials:
i. .Net Framework at URL https://en.wikipedia.org/wiki/.NET_Framework
ii. Visual Basic .Net at URL https://en.wikipedia.org/wiki/Visual_Basic_.NET
3.7 Programming activity - Starting Visual Studio
Let us differentiate between VB and Visual Studio before we start. The programming language you
will be learning is VB, but the development environment you will be using to create, run and debug VB
programs is called Microsoft Visual Studio Integrated Development Environment (IDE). Visual Studio
includes the C, C++, C++CLI, VB .NET, C#, F#, JavaScript, TypeScript, XML, XSLT, HTML, and CSS.
Support for other languages such as Python, Ruby, Node.js, and MUMPS (also known as M) among
others is available via plug-ins. Java (and J#) were supported in the past version [4]. The following
46
readings will provide you with an overview of the Visual Studio IDE and guide you in creating your first
application using it.
3.7.1 Activity
Answer the following questions.
1. The Solution Explorer window of Visual Studio is used to expose the object’s properties to the
programmer. True/False?
2. A(n) is an environment you need to create, run, and debug your programs.
(A) OOP
(B) IDE
(C) OOD
(D) CDE
3. The window of Visual Studio contains the tools, such as button and TextBox, that you can use
to create your application.
4. Briefly describe each of the following terms:
i. Toolbar
ii. Menu bar
iii. Toolbox
iv. Control
v. Form
vi. Solution
Activity - Suggested answers
1. False
2. B
3. Toolbox
4. Elaborations are not limited as follows.
i. Toolbar: A toolbar contains icons. When an icon is clicked, a command is executed.
ii. Menu bar: A menu bar contains menus, which are a group of related commands.
47
iii. Toolbox: The toolbox contains controls used to customise forms.
iv. Control: A control is a component, such as PictureBox or Label. Controls are added
to a form.
v. Form: It represents the Windows application that you are creating. The Form and controls
collectively represent the program’s GUI.
vi. Solution: A solution is a group of projects.
3.7.2 Hands-on: Hello World program
To begin your first coding in VB, you need an Integrated Development Environment (IDE), i.e. Visual
Studio, that supports the Microsoft .NET Framework. After installing the IDE, and do launch it. You
then need to create a new project, which will manage the VB source files and other resources.
Now we will walk through the following steps to create your first VB console program, as the video clip
at https://youtu.be/9k4MTV1OKlA
1. Create a new project of Windows Console App (.Net framework) in VB with the installed Visual
Studio.
2. You have now created a VB project. In the Solution Explorer pane (View, choose Solution Explorer),
you can see that the project consists of a single VB source file (.vb) that should already be opened. If
not, you can double-click on the file in the Solution Explorer in order to open it. In the source file, there
is some basic code to help you get started. However, to keep things simple at this stage, go ahead
and simplify the code into this.
3. In the source file, i.e. Module1.vb, locate the following lines of VB code.
1 Sub Main()
2 Console.Out.WriteLine("Hello World!")
3 End Sub
The Main method is the entry point of the program, and must have this format. VB program must
contain a Main method. It is where execution of the program begins.
The casing is also important since VB is case-sensitive. The curly brackets delimit what belongs to
a code entity, such as a class or method, and they must be included. The brackets, along with their
content, are referred to as code blocks, or just blocks.
4. Suppose that you did not install the Visual Studio, you could still compile the VB program as
long as you have the .NET Framework installed. To try this, open a console window (C:\Windows\
System32\cmd.exe) and navigate to the project folder where the source file is located. You then
need to find the VB compiler called vbc.exe, which is located in a path similar to the one shown here.
48
Run the compiler with the source file name as an argument and it will produce an executable in the
current folder.
1 C:\Users\choojun\source\repos\TCC123 -VB\101 HelloWorld
\Windows\Microsoft.NET\Framework64\v4.0.30319\ vbc.exe Module1.vb
Suppose that you try running the compiled program, it will show the same output as the one created
by Visual Studio.
1 C:\Users\choojun\source\repos\TCC123 -VB\101 HelloWorld Module1.exe
2 Hello World!
Congratulation! You have created the first Windows Console App in VB in this exercise.
3.7.3 Hands-on: TextBox control in Windows Forms App with .Net framework
In this exercise, you will learn to use the TextBox control to input information from user or display
information. Firstly, create a new project of Windows Forms App (.Net framework) in VB with the
installed Visual Studio.
Now we will walk through the following steps to create a TextBox control, as the video clip at
https://youtu.be/6Xu5tX2kCfc
1. Double click the TextBox icon in the Toolbox. A rectangular box appears at the upper left corner
of the form. Drag the TextBox to the center of the form.
2. Move the mouse arrow to the sizing handle in the center of the right side of the TextBox. The
cursor changes to a double arrow (←→). Hold down the mouse button and move it to the right. The
TextBox is stretched to the right. Repeat step 2 again to stretch the TextBox to the left. Resize it
about to 2 inches wide.
3. Click to select the TextBox, and press the <Delete> key to remove the TextBox.
4. Step 4 gives you another way to place a TextBox on a form. Click the TextBox in the ToolBox.
Hold and drag the mouse pointer to the form and release the mouse button to obtain a TextBox.
5. Press <F4> or go to View menu, select Properties Window, to activate the Properties window.
As in figure below, TextBox1 is the name of the text box. Use the up-arrow and down-arrow keys to
move through the list.
6. Move to the Text property; type the full name of WOU in the setting box, i.e. Wawasan Open
University.
7. Move to the ForeColor property. Click on the down arrow in the right part, then click Custom tab to
select green colour. Observe that your name has been changed to green colour.
8. Move to the Font property. Click on the ellipsis (. . . ) box to display a dialogue box as in figure
below. You can make any selection from the dialogue box.
49
9. Press <Crtl + F5> (start without debugging) or <F5> to start debugging the program or clicking
on the Start Debugging icon.
10. The string is highlighted. Move the cursor to the end of your surname and type your last name.
You will notice that the string will scroll to the left.
11. Press <Alt> + <F4> to end a program or click on the Close button (X).
12. Press <F4> to activate the Properties window. Click on TextBox and select ReadOnly property,
click on the down-arrow button and select True. You will notice that the background colour becomes
other colour.
13. Run again the program, and try typing into the TextBox. Note that you cannot type anything,
because we set ReadOnly = True.
14. Press <Alt> + <F4> to end the program.
15. Click on Save All icon to save the work.
Congratulation! You have created the first Windows Form App in VB in this exercise.
3.7.4 Hands-on: Button control in Windows Forms App with .Net framework
In this exercise, you will learn to use the Button control to perform an action when user clicked. In
GUI-based application, the OK and Cancel buttons are examples of buttons. We will walk through
the following steps to create a button control, as the video clip at https://youtu.be/
rXVPYyIvsK8
1. Create a new project of Windows Forms App (.Net framework) in VB with the installed Visual
Studio.
2. Double-click the Button icon in the Toolbox.
3. Move the Button to the centre of the form.
4. Press <F4> to activate Properties window. Select Text property and type ’Please Press Me’.
5. Click on the Button, and you can use the sizing handlers to make a button bigger.
6. There are many properties of a Button control. Try to play around with the BackgroundImage,
Font, Image, ImageAlign, Margin and TextAlign properties.
7. Run the program. Click on the Button, you will notice that the Button appears to move in and
then out. The output result is shown in figure below.
8. Press <Alt> + <F4> to end the program.
9. Press the <Delete> key on your keyboard to delete the Button.
10. Click Edit on the menu bar and then click Undo to reinstate the Button.
11. Click on Save All icon to save the work. Save the project name as ’U1 ButtonControl’.
50