Home > database >  Subclass overrides value of variable in Base class
Subclass overrides value of variable in Base class

Time:06-03

I have two pages that inherit from the Base class.

In Base, I declared a string, the value of which I set on the A Page. After switching to the B Page, the value of this string is overwritten as empty.

Is there a way to pass this string between these pages?

CodePudding user response:

You are wrong, page A does not share instance data with page B. Although they share the same base class. They are different instances. Inheritance is about extending base classes with extra information. This also means that multiple Page A instances won't share data as well.

 ----------------         ---------------- 
|     Page A     |       |     Page B     |
|  ------------  |       |  ------------  |
| | BaseClass  | |       | | BaseClass  | |
| |  --------  | |       | |  --------  | |
| | | string | | |       | | | string | | |
| |  --------  | |       | |  --------  | |
|  ------------  |       |  ------------  |
 ----------------         ---------------- 

Who is creating Page A/B? This class can also be responsible to pass data between Page A and B. (passing via contructor/properties)

                ------------ 
               | DataClass  |
               |  --------  |
               | | string | |
               |  --------  |
                ------------ 
                /          \    
 ----------------         ---------------- 
|     Page A  /  |       |   \  Page B    |
|  ------------  |       |  ------------  |
| | BaseClass  | |       | | BaseClass  | |
| |            | |       | |            | |
|  ------------  |       |  ------------  |
 ----------------         ---------------- 

CodePudding user response:

For anyone with similar problem I solved it by using design pattern called mono state.

Basically I created public class which has static variables that store values for me. Then I refer to them by creating object of that class.

  • Related