Visual Basic基础语法.docx

上传人:b****7 文档编号:9633379 上传时间:2023-02-05 格式:DOCX 页数:16 大小:22.84KB
下载 相关 举报
Visual Basic基础语法.docx_第1页
第1页 / 共16页
Visual Basic基础语法.docx_第2页
第2页 / 共16页
Visual Basic基础语法.docx_第3页
第3页 / 共16页
Visual Basic基础语法.docx_第4页
第4页 / 共16页
Visual Basic基础语法.docx_第5页
第5页 / 共16页
点击查看更多>>
下载资源
资源描述

Visual Basic基础语法.docx

《Visual Basic基础语法.docx》由会员分享,可在线阅读,更多相关《Visual Basic基础语法.docx(16页珍藏版)》请在冰豆网上搜索。

Visual Basic基础语法.docx

VisualBasic基础语法

VisualBasic程序的编译首先涉及到将原始的Unicode字符流转换为有序的词法标记集。

因为VisualBasic语言不是自由格式语言,标记集随后进一步划分为一系列逻辑行。

一个逻辑行的跨度为从流的开头或一个行终止符到前面没有行继续符的下一个行终止符或到流的末尾。

注意随着在该语言9.0版本中引入XML文本表达式,VisualBasic不再有独特的词法文法,也就是说,可在不考虑句法上下文的情况下对VisualBasic代码进行标记化。

这是因为XML和VisualBasic有不同的词法规则,并且任意特定时间使用哪组词法规则取决于当时所处理的句法构造。

本规范保留此词法文法节作为常规VisualBasic代码的词法规则指南。

但是,就长期计划来说,词法规则将并入句法规则。

Start:

:

=[LogicalLine+]

LogicalLine:

:

=[LogicalLineElement+][Comment]LineTerminator

LogicalLineElement:

:

=WhiteSpace|LineContinuation|Token

Token:

:

=Identifier|Keyword|Literal|Separator|Operator

1.1字符和行

VisualBasic程序由来自Unicode字符集的字符组成。

Character:

:

=

行终止符

Unicode换行符字符划分逻辑行。

LineTerminator:

:

=

|

|

|

|

行继续符

行继续符由一个空白字符后跟作为文本行中最后一个字符(空白除外)的下划线字符组成。

行继续符使得一个逻辑行可以跨越多个物理行。

虽然行继续符不是空白,但它们被视为空白。

下面的程序显示一些行继续符:

ModuleTest

SubPrint(_

Param1AsInteger,_

Param2AsInteger)

