Hi! I was writing a calculator to practice my programming skills and I really cannot solve a problem with an abstract class

//base class for all tokens
    public abstract class BaseToken
    {
        public override string ToString() {
            return tokenValue;
        }
    }

    //base class for all operators
    public abstract class Operators:BaseToken {
        protected char tokenValue;
    }

    //numbers
    public class number : BaseToken {
        protected int tokenValue;
        public number(int val) {
            tokenValue = val;
        }
    }

problem here is that I implement "tokenValue" varaible in sub-classes of baseToken class but I want to use it in base class but it gives me error (the name 'tokenValue' does not exist in the current context) what can I do to solve that problem. thank you!

BaseToken is returning a child classes variable (tokenValue), which it doesn't know exists because it's the Base class. The base class should declare the variable.

You derive Operators from BaseToken, but tokenValue is a member of Operators, not BaseToken. So BaseToken can't use it because inheritance goes down the chain, not up it.

yes, but base token is an abstract class and I will not use base token except for inheritance. so it would be logical to use child class declerations in base class. I just think that there should be a way to do that.
real problem here is I have a tokenValue which is a char in operators and tokenValue in numbers which is an int. thats why I cannot declare that varaible in baseToken. and thank you for your answers! :)

real problem here is I have a tokenValue which is a char in operators and tokenValue in numbers which is an int.

no the real problem is that you're using a variable before it exists. which is what Narue and I said.

well I couldn't find a way to use that variable before I declare it but I work my way around with an abstract property . and I have my toString function in my base class . here is the code. thanx for all the replies.

//base class for all BaseToken
    public abstract class BaseToken
    {
        public abstract string tokenString{
            get;
           }
        public override string ToString() {
            return tokenString;
        }
       
    }

    //base class for all operators
    public abstract class Operators:BaseToken {
        protected char tokenValue;
        public override string tokenString{
            get{
                return tokenValue.ToString();
            }
        }//tokenString
    }
    public class number : BaseToken {
        protected float tokenValue;
        public number(float val) {
            tokenValue = val;
        }//constructor
        public override string tokenString{
            get{
                return tokenValue.ToString();
            }
        }//tokenString
    }//number
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.