Vb. NET learning Notes (variable lifetime and assignment)

Source: Internet
Author: User
Tags arrays expression variables
Notes | Variable variables have a life cycle
This, as programmers know, adds a cosmetic shared to the vb.net. The Shared keyword indicates that one or more declared programming elements will be shared. The point is that a shared element is not associated with a particular instance of a class or struct. You need to access them by qualifying the shared elements by using the class name or struct name or the variable name of a particular instance of the class or struct. A simple description is that a shared variable is serviced for the type itself rather than for a particular object.



Shared examples

Public Class Testa

Public Shared i as Int32 = 10

End Class





Public Class Testb

Inherits Testa ' Inheritance Testa

Public Sub New ()

TESTA.I = 100 ' Use the name of the type to access the shared variable

End Sub

End Class




Note that the relationship between shared variables and types and instances in vb.net does not appear to be strictly grammatical, and programmers can use instances of types to access and modify shared variables, but in C # The requirements are rigorous.







Assignment statement
Assignment statements in vb.net are divided into: simple assignment statements, compound assignment statements, delegate assignment statements, Mid assignment statements.

Simple assignment statement
The simple assignment statement and the previous VB assignment statement are basically not much different, but now vb.net allow the variable to be declared immediately after the assignment (see examples in front of accessibility examples). What we need to focus on is the Declaration and assignment method of the array.

In vb.net, you can declare an array by describing both the latitude and the upper bounds of the array, and the lower latitude of each array is 0 and cannot be changed. However, you can specify a "0-length array" by specifying the upper bound of the array. This array does not contain any elements.

If the length of the array is not displayed, you can immediately assign a value while declaring the array.



Examples of declarations and assignments for arrays

Public Class Testa

Dim Iarr () as Int32

Dim BARR (5) as Boolean ' 5 refers to the upper bound of the BARR subscript, which can have 6 elements

Dim Larr () as Long = New Long (3) {100, 200, 300, 400}

Dim CARR () as String = New String () {"A", "B", "C"}

Dim Darr ( -1) as Double

End Class




More features about arrays are described in detail in the later introduction of arrays.



Compound assignment
Now vb.net is starting to support compound assignment statements. Unlike a fully expanded expression, a variable on the left side of a compound assignment statement is evaluated only once. This means that at run time, the variable expression is evaluated before the expression on the right side of the assignment statement.



Composite Assignment Examples

Public Class Testa

Public Sub New ()

Dim I as Int32

i + 10

End Sub

End Class




Mid Assignment
Mid assignment is actually a processing assignment of a string. You can see how mid is assigned by using the following examples.



Mid example

Public Class Testa

Public Sub New ()

Dim Tmpstr as String

Tmpstr = "Hello vb.net" ' Hello vb.net

Mid (TMPSTR, 7, 2) = "C #" ' Hello c#.net

Mid (TMPSTR, 7) = "VB6" ' Hello vb6net

Mid (TMPSTR, 7) = "VB6 to vb.net" ' Hello VB6 to

Mid (TMPSTR, 7, 3) = "VB6 to vb.net" ' Hello VB6 to

End Sub

End Class






The keyword AddressOf for a delegate statement is described in detail in later delegates and events.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.