Saturday, August 31, 2013

C# Coding help

This is something C# only coders don't easily understand.


To achieve what you want, replace



// Can NOT access because type being BaseClass
bc2.ExtraDetails = "Extended testing";

by this



// CAN access despite type being BaseClass - if it is an instance of ChildClass
ChildClass cc = bc2 as ChildClass;
if (cc != null)
cc.ExtraDetails = "Extended testing";

I'm assuming the BaseClass is not under your control, otherwise the code could be (and should be) easier.



No comments:

Post a Comment