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.




The code is under my control, what I am trying to do, is I have a base class that I created with all of the properties and methods I need to work with it. Each child class will inherit, the base class to add some extra properties, depeneding on what the child class is for. The child class will override the base class Save method, which first it will call the base.Save(), to save all the inherited properties to the base table, and then the child class will save the extended properties to a different table.


I do not want all the data in one table as each child class has different information, so I would have a table with a lot of null values. I hope I am making since and looking at it correctly.




If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.



Don't Retire Technet


No comments:

Post a Comment