Home Assigning of variables in parent class in c#
Reply: 0

Assigning of variables in parent class in c#

user9944
1#
user9944 Published in September 21, 2018, 8:04 am

I’m working with a state pattern and are wondering how to define variables so they can use in each child class. A protected variable in the abstract parent class may be the right choice, but with this, I’m wondering how to initialize these variables from the main class.

class Main
{
    \\Initialize variable "file" here?  
    \\...
            Context tc = new Context(new Step01());
    \\...
}


class Context
{
    private State ts;

    // Constructor
    public Context(State st)
    {
        this.State = st;
    }

    // Gets or sets the state
    public State State
    {
        get
        {
            return st;
        }
        set
        {
            st = value;
        }
    }

    public void Request()
    {  
       ts.Handle(this);
    }
}


abstract class State
{
    protected string file = "file";
    public abstract void Handle(Context tc);
}

class Step01 : State
{      
    tc.State = new Step02();
    // use variable "file"
}

class Step02 : State
{   
    tc.State = new Step0x()   
    // use variable "file"
}

The example is a code snipped and don't work. I hope it helps to explain my question more accurate.

The quantity of child classes (Step0x) varies, so I think it's easier to define the variable only once in the parent class.

Does anybody have an idea how to initialize my variables in the main class?

Thank you.

share|improve this question
  • Why not initialize it in the abstract class's constructor? the fact that it's an abstract class doesn't mean you can't have a constructor for it... – Zohar Peled Feb 11 at 10:24
  • 1
    Is your question how to declare a variable so that all the State subclasses can use it? Make sure you understand that the way you declared it, they will all have access to the "file" variable, but not to the same instance, so not necessarily the same value. – Gonen I Feb 11 at 10:24
  • Defining it in the abstact works pretty well, but I try to find a way defining it in the main. This should keep it more simple because you only have to adapt the main, not the abstract. The value of “file” should be constant in all instances. It’s not necessary to change it. – Mr. Knackwurst Feb 11 at 10:29
  • If the value should be set from Main and shared between all instances inheriting the abstract base class, why not use a static property in the base class? – Zohar Peled Feb 11 at 10:32
  • @Mr.Knackwurst - If the dependency is hard coded across all instances on State, you may prefer to add it to a configuration file and the base abstract class will simply read it from there. – Amir Popovich Feb 11 at 10:33

1 Answer 1

active oldest votes
up vote 1 down vote accepted
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330285 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO