Вопрос

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".

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".

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.

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.

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

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.

Thanks,

Ricardo S.

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top