<< Chapter < Page Chapter >> Page >

Phạm vi biến cục bộ.

Phạm vi biến module.

Phạm vi biến toàn cục.

Biến toàn cục

  • Khái niệm: Biến toàn cục là biến có phạm vi hoạt động trong toàn bộ ứng dụng.
  • Khai báo:

Global<Tên biến>[As<Kiểu dữ liệu>]

Biến cục bộ

  • Khái niệm: Biến cục bộ là biến chỉ có hiệu lực trong những chương trình mà chúng được định nghĩa.
  • Khai báo:

Dim<Tên biến>[As<Kiểu dữ liệu>]

  • Lưu ý:

Biến cục bộ được định nghĩa bằng từ khóa Dim sẽ kết thúc ngay khi việc thi hành thủ tục kết thúc.

Biến module

  • Khái niệm: Biến Module là biến được định nghĩa trong phần khai báo (General|Declaration) của Module và mặc nhiên phạm vi hoạt động của nó là toàn bộ Module ấy.
  • Khai báo:

- Biến Module được khai báo bằng từ khóa Dim hay Private&đặt trong phần khai báo của Module.

Ví dụ:

Private Num As Integer

- Tuy nhiên, các biến Module này có thể được sử dụng bởi các chương trình con trong các Module khác. Muốn thế chúng phải được khai báo là Public trong phân Khai báo (General|Declaration) của Module.

Ví dụ:

Public Num As Integer

Lưu ý: Không thể khai báo biến với từ khóa là Public trong chương trình con.

Truyền tham số cho chương trình con

  • Khái niệm

Một chương trình con đôi lúc cần thêm một vài thông tin về trạng thái của đoạn mã lệnh mà nó định nghĩa để thực thi. Những thông tin này là các biến được truyền vào khi gọi chương trình con, các biến này gọi là tham số của chương trình con.

Có hai cách để truyền tham số cho chương trình con: Truyền bằng giá trị&truyền bằng địa chỉ.

  • Truyền tham số bằng giá trị

Với cách truyền tham số theo cách này, mỗi khi một tham số được truyền vào, một bản sao của biến đó được tạo ra. Nếu chương trình con có thay đổi giá trị, những thay đổi này chỉ tác động lên bản sao của biến. Trong VB, từ khóa ByVal được dùng để xác định tham số được truyền bằng giá trị.

Ví dụ:

Sub Twice (ByVal Num As Integer)

Num = Num * 2

Print Num

End Sub

Private Sub Form_Click()

Dim A As Integer

A = 4

Print A

Twice A

Print A

End Sub

Kết quả thực hiện của đoạn chương trình trên:

4

8

4

  • Truyền tham số bằng địa chỉ

Truyền tham số theo địa chỉ cho phép chương trình con truy cập vào giá trị gốc của biến trong bộ nhớ. Vì thế, giá trị của biến có thể sẽ bị thay đổi bởi đoạn mã lệnh trong chương trình con. Mặc nhiên, trong VB6 các tham số được truyền theo địa chỉ; tuy nhiên ta có thể chỉ định một cách tường minh nhờ vào từ khóa ByRef.

Ví dụ:

Sub Twice (Num As Integer)

Num = Num * 2

Print Num

End Sub

Private Sub Form_Click()

Dim A As Integer

A = 4

Print A

Twice A

Print A

End Sub

Kết quả thực hiện của đoạn chương trình trên:

4

8

8

Bẫy lỗi trong Visual Basic

Các thao tác bẫy các lỗi thực thi của chương trình là cần thiết đối với các ngôn ngữ lập trình. Người lập trình khó kiểm soát hết các tình huống có thể gây ra lỗi. Chẳng hạn người ta khó có thể kiểm tra chặt chẽ việc người dùng đang chép dữ liệu từ đĩa mềm (hay CD) khi chúng không có trong ổ đĩa. Nếu có các thao tác bẫy lỗi ở đây thì tiện cho người lập trình rất nhiều.

Visual Basic cũng cung cấp cho ta một số cấu trúc để bẫy các lỗi đang thực thi.

Cú pháp:

Dạng 1:

On Error GoTo<Tên nhãn>

<Các câu lệnh có thể gây ra lỗi>

<Tên nhãn>:

