课程表

VB.Net基本教程

VB.Net高级教程

工具箱
速查手册

VB.Net - 声明

当前位置:免费教程 » 程序设计 » VB.Net
statement 声明是Visual Basic程序中的完整指令。 它可以包含关键字,运算符,变量,字面值,常量和表达式。

语句可以分类为:

1、Declaration statements 声明语句 - 这些语句是您命名变量,常量或过程的语句,也可以指定数据类型。


2、Executable statements 可执行语句 - 这些是启动动作的语句。 这些语句可以调用方法或函数,通过代码块循环或分支,或者将值或表达式赋值给变量或常量。 在最后一种情况下,它被称为Assignment语句。

声明语句

声明语句用于命名定义过程 变量 属性 数组常量声明编程元素可以定义数据类型 访问级别范围

可以声明编程元素包括变量 常数 枚举  结构模块 接口 程序 过程参数 函数返回值 外部过程引用 运算符 属性 事件委托

以下在 VB.Net 声明语句
S.N声明和说明示例
1

Dim Statement

Dim语句

Declares and allocates storage space for one or more variables.

声明和分配一个或多个变量的存储空间。

  1. Dim number As Integer
  2. Dim quantity As Integer = 100
  3. Dim message As String = "Hello!"
2Const Statement 
Const语句

Declares and defines one or more constants.
声明和定义一个或多个常量。
  1. Const maximum As Long = 1000
  2. Const naturalLogBase As Object
  3. = CDec(2.7182818284)
3Enum Statement 
枚举语句

Declares an enumeration and defines the values of its members.
声明一个枚举并定义其成员的值。
  1. Enum CoffeeMugSize
  2. Jumbo
  3. ExtraLarge
  4. Large
  5. Medium
  6. Small
  7. End Enum
4

Class Statement

类语句

Declares the name of a class and introduces the definition of the variables, properties, events, and procedures that the class comprises.

声明类的名称,并引入该类包含的变量,属性,事件和过程的定义。

  1. Class Box
  2. Public length As Double
  3. Public breadth As Double
  4. Public height As Double
  5. End Class
5

Structure Statement

结构声明

Declares the name of a structure and introduces the definition of the variables, properties, events, and procedures that the structure comprises.

声明结构的名称,并引入结构所包含的变量,属性,事件和过程的定义。

  1. Structure Box
  2. Public length As Double
  3. Public breadth As Double
  4. Public height As Double
  5. End Structure
6

Module Statement

模块语句

Declares the name of a module and introduces the definition of the variables, properties, events, and procedures that the module comprises.

声明模块的名称,并介绍模块包含的变量,属性,事件和过程的定义。

  1. Public Module myModule
  2. Sub Main()
  3. Dim user As String =
  4. InputBox("What is your name?")
  5. MsgBox("User name is" & user)
  6. End Sub
  7. End Module
7

Interface Statement

接口语句
Declares the name of an interface and introduces the definitions of the members that the interface comprises.
声明接口的名称,并介绍接口包含的成员的定义。

  1. Public Interface MyInterface
  2. Sub doSomething()
  3. End Interface
8

Function Statement

函数语句

Declares the name, parameters, and code that define a Function procedure.

声明定义函数过程的名称,参数和代码。

  1. Function myFunction
  2. (ByVal n As Integer) As Double
  3. Return 5.87 * n
  4. End Function
9

Sub Statement

子语句

Declares the name, parameters, and code that define a Sub procedure.

声明定义Sub过程的名称,参数和代码。

  1. Sub mySub(ByVal s As String)
  2. Return
  3. End Sub
10

Declare Statement

声明语句

Declares a reference to a procedure implemented in an external file.

声明对在外部文件中实现的过程的引用。

  1. Declare Function getUserName
  2. Lib "advapi32.dll"
  3. Alias "GetUserNameA"
  4. (
  5. ByVal lpBuffer As String,
  6. ByRef nSize As Integer) As Integer
11

Operator Statement

运算符声明

Declares the operator symbol, operands, and code that define an operator procedure on a class or structure.

声明运算符符号 操作数结构定义一个运算符过程代码

  1. Public Shared Operator +
  2. (ByVal x As obj, ByVal y As obj) As obj
  3. Dim r As New obj
  4. ' implemention code for r = x + y
  5. Return r
  6. End Operator
12

Property Statement

属性声明

Declares the name of a property, and the property procedures used to store and retrieve the value of the property.

声明属性的名称,以及用于存储和检索属性值的属性过程。

  1. ReadOnly Property quote() As String
  2. Get
  3. Return quoteString
  4. End Get
  5. End Property
13

Event Statement

事件声明

Declares a user-defined event.

声明用户定义的事件。

  1. Public Event Finished()
14

Delegate Statement

委托声明

Used to declare a delegate.

用于声明一个委托。

  1. Delegate Function MathOperator(
  2. ByVal x As Double,
  3. ByVal y As Double
  4. ) As Double

可执行语句

可执行语句执行操作。 调用过程,分支到代码中的另一个地方,循环使用几个语句或评估表达式的语句是可执行语句。 赋值语句是可执行语句的一种特殊情况。


以下示例演示了一个决策语句:

  1. Module decisions
  2. Sub Main()
  3. 'local variable definition '
  4. Dim a As Integer = 10
  5.  
  6. ' check the boolean condition using if statement '
  7. If (a < 20) Then
  8. ' if condition is true then print the following '
  9. Console.WriteLine("a is less than 20")
  10. End If
  11. Console.WriteLine("value of a is : {0}", a)
  12. Console.ReadLine()
  13. End Sub
  14. End Module


当上述代码被编译和执行时,它产生了以下结果:

  1. a is less than 20;
  2. value of a is : 10
转载本站内容时,请务必注明来自W3xue,违者必究。
 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号