I have the following list of slide objects. Based on the value of object’s ‘type’ var I want to upcast the Slide object in the list. Is it possible?
foreach(Slide slide in slidePack.slides)
{
if(slide.type == SlideType.SECTION_MARKER)
{
//upcast Slide to Section
}
}
Section extends Slide and adds one more parameter.
Here’s the proper way to handle that cast.
Edit: There are ways to design programs to not need the test/cast you are looking for, but if you run into a case where to need to attempt to cast a C# object to some type and handle it different ways depending on success, this is definitely the way to do it.