Use the New keyword

Source: Internet
Author: User

Using the new keyword to create a novel object is the same as defining the class that it belongs to. The new keyword can be used to create an instance of a form, a class module, and a collection. Each form created by the user at design time is a class. The new keyword can be used to create an instance of a class.
You can create an instance of a class by following these steps:
(1) Draw a button and several other controls on the form.
(2) The Click event procedure for the button adds the following code:
Dim x as New Form1
X.show
(3) Run the program, and click the button several times to remove the front form, because the form is a visual interface of the class, you can see a lot of forms of copies, each form has the same control, and appears in one place.
(4) Add the column code to the button's Click event procedure:
Dim F as Form1
F=new Form1
f.text= "Hello"
F.show
Use the new keyword to create a new collection and object from classes defined in the class module, and to see how they work, refer to the following example:
(1) Create a new project and draw a button control on a form named Form1.
(2) Under the Project menu, select Add Class to add a class to the project
(3) Name the new class as Showme.vb
(4) Add the following code to the new class:

Public Class ShowMe
  Sub showfrm ()
  Dim frmnew as Form1
  frmnew=new Form1 frmnew.show
  ()
  The frmnew.windowstate=1 end
  Sub
-End Class
(5) adds the following code during the Button1_Click event:
Protected Sub button1_click ( Byval sender as System.object,_
                                Byval as System.EventArgs)
   Dim clsnew as New ShowMe ()
   clsnew.showfrm () C13/>end Sub

Run the program and click a few times to see the icon for a minimized form on the desktop each time you create a new instance of the ShowMe class.
The new keyword can only be used to create objects of a class, and you cannot create an object based on a basic data type, such as Integer. Also, you cannot create an object that is based on a specific object. For example, the following code creates an object named OBJX based on a class named SomeClass, and then incorrectly attempts to create another object based on the object OBJX:
Dim OBJX as New SomeClass ()
Dim OBJX as New objx ()
The above code is illegal.

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.