Polymorphism in VB 6

zhaozj2021-02-16  55

Polymorphism in VB 6

Recently written a MDI form, do some operations for each time you call a child. So I use a SUB to do the operation of the subform

Private Sub ShowFRM (Byref TFRM AS FORM)

{Code. . . . . . }

Tfrm.show

End Sub

Suppose there are two sub-form frMProduct product lists, and the FRMUSER user list, then use the two forms to use

Call showfrm (frMproduct)

Call showfrm (frmuser)

FRMPRODUCT is an object (entity) of the FORM class that is automatically generated by the system.

Of course, there is a FRMPRODUCT class in the system. They are just the same name.

This is in the showfrm in the showfrm, which is called in the showfrm when the display subform is displayed. . . . . } You can do what you want to do for TFRM, the foreground color, and the size of the desired operation, and it is convenient to modify it. I always believe in different places that implement the same functionality, or don't need a function, or what is packaged, you will bring more trouble to yourself.

But if I want to operate with a variable in FRMPRODUCT, then the current showfrm cannot be done. Because he only processes the FORM class, STRA is a frMproduct class. So change the showfrm

Private Sub ShowFRM (Byref TFRM As FRMPRODUCT)

{Code. . . . . . }

Tfrm.show

End Sub

Such ShowFRM can handle the FRMPRODUCT STRA. But Call showfrm (frmuser) will report an error, why don't you say more, even frmuser also has a Public Stra AS String variable.

Now I want ShowFRM to handle FRMPRODUCT STRA and can process the FRMUSER's Stra.

Yes VB 6 allows us to achieve this showfrm. I first thought of the interface, what is interface, interface, I personally believed that the interface is a class describing the common features of multiple classes, which is a description of multiple class contractions. Newly built a category of IFRMBase in the VB project:

Public property Get Stra () AS STRING

End Property

Public Sub show () 'Display Subform

End Sub

IfRMBase huh, you will know that the name is the basic form interface, and all child forms must comply with the description of IFRMBase.

Then tell the system, frmProduct and frmuser are classes that conform to IFRMBase.

Add in frmproduct, frmuser

IMPLEMENTS IFRMBASE

'----------------------------

'Interface implementation

'---------------------------

Private Property Get IFRMBase_staa () AS STRING

IFRMBASE_STAA = Me.staa

End Property

Public Sub IFRMBase_show ()

Me.show

End Sub

Private Sub ShowfRM (Byref TFRM As IFRMBase)

{Code. . . . . }

Tfrm.show

End Sub

So you can write the code where you call the child form.

Call showfrm (New frMproduct)

Call showfrm (new frmuser)

Seeing this, ShowFrm () is a polymorphic. The class he has to deal with is unable to compile, so he is lagging behind. I have encountered a lot of people with a look that the VB has a look, saying that VB has no polymorphism. I just want to say that VB is an excellent development tool. In addition, I still want to say more. Recently hot factory models. It is indeed a handling mode in line with the treatment mode in line with only items that meet certain features.

The blacksmith only has a multi-iron thing. Doctors are only treated with people who are sick. The pump is only pairs of balls, tires, which are air in the air core, and the inner enclosed object. If the pump made can only give the ball, I will still count.

I am first writing, I hope you will support it!

转载请注明原文地址:https://www.9cbs.com/read-23655.html

New Post(0)