Sebastian Litwiniuk
Sebastian Litwiniuk

Reputation: 175

Subclass overrides value of variable in Base class

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?

Upvotes: 1

Views: 235

Answers (2)

Sebastian Litwiniuk
Sebastian Litwiniuk

Reputation: 175

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.

Upvotes: 0

Jeroen van Langen
Jeroen van Langen

Reputation: 22073

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  | |
| |            | |       | |            | |
| +------------+ |       | +------------+ |
+----------------+       +----------------+

Upvotes: 1

Related Questions