How to pass an object to a procedure

Source: Internet
Author: User
Tags variables

In vb.net, the user can pass an object into the process. The following example establishes an object for a form class and passes it to a process. To use this example, the user can add a button named Button1 to the form and copy the following code into the Button1_Click event.

Protected Sub button1_click (Byval sender as System. Objec,_
                               Byval e as System.EventArgs)
  Dim frmform as New Form1 ()
  frmform.show () centerform (
  frmform) End
Sub
Sub Centerform (Byval theform as form)
   ' Centers the ' form on the screen.
   Dim Recform as Rectangle=screen.getbounds (theform)
   theform.left=cint ((recform.width-theform.width)/2)
   Theform.top=cint ((recform.height-theform.height)/2) End
Sub

Users can refer to an object as a parameter, and then, in the process, declare the parameter as a new object. The following example is the process of referencing an object to another form, as follows:
(1) Establish a project and confirm that there is a form named Form1;
(2) Adding another form to the project named Form2;
(3) Adding a PictureBox control to each of the two forms;
(4) Naming the PictureBox in the form Form1 as picturebox1;
(5) Naming the PictureBox in the form Form2 as Picturebox2;
(6) by clicking on the "image" property in the Picturebox2 Property window, assign a picture to the property, basically all the small pictures can be assigned to the "image" property, you can find the. bmp file in the Windows directory.
(7) Add the following code to the Form1 Form1_Click event:

Protected Sub Form1_Click (Byval sender as System.object,_
                               Byval e as System.EventArgs)
   Dim Frmform2 as New Form2 ()
   Frmform2. Getpicture (Picturebox1)
end Sub
(8) adds the following code to the Form2: public
Sub Getpicture (Byval x as PictureBox)
Assign the passed-in picturebox to a object variable.
   Objx=x
   ' Assign ' The "the" the "the" the "the" to Form1 PictureBox
   Objx.image=picturebox2.imgae end
Sub

After you run the program, click Form1, and the Form2 icon will appear on the form Form1. The Form1_Click procedure event invokes the Getpicture procedure of the Form2 form and passes an empty PictureBox object. Then, in the getpicture process of the Form2 form, the value of the PictureBox of the Form2 form is transferred to this empty Piturebox object, The image on the Form2 form appears in the PictureBox object in the Form1 form.

In general, these object variables (that is, variables declared as objects) can act as objects to many classes. Similarly, object variables declared by various forms and controls within Visual Basic will contain forms and controls within each category.
When users use these types of object variables again, they must take a specific approach based on different objects, for example, some objects may not support a particular property or method. Visual Basic provides two ways to find out which class an object variable belongs to: Keyword typeof and Function typename.
The keyword typeof can only be used in the If ... Then ... else expression, the user must include the name of the class directly in the code. For example:
If TypeOf mycontrol is CheckBox Then
The function typename is more flexible, the user can use it anywhere in the code, and because it returns the name of the class as a character, the user can get to which class it belongs by comparing the characters.

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.