954,546 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

problem with an abstract class

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!

yilmazhuseyin
Light Poster
48 posts since Oct 2006
Reputation Points: 31
Solved Threads: 5
 

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.

mariocatch
Junior Poster
103 posts since Apr 2007
Reputation Points: 11
Solved Threads: 17
 

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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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! :)

yilmazhuseyin
Light Poster
48 posts since Oct 2006
Reputation Points: 31
Solved Threads: 5
 
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.

mariocatch
Junior Poster
103 posts since Apr 2007
Reputation Points: 11
Solved Threads: 17
 

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
yilmazhuseyin
Light Poster
48 posts since Oct 2006
Reputation Points: 31
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You