在派生形式之间共享代码 [英] Sharing code between derived forms

查看:26
本文介绍了在派生形式之间共享代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的一个项目中实现了视觉继承.从基本形式有两种派生形式.我还有两个来自一个基类的常规派生类.每个派生形式都处理派生类中的一个,以创建适当类的对象.例如,有一个称为letter"的基本形式和两个派生形式A"和B",而有一个基类base"和两个派生类a"和b".当我调用"A"时,会实例化来自a"的对象,b"和B"的规则相同.

I have implemented visual inheritance in a project of mine. There are two derived forms from a base one. I also have two regular derived classes from one base class. Each derived form deals with one of the derived classes instatiating an object of the proper class. For example, there is a base form called "letter" and two derived forms "A" and "B" whilst there is a base class "base" and two derived classes "a" and "b". An object from "a" is instantiated when I "call" "A" and it´s the same rule for "b" and "B".

我在表单A"上所做的是使用几个文本框来填充类a"中对象的属性,在表单B"的情况下,填充类中"对象的属性b".

What I do on the form "A" is to use several textboxes to fill the properties of the object from class "a" and, in the case of form "B", to fill the properties of the object from class "b".

我有点期望做的是从程序的某个点调用"每个表单(A"或B") - 在这种情况下,另一个表单 - 并处理我拥有的对象用正确形式的事件实例化.

What I was sort of expecting to do was to "call" each form ("A" or "B") from a certain point of the program - in the case, another form - and to work on the object I have instantiated with the events of the correct form.

问题是我的来自表单A"和B"的事件代码在 95% 的时间是相同的.我最初以为我可以将所有这些放在基本形式(字母")上并在那里处理派生对象,但我不能.发生这种情况主要是因为我不能使用我在派生形式上实例化的对象和来自基本形式的事件.例如,我无法使用我在A"上创建的a"类中的一个对象以及信件"上的事件,尽管我可以使用我在a"上创建的a"类中的对象字母"与A"上的事件.经过一番思考,我明白为什么会这样了.

The thing is my code for events from forms "A" and "B" are the same 95% of the time. I first thought I could put all of that on the base form ("letter") and work with the derived objects there, but I can´t. This happens basically because I can´t use the object I have instantiated on a derived form with the events from the base form. For example, I can´t work with the one object from class "a" that I have created on "A" with events on "letter", although I can work with an object from class "a" that I have created on "letter" with events on "A". After some ponderation, I do get why this happens.

我的解决方案是将所有事件代码放在表单A"和B"上,并且效果很好.

My solution was to put all the events code on the forms "A" and "B" and it works well.

问题是我的A"和B"形式的事件在 95% 的情况下仍然相似.我想我正在做一些非常混乱和不必要的事情.所以,我的问题是:如何优化我的工作,以便我只能编写一次事件?大声笑,长帖和简短的问题.感谢您的帮助.

The thing is my events from forms "A" and "B" are still similar 95% of the time. I think I am doing something pretty messy and unecessary. So, my question is: how do I optimize my work so that I can write the events only once ? lol, long post and short question. I appreciate any help.

谢谢,

里卡多 S.

推荐答案

letter 可以有一个 base 实例.在 A 中会有一个将 base 的实例转换为 a 的属性.

letter could have a instance of base. In A would have a property that convert the instance of base to a.

Class letter
    Inherits Forms

    protected _base As base
End Class

Class A
    Inherits letter

    Public ReadOnly Property objA As a
        Get
            Return CType(_base, a)
        End Get
    End Property

    Public Sub New()
        _base = new a
    End Sub
End Class

另一种选择是创建一个包含类似功能的用户控件,该功能将基数作为参数.然后,您只需将相同的用户控件放入两个表单中即可.

An other option is to create a user control that contains the similar functionality that would take a base as parameter. Then you just need to put that same user control into both forms.

这篇关于在派生形式之间共享代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