Declaration of VB.net object variables

Source: Internet
Author: User

A user can declare an object variable using a general description of the declaration, but you must declare the variable as object or the name of a specific class, using the following syntax to declare the object:
Dim variable as [New]{object|class}
Users can use protected,friend,private,shared or static to make declarations of objects. The descriptions of the following objects are valid:
Private Obja As Object ' Declare Obja as generic object data type.
Static objb as label ' Declare OBJB as label class type.
Dim OBJC as System.buffer ' Declare OBJC as Buffer class type.
Note: If you do not declare an object variable, the object's data type is the default object. However, this method of not declaring variables is not recommended for use.
Sometimes the type of an object is indeterminate before the procedure is run, in which case the type of the object variable can be declared to be the object data type. This can create a reference to any object.
However, if you know which class the object belongs to, it's best to declare it as an object of that class, as in those examples, if you already know that the object is an instance of class label, you should declare the variable as label.
The benefit of declaring an object as an instance of a particular class is as follows:
(1) dynamic Check type.
(2) Get Microsoft's IntelliSense support in the code.
(3) Increase readability
(4) Reduce the error rate of the code.
(5) The code runs more efficiently.
When declaring the type of an object, the type of declaration determines the flexibility and scope of application of the object variable. For example, if a user defines a form in an application named Form2, the user can declare an object variable as a Form2 object:
Dim MyForm as New Form2 ' Can refer only to an object of class Form2
The user can also declare this object variable as a normal form:
Dim AnyForm as Form ' Can refer to any form,but only a form
You can also declare this object variable as a normal control:
Dim Anycontrol as Control ' Can refer to any type

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.