Click here to Skip to main content
15,921,660 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi,
I have multiple derived classes with a single common base class. I would like to share the base class properties across all derived classes. Furthermore, I would like to set the base class properties only once. Here's my problem, when I set the base class properties on one derived class, those same properties are null in other derived classes. How should I architect my program to accomplish this task?
Posted
Comments
Sergey Alexandrovich Kryukov 27-Oct-15 10:24am    
We call it "trying to break through an open door".
—SA
F-ES Sitecore 27-Oct-15 10:42am    
The closest thing to your requirement is to use static variables, but note that static variables are shared across *all* objects in the app domain, including multiple users\sessions if your code is running in a web site.

1 solution

This is called "inheritance". You always, absolutely always "share" all members of base classes with all derived classes. You need to learn at least the very basics of OOP before you can "architect your program" or just simply write anything sensible. I would advise to start right now.

Now, your members of the base class are null only because you overwrite (be careful with terminology, I did not say "override") then in your derived classes, by one or another reason, say, in your constructors, or other kind of initialization, in the call to inherited constructors, etc. If you simply don't touch these members, they will be the same as in your base classes. (Of course, I only mean instance members; static members make only one object instance per Application Domain.)

—SA
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900