<< Chapter < Page Chapter >> Page >

Hằng số

Khái niệm

Hằng số (Constant) là giá trị dữ liệu không thay đổi.

Khai báo hằng

[Public|Private] Const<tên hằng>[As<kiểu dữ liệu>] =<biểu thức>

Trong đó, tên hằng được đặt giống theo quy tắc đặt tên của điều khiển.

Ví dụ:

Const g = 9.8

Const Num As Integer = 4*5

Ta có thể dùng cửa sổ Object Browser để xem danh sách các hằng có sẵn của VB và VBA (Visual Basic for Application).

Trường hợp trùng tên hằng trong những thư viện khác nhau, ta có thể chỉ rõ tham chiếu hằng.

[<Libname>.][<tên mô-đun>.]<tên hằng>

Biến

Khái niệm

Biến (Variable) là vùng lưu trữ được đặt tên để chứa dữ liệu tạm thời trong quá trình tính toán, so sánh và các công việc khác.

Biến có 2 đăc điểm:

  • Mỗi biến có một tên.
  • Mỗi biến có thể chứa duy nhất một loại dữ liệu.

Khai báo

[Public|Private|Static|Dim]<tên biến>[ As<kiểu dữ liệu>]

Trong đó, tên biến: là một tên được đặt giống quy tắc đặt tên điều khiển. Nếu cần khai báo nhiều biến trên một dòng thì mỗi khai báo cách nhau dấu phẩy (,).

Nếu khai báo biến không xác định kiểu dữ liệu thì biến đó có kiểu Variant.

Khai báo ngầm: Đây là hình thức không cần phải khai báo một biến trước khi sử dụng. Cách dùng này có vẻ thuận tiện nhưng sẽ gây một số sai sót, chẳng hạn khi ta đánh nhầm tên biến, VB sẽ hiểu đó là một biến mới dẫn đến kết quả chương trình sai mà rất khó phát hiện.

Ví dụ:

Dim Num As Long, a As Single

Dim Age As Integer

Khai báo tường minh: Để tránh rắc rối như đã nêu ở trên, ta nên quy định rằng VB sẽ báo lỗi khi gặp biến chưa được khai báo bằng dòng lệnh:

Option Explicit trong phần Declaration (khai báo) của mô-đun.

Option Explicit chỉ có tác dụng trên từng mô-đun do đó ta phải đặt dòng lệnh này trong từng mô-đun của biểu mẫu, mô-đun lớp hay mô-đun chuẩn.

Biểu thức

Khái niệm

Toán tử hay phép toán (Operator): là từ hay ký hiệu nhằm thực hiện phép tính và xử lý dữ liệu.

Toán hạng: là giá trị dữ liệu (biến, hằng…).

Biểu thức: là tập hợp các toán hạng và các toán tử kết hợp lại với nhau theo quy tắc nhất định để tính toán ra một giá trị nào đó.

Các loại phép toán

  1. Các phép toán số học: Thao tác trên các giá trị có kiểu dữ liệu số.
Phép toán Ý nghĩa Kiểu của đối số Kiểu của kết quả
- Phép lấy số đối Kiểu số (Integer, Single…) Như kiểu đối số
+ Phép cộng hai số Kiểu số (Integer, Single…) Như kiểu đối số
- Phép trừ hai số Kiểu số (Integer, Single…) Như kiểu đối số
* Phép nhân hai số Kiểu số (Integer, Single…) Như kiểu đối số
/ Phép chia hai số Kiểu số (Integer, Single…) Single hay Double
\ Phép chia lấy phần nguyên Integer, Long Integer, Long
Mod Phép chia lấy phần dư Integer, Long Integer, Long
^ Tính lũy thừa Kiểu số (Integer, Single…) Như kiểu đối số
  1. Các phép toán quan hệ

Đây là các phép toán mà giá trị trả về của chúng là một giá trị kiểu Boolean (TRUE hay FALSE).

Phép toán Ý nghĩa
= So sánh bằng nhau
<> So sánh khác nhau
> So sánh lớn hơn
< So sánh nhỏ hơn
>= So sánh lớn hơn hoặc bằng
<= So sánh nhỏ hơn hoặc bằng
  1. Các phép toán Logic: là các phép toán tác động trên kiểu Boolean và cho kết quả là kiểu Boolean. Các phép toán này bao gồm AND (và), OR (hoặc), NOT (phủ định). Sau đây là bảng giá trị của các phép toán:

Questions & Answers

Discuss the differences between taste and flavor, including how other sensory inputs contribute to our  perception of flavor.
John Reply
taste refers to your understanding of the flavor . while flavor one The other hand is refers to sort of just a blend things.
Faith
While taste primarily relies on our taste buds, flavor involves a complex interplay between taste and aroma
Kamara
which drugs can we use for ulcers
Ummi Reply
omeprazole
Kamara
what
Renee
what is this
Renee
is a drug
Kamara
of anti-ulcer
Kamara
Omeprazole Cimetidine / Tagament For the complicated once ulcer - kit
Patrick
what is the function of lymphatic system
Nency Reply
Not really sure
Eli
to drain extracellular fluid all over the body.
asegid
The lymphatic system plays several crucial roles in the human body, functioning as a key component of the immune system and contributing to the maintenance of fluid balance. Its main functions include: 1. Immune Response: The lymphatic system produces and transports lymphocytes, which are a type of
asegid
to transport fluids fats proteins and lymphocytes to the blood stream as lymph
Adama
what is anatomy
Oyindarmola Reply
Anatomy is the identification and description of the structures of living things
Kamara
what's the difference between anatomy and physiology
Oyerinde Reply
Anatomy is the study of the structure of the body, while physiology is the study of the function of the body. Anatomy looks at the body's organs and systems, while physiology looks at how those organs and systems work together to keep the body functioning.
AI-Robot
what is enzymes all about?
Mohammed Reply
Enzymes are proteins that help speed up chemical reactions in our bodies. Enzymes are essential for digestion, liver function and much more. Too much or too little of a certain enzyme can cause health problems
Kamara
yes
Prince
how does the stomach protect itself from the damaging effects of HCl
Wulku Reply
little girl okay how does the stomach protect itself from the damaging effect of HCL
Wulku
it is because of the enzyme that the stomach produce that help the stomach from the damaging effect of HCL
Kamara
function of digestive system
Ali Reply
function of digestive
Ali
the diagram of the lungs
Adaeze Reply
what is the normal body temperature
Diya Reply
37 degrees selcius
Xolo
37°c
Stephanie
please why 37 degree selcius normal temperature
Mark
36.5
Simon
37°c
Iyogho
the normal temperature is 37°c or 98.6 °Fahrenheit is important for maintaining the homeostasis in the body the body regular this temperature through the process called thermoregulation which involves brain skin muscle and other organ working together to maintain stable internal temperature
Stephanie
37A c
Wulku
what is anaemia
Diya Reply
anaemia is the decrease in RBC count hemoglobin count and PVC count
Eniola
what is the pH of the vagina
Diya Reply
how does Lysin attack pathogens
Diya
acid
Mary
I information on anatomy position and digestive system and there enzyme
Elisha Reply
anatomy of the female external genitalia
Muhammad Reply
Organ Systems Of The Human Body (Continued) Organ Systems Of The Human Body (Continued)
Theophilus Reply
what's lochia albra
Kizito
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