首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

VB.Net - 基本语法

  • 打开文本编辑器,并添加上述代码。 Open a text editor and add the above mentioned code.
  • 将文件另存为helloworld.vb。 Save the file as helloworld.vb
  • 打开命令提示符工具并转到保存文件的目录。 Open the command prompt tool and go to the directory where you saved the file.
  • 类型VBC helloworld.vb,然后按回车编译代码。 Type vbc helloworld.vb and press enter to compile your code.
  • 如果在你的代码中没有错误命令提示符下会带你到下一行,并会产生HelloWorld.exe的可执行文件。 If there are no errors in your code the command prompt will take you to the next line and would generate helloworld.exe executable file.
  • 接下来,输入的HelloWorld来执行你的程序。 Next, type helloworld to execute your program.
  • 您将可以看到“Hello World”字样在屏幕上。 You will be able to see "Hello World" printed on the screen.

VB.Net是一种面向对象的编程语言。 在面向对象编程方法中,程序由通过动作相互交互的各种对象组成。 对象可能采取的动作称为方法。 相同类型的对象被认为具有相同的类型,或者更经常地被称为在同一类中。

当我们考虑VB.Net程序时,它可以定义为通过调用对方的方法进行通信的对象的集合。 现在让我们简单地看看类,对象,方法和实例变量是什么意思。

  • Object 对象 -对象具有状态和行为。 示例:狗有状态 - 颜色,名称,品种以及行为 - 摇摆,吠叫,吃饭等。对象是类的实例。Objects have states and behaviors. Example: A dog has states - color, name, breed as well as behaviors - wagging, barking, eating, etc. An object is an instance of a class.
  • Class 类 -类可以被定义为描述其类型的对象支持的行为/状态的模板/蓝图。A class can be defined as a template/blueprint that describes the behaviors/states that objects of its type support.
  • Methods 方法 -方法基本上是一种行为。 一个类可以包含许多方法。 它是在写逻辑,操纵数据和执行所有动作的方法中。A method is basically a behavior. A class can contain many methods. It is in methods where the logics are written, data is manipulated and all the actions are executed.
  • 实例变量 -每个对象都有其唯一的实例变量集。 对象的状态由分配给这些实例变量的值创建。Each object has its unique set of instance variables. An object's state is created by the values assigned to these instance variables.

VB.Net中的Rectangle类

例如,让我们考虑一个Rectangle对象。 它具有长度和宽度等属性。 根据设计,它可能需要接受这些属性的值,计算面积和显示细节的方式。

让我们看一个Rectangle类的实现,并在我们的观察的基础上讨论VB.Net基本语法:

代码语言:txt
复制
Imports System
Public Class Rectangle
    Private length As Double
    Private width As Double

    'Public methods
    Public Sub AcceptDetails()
        length = 4.5
        width = 3.5
    End Sub

    Public Function GetArea() As Double
        GetArea = length * width
    End Function
    Public Sub Display()
        Console.WriteLine("Length: {0}", length)
        Console.WriteLine("Width: {0}", width)
        Console.WriteLine("Area: {0}", GetArea())

    End Sub

    Shared Sub Main()
        Dim r As New Rectangle()
        r.Acceptdetails()
        r.Display()
        Console.ReadLine()
    End Sub
End Class

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

代码语言:txt
复制
Length: 4.5
Width: 3.5
Area: 15.75

在上一章中,我们创建了一个包含代码的Visual Basic模块。 Sub Main表示VB.Net程序的入口点。 这里,我们使用包含代码和数据的类。 您使用类来创建对象。 例如,在代码中,r是一个Rectangle对象。

对象是类的一个实例:

代码语言:txt
复制
Dim r As New Rectangle()

类可以具有可以从外部类访问的成员,如果指定的话。 数据成员称为字段,过程成员称为方法。

可以在不创建类的对象的情况下调用共享方法或静态方法。 通过类的一个对象调用实例方法:

代码语言:txt
复制
Shared Sub Main()
   Dim r As New Rectangle()
   r.Acceptdetails()
   r.Display()
   Console.ReadLine()
End Sub

标识符

标识符是用于标识类,变量,函数或任何其他用户定义项的名称。 在VB.Net中命名类的基本规则如下:

  • 名称必须以字母开头,后跟一个字母,数字(0 - 9)或下划线。 标识符中的第一个字符不能是数字。A name must begin with a letter that could be followed by a sequence of letters, digits (0 - 9) or underscore. The first character in an identifier cannot be a digit.
  • 它不能包含任何嵌入的空格或符号是怎样的?  - +! @#%^&*()[] {}。 ; :“'/和\。但是,可以使用下划线(_)。It must not contain any embedded space or symbol like ? - +! @ # % ^ & * ( ) { } . ; : " ' / and . However, an underscore ( _ ) can be used.
  • 它不应该是保留关键字。It should not be a reserved keyword.

VB.Net关键字

下表列出了VB.Net保留的关键字:

AddHandler

AddressOf

Alias

And

AndAlso

As

Boolean

ByRef

Byte

ByVal

Call

Case

Catch

CBool

CByte

CChar

CDate

CDec

CDbl

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

End If

Enum

Erase

Error

Event

Exit

False

Finally

For

Friend

Function

Get

GetType

GetXMLNamespace

Global

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

While

Widening

With

WithEvents

WriteOnly

Xor

扫码关注腾讯云开发者

领取腾讯云代金券