VB.net learning notes (5) Data Type-reference type (class)

Source: Internet
Author: User

. NET provides thousands of classes. The object created by class is the reference type.

1. Object Class

An object is a base class of every type in. Net (including value type and reference type). In essence, each variable is an object and can be processed as an object.

Therefore, any variable can be converted to an object: reference type-maintenance reference and implementation code, but general type processing is performed; Value Type-is extracted from the stack and stored in the memory of the object.

The tostring () method created by the custom class also calls the customized tostring method when the instance is forcibly converted to The objec Type.

Example: Define an object

Dim A as object A = me ctype (A, form). Text = "the title of this form is displayed here" 'form type

2. string class

The unique reference type in the basic type, with a very shared processing method, will lead to many methods after adding a value after a string variable

(1) string () method

There are multiple constructors of the string class. String () is the default constructor.

The following are the methods for constructing strings:

Dim A as string = "ABC" dim B = "ABC" dim C as string = new string ("A" C, 23) 'Repeat 23 characters a dim d = new string ("+" C, 33)

There are multiple sharing methods for the stirng class, such:

Empty attribute, empty string, used for comparison or initialization;

Compare compares two strings

Compareordinal compares two strings (regardless of the local language or culture)

Concat string connection

Copy creates a new string (copy) using the instance)

Equals checks whether the two strings are the same

Isnullorempty efficiently determines whether the string is a null string or a nothing

(2) substring Method

Substring is a method for extracting substrings from a string. There are two methods, for example:

Dim A as string = new string ("Hello World") textbox1.text =. substring (0, 5) 'starts from the beginning of the string. The length is 5, that is, hello textbox1.text =. substring (6) 'position 6 start (W), end to the string

(3) padleft and padright

The left and right alignment strings. That is to say, which side is filled with characters to achieve the effect of left and right alignment, such:

(4) string. Split Method

Strings are separated by character characters (groups) into an array of substrings. For example:

A. Split ("a" C, "B" c) 'is separated by the separator A and B.

A. Split (d) 'is separated by array D.

Therefore, there are multiple separators at a single time.

(5) immutable string class

The string class is defined as an unchangeable class. If the string is changed or copied, the system allocates new memory space for storage (the original space is recycled by the recycle mechanism ).

Because every time a variable string is changed, it means that it will re-allocate the memory space for storage, and change the nest at any time like a shocking bird. Allocating space means spending more time.

For example, each string appended below is allocated, so it takes a long time.

To overcome this problem, you can use the system. Text. stringbuilder class in the. NET library, which is similar to allocating a larger memory first, despite

It is not fully occupied and provides space for the suffix-added and edited strings. This memory is greatly expanded only when it cannot be accommodated. This greatly reduces

The burden to be allocated.

For example, it can be seen that stringbuider takes little time

(6) string constants

The Microsoft. VisualBasic. contants class provides constants. For example, contants. vbcrlf can be used to easily process common constant strings.

3. xml literal

Just like the HTTP source code, in addition to the display string with nodes <>, the "included inside" string is also displayed.

In addition, some variable strings are embedded to display the variable string values, such as: <% = mystring %>

4. dbnull class and isdbnull () function

There are data types in the database, but it is null. To adapt to this, customize this to determine its value.

5. parameter transfer

In addition to value transfer and reference transfer, parameter transfer also has other features.

(1) default parameters.

Private function Add2 (byval A as int32, optional byval B as char = "D" C)

Optional indicates the default parameter. Optional. It can be omitted (its value is the default value ).

Default parameters can only be at the end of the parameter list

(2) paramarray parameter Array

Parameter array. One parameter can only pass one value. To pass multiple values without writing multiple parameters, paramarray is used to specify an array (multiple parameters)

Besides, paramarray has a wonderful usage. Although it is not an array, the function will treat multiple parameters as an Array Based on the passing parameters ".

Paramarray can only be the last parameter in the parameter list.

Note the following position with optional

Optional byval

Byval paramarray

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click        Dim a As Int32() = {1, 2, 3, 4, 5}        TextBox1.Text = add2(a)    End Sub    Private Function add2(ByVal ParamArray c() As Int32) As Int32        Dim i As Int32        Dim temp As Int32 = 0        For Each i In c            temp += i        Next        Return temp    End Function

The above can use Add2 (,), the function automatically treats these arrays as an array, rather than necessarily using array variables (in the preceding example,)

This is why the paramarray parameter can only be the last parameter (otherwise, it is easy to take other parameters as a member of the array parameter)

Paramarray cannot be used with optional.
Similarly, paramarray cannot be used with byref. But it can be used with byval. For example.
It is best not to modify the front of paramarray, so that the memory is good.

6. Scope of Variables

. Net has four scopes, which define the lifetime and priority of each variable.

When the variable is no longer in the scope, the garbage collector can recycle it.

Principle: A smaller scope has a higher priority. When two scopes overlap, a smaller scope is used first.

(1) global scope: the global scope has the lowest priority during the entire program running.

(2) category-level or module-level scope: If a class or module is deleted during its survival, the variable does not exist.

When it overlaps with the global scope, the global scope variables are temporarily "invisible", and the scope of the class variables takes effect.

(3) method scope: Sub, function, and other scopes (such as parameters .)

(4) block-level scope: declared in a program statement block. For example, the loop variable declared in the statement block of IF... then or for each fails when the loop block is generated.

Dim I as int32 'Global scope public class form1 dim I as int32 'class scope private sub button#click (sender as object, e as eventargs) handles button1.click dim A as int32 () = {1, 2, 3, 4, 5} textbox1.text = Add2 (a) end sub private function Add2 (byval paramarray C () as int32) as int32 'parameter, method scope dim temp as int32 = 0 for each I as int32 in c' I is block scope temp + = I next return temp end functionend class

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.