If(Param1

(Param1>Param2)Then

Console.WriteLine("Notequal")

EndIf

EndFunction

EndModule

句法文法的某些位置允许使用隐式行继续符。

在以下位置遇到行终止符时:

逗号(,)、左括号(()、左大括号({)或左嵌入式表达式(<%=)之后

成员限定符(.或.@或...)之后,如果限定了某些内容(即不使用隐式With上下文)

右括号())、右大括号(})或右嵌入式表达式(%>)之前

特性上下文中小于号(<)之后

特性上下文中大于号(>)之前

非文件级别特性上下文中大于号(>)之后

查询运算符(Where、Order、Select等)之前或之后

表达式上下文中二元运算符(+、-、/、*等)之后

任意上下文中赋值运算符(=、:

=、+=、-=等)之后。

行终止符视为行继续符。

例如,上例也可以如下编写:

ModuleTest

SubPrint(

Param1AsInteger,

Param2AsInteger)

If(Param1

(Param1>Param2)Then

Console.WriteLine("Notequal")

EndIf

EndFunction

EndModule

只有在紧挨指定标记的之前或之后才能推断隐式行继续符。

在行继续符之前或之后不能推断为隐式行继续符。

例如:

Dimy=10

'Error:

Expressionexpectedforassignmenttox

Dimx=_

y

在条件编译上下文中将不能推断行继续符。

批注

最后这一条约束是必需的,因为不编译的条件编译块中的文本不必在句法上有效。

因此,此类块中的文本可能会被条件编译语句意外“选中”,特别是将来在扩展该语言时。

LineContinuation:

:

=WhiteSpace_[WhiteSpace+]LineTerminator

Comma:

:

=,[LineTerminator]

Period:

:

=.[LineTerminator]

OpenParenthesis:

:

=([LineTerminator]

CloseParenthesis:

:

=[LineTerminator])

OpenCurlyBrace:

:

={[LineTerminator]

CloseCurlyBrace:

:

=[LineTerminator]}

Equals:

:

==[LineTerminator]

ColonEquals:

:

=:

=[LineTerminator]

空白

空白只是用来分隔标记,其他情况下将被忽略。

只包含空白的逻辑行将被忽略。

注意   行终止符不视为空白。

WhiteSpace:

:

=

|

注释

注释以单引号字符或关键字REM开始。

单引号字符是ASCII单引号字符、Unicode左单引号字符或Unicode右单引号字符。

注释可以从源行中的任意位置开始,物理行结尾结束注释。

编译器会忽略注释开始和行终止符之间的字符。

因此,不能使用行继续符将注释扩展到多行。

Comment:

:

=CommentMarker[Character+]

CommentMarker:

:

=SingleQuoteCharacter|REM

SingleQuoteCharacter:

:

=

'|

|

1.2标识符

标识符是名称。

VisualBasic标识符符合Unicode标准附录15,但有一个例外:

标识符可以以下划线(连接线)字符开始。

如果标识符以下划线开始,它必须包含至少一个其他有效标识符字符来与行继续符区分。

规则标识符不能与关键字冲突,但转义标识符或具有类型字符的标识符可以。

转义标识符是以方括号分隔的标识符。

转义标识符遵循与规则标识符相同的规则,但转义标识符可以与关键字冲突并且可以没有类型字符。

此示例定义一个名为class的类,该类包含一个名为shared的共享方法,该方法使用名为boolean的参数,然后调用该方法。

Class[class]

SharedSub[shared]([boolean]AsBoolean)

If[boolean]Then

Console.WriteLine("true")

Else

Console.WriteLine("false")

EndIf

EndSub

EndClass

Module[module]

SubMain()

[class].[shared](True)

EndSub

EndModule

标识符不区分大小写,因此只有大小写不同的两个标识符将被认为是同一个标识符。

注意   当比较标识符时,将使用Unicode标准一对一大小写映射,并将忽略任何区域设置特定的大小写映射。

Identifier:

:

=

NonEscapedIdentifier[TypeCharacter]|

KeywordTypeCharacter|

EscapedIdentifier

NonEscapedIdentifier:

:

=

EscapedIdentifier:

:

=[IdentifierName]

IdentifierName:

:

=IdentifierStart[IdentifierCharacter+]

IdentifierStart:

:

=

AlphaCharacter|

UnderscoreCharacterIdentifierCharacter

IdentifierCharacter:

:

=

UnderscoreCharacter|

AlphaCharacter|

NumericCharacter|

CombiningCharacter|

FormattingCharacter

AlphaCharacter:

:

=

NumericCharacter:

:

=

CombiningCharacter:

:

=

FormattingCharacter:

:

=

UnderscoreCharacter:

:

=

IdentifierOrKeyword:

:

=Identifier|Keyword

类型字符

类型字符表示前面标识符的类型。

类型字符不视为标识符的一部分。

如果声明包含类型字符,类型字符必须符合声明本身中指定的类型;否则,将引发编译时错误。

如果声明省略类型(例如,如果未指定As子句),则隐式使用类型字符作为声明的类型。

标识符及其类型字符之间不能有任何空白。

因为没有合适的字符,所以Byte、SByte、UShort、Short、UInteger或ULong没有对应的类型字符。

向概念上没有类型的标识符(例如,命名空间名称)或类型与类型字符的类型不匹配的标识符添加类型字符会导致编译时错误。

下例显示类型字符的用法:

'Thefollowlinewillcauseanerror:

standardmoduleshavenotype.

ModuleTest1#

EndModule

ModuleTest2

'ThisfunctiontakesaLongparameterandreturnsaString.

FunctionFunc$(Param&)

'Thefollowinglinecausesanerrorbecausethetypecharacter

'conflictswiththedeclaredtypeofFuncandParam.

Func#=CStr(Param@)

'Thefollowinglineisvalid.

Func$=CStr(Param&)

EndFunction

EndModule

类型字符!

表示存在一个特殊问题,在该语言中,它既可用作类型字符,也可用作分隔符。

为消除多义性,如果!

字符后的字符不能开始一个标识符,那它就是类型字符。

如果能,则!

字符就是分隔符,而不是类型字符。

TypeCharacter:

:

=

IntegerTypeCharacter|

LongTypeCharacter|

DecimalTypeCharacter|

SingleTypeCharacter|

DoubleTypeCharacter|

StringTypeCharacter

IntegerTypeCharacter:

:

=%

LongTypeCharacter:

:

=&

DecimalTypeCharacter:

:

=@

SingleTypeCharacter:

:

=!

DoubleTypeCharacter:

:

=#

StringTypeCharacter:

:

=$

1.3关键字

关键字是在语言构造中具有特殊含义的词。

所有关键字都由语言保留,不能用作标识符(转义标识符除外)。

注意   虽然EndIf、GoSub、Let、Variant和Wend在VisualBasic中不再使用,但仍保留为关键字。

Keyword:

:

=

AddHandler

AddressOf

Alias

And

AndAlso

As

Boolean

ByRef

Byte

ByVal

Call

Case

Catch

CBool

CByte

CChar

CDate

CDbl

CDec

Char

CInt

Class

CLng

CObj

Const

Continue

CSByte

CShort

CSng

CStr

CType

CUInt

CULng

CUShort

Date

Decimal

Declare

Default

Delegate

Dim

DirectCast

Do

Double

Each

Else

ElseIf

End

EndIf

Enum

Erase

Error

Event

Exit

False

Finally

For

Friend

Function

Get

GetType

GetXmlNamespace

Global

GoSub

GoTo

Handles

If

Implements

Imports

In

Inherits

Integer

Interface

Is

IsNot

Let

Lib

Like

Long

Loop

Me

Mod

Module

MustInherit

MustOverride

MyBase

MyClass

Namespace

Narrowing

New

Next

Not

Nothing

NotInheritable

NotOverridable

Object

Of

On

Operator

Option

Optional

Or

OrElse

Overloads

Overridable

Overrides

ParamArray

Partial

Private

Property

Protected

Public

RaiseEvent

ReadOnly

ReDim

REM

RemoveHandler

Resume

Return

SByte

Select

Set

Shadows

Shared

Short

Single

Static

Step

Stop

String

Structure

Sub

SyncLock

Then

Throw

To

True

Try

TryCast

TypeOf

UInteger

ULong

UShort

Using

Variant

Wend

When

While

Widening

With

WithEvents

WriteOnly

Xor

1.4文本

文本是某一类型的特定值的文本表示。

文本类型包括布尔值、整数、浮点数、字符串、字符和日期。

Literal:

:

=

BooleanLiteral|

IntegerLiteral|

FloatingPointLiteral|

StringLiteral|

CharacterLiteral|

DateLiteral|

Nothing

Boolean文本

True和False是Boolean类型的文本,分别对应true和false状态。

BooleanLiteral:

:

=True|False

Integer文本

Integer文本可以是十进制(基数为10)、十六进制(基数为16)或八进制(基数为8)。

十进制Integer文本是十进制数字(0-9)字符串。

十六进制文本是&H后跟十六进制数字(0-9、A-F)字符串。

八进制文本是&O后跟八进制数字(0-7)字符串。

十进制文本直接表示Integer文本的十进制值,而八进制和十六进制文本表示Integer文本的二进制值(因此,&H8000S是–32768,而不是溢出错误)。

文本的类型由其值或后跟的类型字符确定。

如果未指定类型字符,则Integer类型范围内的值类型化为Integer;Integer范围外的值类型化为Long。

如果某一整数文本的类型大小不足以容纳整数文本,则会产生编译时错误。

批注

没有针对Byte的类型字符,因为最适合的字符是B,而B是十六进制文本中的合法字符。

IntegerLiteral:

:

=IntegralLiteralValue[IntegralTypeCharacter]

IntegralLiteralValue:

:

=IntLiteral|HexLiteral|OctalLiteral

IntegralTypeCharacter:

:

=

ShortCharacter|

UnsignedShortCharacter|

IntegerCharacter|

UnsignedIntegerCharacter|

LongCharacter|

UnsignedLongCharacter|

IntegerTypeCharacter|

LongTypeCharacter

ShortCharacter:

:

=S

UnsignedShortCharacter:

:

=US

IntegerCharacter:

:

=I

UnsignedIntegerCharacter:

:

=UI

LongCharacter:

:

=L

UnsignedLongCharacter:

:

=UL

IntLiteral:

:

=Digit+

HexLiteral:

:

=&HHexDigit+

OctalLiteral:

:

=&OOctalDigit+

Digit:

:

=0|1|2|3|4|5|6|7|8|9

HexDigit:

:

=0|1|2|3|4|5|6|7|8|9|A|B|C|D|E|F

OctalDigit:

:

=0|1|2|3|4|5|6|7

Floating-Point文本

Floating-Point文本是一个Integer文本后跟一个可选的小数点(ASCII句点字符)和尾数,以及一个可选的以10为基数的指数。

默认情况下,Floating-Point文本为Double类型。

如果指定了Single、Double或Decimal类型字符,则文本为该指定类型。

如果某一Floating-Point文本的类型大小不足以容纳Floating-Point文本,就会产生编译时错误。

注释:

值得注意的是,Decimal数据类型可将尾随零编码到值中。

当前,规范未对编译器是否识别Decimal文本中的尾随零提供任何说明。

FloatingPointLiteral:

:

=

FloatingPointLiteralValue[FloatingPointTypeCharacter]|

IntLiteralFloatingPointTypeCharacter

FloatingPointTypeCharacter:

:

=

SingleCharacter|

DoubleCharacter|

DecimalCharacter|

SingleTypeCharacter|

DoubleTypeCharacter|

DecimalTypeCharacter

SingleCharacter:

:

=F

DoubleCharacter:

:

=R

DecimalCharacter:

:

=D

FloatingPointLiteralValue:

:

=

IntLiteral.IntLiteral[Exponent]|

.IntLiteral[Exponent]|

IntLiteralExponent

Exponent:

:

=E[Sign]IntLiteral

Sign:

:

=+|-

字符串文本

字符串文本是以ASCII双引号字符、Unicode左双引号字符或Unicode右双引号字符开始并结束的零个或零个以上Unicode字符序列。

在字符串中,两个双引号字符组成的序列是转义序列,表示字符串中的双引号。

字符串常量属于String类型。

ModuleTest

SubMain()

'Thisprintsout:

".

Console.WriteLine("""")

'Thisprintsout:

a"b.

Console.WriteLine("a""b")

'Thiscausesacompileerrorduetomismatcheddouble-quotes.

Console.WriteLine("a"b")

EndSub

EndModule

允许使用编译器用字符串文本替换常量字符串表达式。

每个字符串文本不一定产生新的字符串实例。

当根据使用二进制比较语义的字符串相等运算符确认为相等的两个或更多个字符串出现在同一个程序中时,这些字符串可能引用相同的字符串实例。

例如,下面程序的输出可能返回True,因为两个文本可引用同一字符串实例。

ModuleTest

SubMain()

DimaAsObject="he"&"llo"

DimbAsObject="hello"

Console.WriteLine(aIsb)

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 党团工作 > 入党转正申请

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1