I've just had an epic derp moment...
I've just had an epic derp moment...
I inherited a form, add a few new controls and the related scaffolding.
Then I thought - wow - it would be nice to have a generic type property on that form - so I added
The form shows up and goes BANG on the first new control reference in the code. It was nil. All the new controls were nil.
I open the new form - or rather I try: Parent not found - so I can't open the form designer.
Turns out that adding a generic
Interesting that it compiled, really - considering the visual inheritance was totally broken.
Edit: But it would have been super neat! :P
I'm not quite sure that's a derp moment - if you think of a form as a class, which it is, it makes absolute sense to be able to have it generic, just like any other. (Whether that's a good idea for a UI control or not...) What if you create it entirely in-code, without streaming via the DFM?
ReplyDeleteObviously that will work. It's the streaming that cannot handle this.
ReplyDeleteNot sure if that would work as you are still breaking the visual inheritance which screws up the form initialization. Besides, creating controls in code is so 1992 :P
ReplyDeleteI've got several forms where I use generics, and they've been working fine in Delphi 2010 and Delphi XE5 (we haven't migrated further yet).
ReplyDeleteLike this:
TBaseForm = class(TForm)
TMasterForm = class(TBaseForm);
TMasterForm = class(TMasterForm)
TForm1 = class(TMasterForm); // where TZone descends from TEpgBaseData
Martijn Coppoolse Do they use visual inheritance where you add new components in the designer?
ReplyDeleteWe can't edit a TNewForm = class(TBaseForm), but I do see how I can take a "final" form and add a descendent with a generic type - and that may actually work for me - but that was not what I was trying.
ReplyDelete#derp
I've actually managed to make this work a few times. You do it in two steps.
ReplyDeletetype TMyForm = class(TForm)
// controls and basic logic go here
end; //has a corresponding DFM
type TMyForm = class(TMyForm)
//generic stuff goes here
end;
This should work for you.
Mason Wheeler Yes, that works - but that was not the approach I tried, hence the derp.
ReplyDeleteWow. A Generic Form. Good GIF choice.
ReplyDeleteWarren Postma Forms are classes too. Why shouldn't they be able to take type parameters like any other class?
ReplyDeleteThey are Classes, but they are not just regular Classes. They are Forms. They have persistent data. How would DFM Loading in a generic Form work since the form class name is embedded in the DFM?
ReplyDeleteForm inheritance is a sucky feature in Delphi, and shouldn't be used, even without Generics. Now combine Form Inheritance's regular suckiness, with generic suckiness. That's why.
Let's also ask how would Application.CreateForm work? You would construct a form and pass the generics and the Application would construct a generic form? Ugly.
What happens if the form class name in the DFM is edited to include the ?
ReplyDeleteDavid Millington Most likely it fails to parse, because the form streaming system doesn't know anything about generics.
ReplyDeleteMason Wheeler Yeah, I'd expect it to get to the < and fail with an unexpected token. But you never know...
ReplyDeleteLars Fosdal Ah, no, they didn't use visual inheritance. We stopped using that a long time ago because of the many limitations associated with it.
ReplyDelete(Sorry for the late response, had to leave in a hurry).
Martijn Coppoolse We use visual inheritance in a very limited fashion - i.e. we have a base form which has some graphics and title fields and a main panel. This form is the base for numerous dialogs which add controls of their own. The dialog I was working on was a list selection tool - and the general idea was that I could make a generic one for our class hierarchy - so that the selection parameter actually returned the object instance, and not just some index or similar.
ReplyDeleteAs outlined by others above, I can still achieve this as long as I move the generic bits to a descendent form who doesn't have a visual form of it's own.