casting - Cast child as far parent and then as direct child c# -
i can’t cast parent class child – keep getting invalid cast exception.
i can’t overcome problem , rally use help.
i’m developing application creates documents. there total of 20 documents. 19 of themare unique , 1 derives different 1 extending contents. certificates have common header information , document-specific body.
so created document
class. never instantiate document
class directly, through children. 19 documents directly derived document
class. document
class defines fields , properties common documents along methods access sql database. constructors of each document
(child), when initiated no argument call document
’s (parent) constructor passing integer helps constructor select unique information common database table, such unique title of document, disclaimer or footer note.
each document created win form interface. created base interface common certificates , operates on header information contains methods common document
-windowsform interactions. each child document has own form derives parent form. on initialization each form instantiates corresponding document
(child) class , displays document title, disclaimer , footnote on load. since documents rely on header info base form methods defined inside base form. methods accept , return document
data type (parent) because never know form call them. each time pass newly instantiated document document class (parent) , cast returned datatype match needed. way retain unique properties od child documents
while using generic methods collect common information. works 19 of 20 documents. 20th problem. doc20
derives doc19
derives document
class. likewise form20
derives form19
derives genericform
. doc20
in fact significant extension of doc19
.
on form init
instantiate new doc20
object. grab title, disclaimer , footnote on form load event , display needed.
then try call method in genericform
accepts , returns document class , cast return value doc20
. works.
next call method in form19
. method accepts document
class , returns document
class , tasked collecting data controls common form19
, form20
. pass doc20
object document. inside method need access properties specific doc19
(thus doc20
) try cast doc19
, exception gets thrown. interestingly intelisense has no problems syntax , application compiles fine. idea why cant cast parent child?
Comments
Post a Comment