Home > Software engineering >  Excuse me: how to create a class instance sun? Thank you very much!
Excuse me: how to create a class instance sun? Thank you very much!

Time:09-21

Excuse me: how to create a class instance sun? Thank you very much!
Class module 1: parent s parent collection
Class module 2: the parent class parent
Class module 3: subclass s subclass collection
Class module 4: subclass subclass
Class module 5: sun s sun class collection
Class module 6: sun sun class

I in the process of parent s Add:
Public Fucntion Add
Dim the son As a subclass
Set the son=subclass

'I want to create a subclass instance, the default in a subclass of the newly created instance to create an instance of the sun, how should do? Namely how to achieve the set=sun. Sun class s.a dd?
End Fucntion

I am an amateur, has first dizzy, don't know what to say no, thank you first!

CodePudding user response:

The default constructor?

Create objects


Is the simplest way to create objects is in the "toolbox" double-click controls, however, all available in Visual Basic object and the object of all the available from other applications, to fully embody their advantage, can use at run time programmable performance of Visual Basic to create objects,

Use object variables to create an object reference,


With "from scratch" create your own object class module,


With a collection of objects to create your own collection,
Introduce other chapter details how to access objects, for example, in "programming with parts will discuss CreateObject" and the GetObject function,

Using the object variable
In addition to store values, variables can reference objects, as well as to the variable assignment, based on the following the same reason, but the object is assigned to variables:

Often compare variable contains the value of the variable name (or, in this case, is that they contain values of reference object) short and easy to remember,


When running the code, in order to reference other objects, should change the variable,


Reference object contains variable has higher efficiency than repeated references to the object itself,
Use object and using conventional variables, only much more variables gives the object to this step:

First declare variables:
Dim variable As the class

Then to assign variables:
The Set variable=object

A statement object variables
Statement object variables method and other variable declaration, with a Dim, ReDim, Static, Private and Public, the only difference is that the New optional keywords and Class parameter, this chapter will be the New keyword and Class parameter were introduced, and the syntax is:

{Dim | ReDim | Static | Private | Public} variable As [New] class

For example, you can declare an object variable, it references the application called frmMain form:

Dim FormVar As New frmMain 'declare a type for frmMain
'the object variables,

Can also declare an object variable, it refers to any form of application:

Dim anyForm As the Form of 'general Form variables,

Similarly, you can declare an object variable, it can be reference in the application of any text box:

Dim anyText As' can refer to any TextBox text box
'(but only is a text box),

Can also declare an object variable, it can refer to any type of control:

Dim anyControl As Control 'general Control variables,

Note that you can declare a reference specified forms in the form of the variables in the application, but you can't declare reference specific controls the control variable, you can declare to reference the specific control type (such as TextBox or list box control variables, but not refer to that type of a particular control (such as txtEntry or List1), however, shall be given the specific control to that type of variable, for example, for a list box lstSample form can code like this:

Dim objDemo As ListBox
The Set objDemo=lstSample

Assign a value object variables
Use the Set statement will give object variables:

The Set variable=object

No matter when, as long as hope variable reference object, can use the Set statement,

Sometimes object variables are available, and especially the available control variables directly to shorten to type in the code, for example, the original code is like this:

If frmAccountDisplay! TxtAccountBalance. Text & lt; 0 Then
FrmAccountDisplay! TxtAccountBalance BackColor=0 frmAccountDisplay! TxtAccountBalance. ForeColor=255
End the If

If you use the control variables, procedures can be significantly shortened:

Dim Bal As TextBox
The Set Bal=frmAccountDisplay! TxtAccountBalance
If Bal. Text & lt; 0 Then
Bal. BackColor=0
Bal. ForeColor=255
End the If

Specific object types and object types commonly
Specific object variable must refer to a specific type of object or class, variable can only be quoted in the application specific form a form (though many instances can be in the form of reference one), is similar to a particular control variable can only be quoted in the application of specific types of controls, such as the TextBox or list box, please have a look at a case, open a new project, a text box is placed in the form, add the following code to the form:

Private Sub Form_Click ()
Dim anyText As TextBox
The Set anyText=Text1
AnyText. Text="Hello"
End Sub

Run the application and click on the form, the Text frame's Text property will become "Hello,"

General object variables available in a variety of specific object types referenced in one, for example, the general forms any variables can be quoted in the application form; General control variable can be referenced in the application of any form of any controls, please have a look at a case, and open a new project in the form in any order placed several Frame, Label and CommandButton control, add the following code to the form:

Private Sub Form_Click ()
Dim anyControl As Control
The Set anyControl=Form1. Controls (3)
AnyControl. Caption="Hello"
End Sub

To run the program, and click the form mentioned in the form of the title of the third control will be "Hello",

There are four general objects in Visual Basic:

General object types can be referenced objects
Any Form in the application Form (including MDI child forms and MDI Form),
The Control in the application of any controls,
MDIForm MDI in the application form (if you have any MDI form application),
Any Object, the Object


In don't know whether variable runtime reference a specific type of object, the general object variable is useful, for example, if you want to write code, and the application of any form can operate, you must use the general form variables,

Note due to only one MDI in the application form, so there is no need to use the general MDIForm type, whenever possible, once the need to declare reference MDI forms the form variables, can be used specific MDIForm type (or MDIForm1, or is the MDI form the Name attribute of the specified any Name), rather than general MDIForm type, in fact, because before running the application, Visual Basic can decided to refer to a particular form type of attributes and methods, so the total should use a specific type of MDIForm,

Just for completeness provide general MDIForm type; In the future versions of Visual Basic, in a single application can have multiple MDI form, it will be very useful,

As an object of the form
Form is the most commonly used to construct the application interface, but also often is the application of other modules called object, is close to the form with the class module, the main difference is that between the two forms is the visual object, and the type of module is no visual interface,

Add custom methods and properties
Can add the custom methods and properties for the form, and can access them from the application of other modules, in order to create the form of new methods, please add in the process of Public declaration,

The method of 'custom Form1,
Public Sub LateJobsCount ()
.
. '& lt; Me>
.
End Sub

The following statement calls from other modules available LateJobsCount process:

Form1. LateJobsCount

Create the form of new properties as the statement in the form module is as simple as a public variable:

Public IDNumber As Integer

Available in two of the following statements from the other module Settings and return to Form1 IDNumber value:

Form1. IDNumber=3
Text1. Text=Form1. IDNumber

Also can use the Property process to form add custom attributes,

Detailed information with object programming "provides a detailed material in the process of the Property,

Note without loading forms can call form variable or a custom method, can also set the custom properties of the form, so, don't need to form loaded into memory can run code on the form, also, refer to the control without reference when its properties or methods, also won't load form,

Use the New keyword
Using the New keyword to create a New object, the object is regarded as its class, as defined by the object, the available New create the collection, the form of instances and modules defined in the class,

In the form to use the New keyword
Create the form when the design is a class, can use the New keyword to create the New instance of the class, in order to see the working process, please draw a command button on the form and several other controls, in the Name attribute of the "properties" window form set to Sample, in the process of command button Click event to add the following code:

Dim x As the New Sample
X.S how

To run the application, and click the command button several times, moving in front of the form to one side, because the form is a class with a visual interface, so you can see attached a copy of each form in the same position have the same control, and the position is the location of the form when the design,

nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull
  • Related