<Các câu lệnh xử lý lỗi>

Ý nghĩa:

-<Tên nhãn>: là một tên được đặt theo quy tắc của một danh biểu.

- Nếu một lệnh trong<Các câu lệnh có thể gây ra lỗi>thì khi chương trình thực thi đến câu lệnh đó, chương trình sẽ tự động nhảy đến đoạn chương trình định nghĩa bên dưới<Tên nhãn>để thực thi.

Dạng 2:

On Error Resume Next

<Các câu lệnh có thể gây ra lỗi>

Ý nghĩa:

- Nếu một lệnh trong<Các câu lệnh có thể gây ra lỗi>thì khi chương trình thực thi đến câu lệnh đó, chương trình sẽ tự động bỏ qua câu lệnh bị lỗi và thực thi câu lệnh kế tiếp.

Questions & Answers

how does Neisseria cause meningitis
Nyibol Reply
what is microbiologist
Muhammad Reply
what is errata
Muhammad
is the branch of biology that deals with the study of microorganisms.
Ntefuni Reply
What is microbiology
Mercy Reply
studies of microbes
Louisiaste
when we takee the specimen which lumbar,spin,
Ziyad Reply
How bacteria create energy to survive?
Muhamad Reply
Bacteria doesn't produce energy they are dependent upon their substrate in case of lack of nutrients they are able to make spores which helps them to sustain in harsh environments
_Adnan
But not all bacteria make spores, l mean Eukaryotic cells have Mitochondria which acts as powerhouse for them, since bacteria don't have it, what is the substitution for it?
Muhamad
they make spores
Louisiaste
what is sporadic nd endemic, epidemic
Aminu Reply
the significance of food webs for disease transmission
Abreham
food webs brings about an infection as an individual depends on number of diseased foods or carriers dully.
Mark
explain assimilatory nitrate reduction
Esinniobiwa Reply
Assimilatory nitrate reduction is a process that occurs in some microorganisms, such as bacteria and archaea, in which nitrate (NO3-) is reduced to nitrite (NO2-), and then further reduced to ammonia (NH3).
Elkana
This process is called assimilatory nitrate reduction because the nitrogen that is produced is incorporated in the cells of microorganisms where it can be used in the synthesis of amino acids and other nitrogen products
Elkana
Examples of thermophilic organisms
Shu Reply
Give Examples of thermophilic organisms
Shu
advantages of normal Flora to the host
Micheal Reply
Prevent foreign microbes to the host
Abubakar
they provide healthier benefits to their hosts
ayesha
They are friends to host only when Host immune system is strong and become enemies when the host immune system is weakened . very bad relationship!
Mark
what is cell
faisal Reply
cell is the smallest unit of life
Fauziya
cell is the smallest unit of life
Akanni
ok
Innocent
cell is the structural and functional unit of life
Hasan
is the fundamental units of Life
Musa
what are emergency diseases
Micheal Reply
There are nothing like emergency disease but there are some common medical emergency which can occur simultaneously like Bleeding,heart attack,Breathing difficulties,severe pain heart stock.Hope you will get my point .Have a nice day ❣️
_Adnan
define infection ,prevention and control
Innocent
I think infection prevention and control is the avoidance of all things we do that gives out break of infections and promotion of health practices that promote life
Lubega
Heyy Lubega hussein where are u from?
_Adnan
en français
Adama
which site have a normal flora
ESTHER Reply
Many sites of the body have it Skin Nasal cavity Oral cavity Gastro intestinal tract
Safaa
skin
Asiina
skin,Oral,Nasal,GIt
Sadik
How can Commensal can Bacteria change into pathogen?
Sadik
How can Commensal Bacteria change into pathogen?
Sadik
all
Tesfaye
by fussion
Asiina
what are the advantages of normal Flora to the host
Micheal
what are the ways of control and prevention of nosocomial infection in the hospital
Micheal
what is inflammation
Shelly Reply
part of a tissue or an organ being wounded or bruised.
Wilfred
what term is used to name and classify microorganisms?
Micheal Reply
Binomial nomenclature
adeolu
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Giáo trình visual basic. OpenStax CNX. Jul 29, 2009 Download for free at http://cnx.org/content/col10777/1.1
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Giáo trình visual basic' conversation and receive update notifications?

Ask