Directly access the overwritten member?

3

Good! I have 3 classes, one is TokenBase , which is used as the parent of the other classes:

abstract class TokenBase
{
    public TokenTypes Type { get; set; }
    public virtual object Value { get; set; }
    public override string ToString()
    {
        return $"Type: {Type}\t\t Value: {Value}";
    }
}

Derived classes:

class StringToken : TokenBase
{
    private string _Value;
    public new string Value 
    {
        get { return _Value; }
        set { _Value = value;}
    }
    public StringToken(string v) { Type = TokenTypes.String; Value = v; }
}

class SymbolToken : TokenBase
{
    private string _Value;
    public new Symbols Value 
    {
        get { return _Value; }
        set { _Value = value;}
    }
    public SymbolToken(Symbols v) { Type = TokenTypes.Symbol; Value = v; }
}

The problem is when I want to call ToString() or Value in one of the derived classes within the list:

public static void Main(string[] args)
{
    IList<TokenBase> Tokens = new List<TokenBase>();
    Tokens.Add(new StringToken("Hola Mundo"));
    foreach (TokenBase T in Tokens)
    {
        Console.WriteLine(T.ToString()); 
        // Obtengo la siguiente salida: "Type: String         Value: "
        Console.WriteLine(T.Value.ToString()); // Obtengo: ""
    }
    Console.ReadLine();
}

// Salida esperada: "Type: String         Value: Hola Mundo"

How can I directly access the Value property of one of the derived types?

    
asked by NaCl 19.04.2016 в 15:56
source

2 answers

2

But you do not need to do what you propose, by defining the property as virtual it has an implementation for the child classes that you could overwrite, but in this case it is not necessary

if you define

abstract class TokenBase
{
    public TokenBase(TokenTypes type, string v)
    {
        Type = type; 
        Value = v; 
    }

    public TokenTypes Type { get; set; }
    public virtual object Value { get; set; }
    public override string ToString()
    {
        return $"Type: {Type}\t\t Value: {Value}";
    }
}



class StringToken : TokenBase
{
    public StringToken(string v) 
        : base(TokenTypes.String, v) 
    { 

    }
}

class SymbolToken : TokenBase
{
    public SymbolToken(string v) 
        : base(TokenTypes.Symbol, v) 
    { 

    }
}

you can from the daughter classes assign the properties of the base

Also if you define a property as virtual you are supposed to use the override

If the behavior should change then you would do the following

abstract class TokenBase
{
    public TokenBase(TokenTypes type)
    {
        Type = type; 
    }

    public TokenTypes Type { get; set; }
    public virtual object Value { get; set; }
    public override string ToString()
    {
        return $"Type: {Type}\t\t Value: {Value}";
    }
}



class StringToken : TokenBase
{
    public StringToken(string v) 
        : base(TokenTypes.String) 
    { 
        this.Value = v;
    }

    public override object Value 
    { 
        get{}; 
        set{ //aqui redefines la implementacion}; 
    }
}

class SymbolToken : TokenBase
{
    public SymbolToken(string v) 
        : base(TokenTypes.Symbol) 
    { 
        this.Value = v;
    }

    public override object Value 
    { 
        get{}; 
        set{ //aqui redefines la implementacion}; 
    }

}   
    
answered by 19.04.2016 / 16:26
source
0

If you change your class StringToken to the following, your example should work:

class StringToken : TokenBase
{  
    public override object Value
    {
        get
        {
            return base.Value;
        }

        set
        {
            base.Value = value;
        }
    }

    public StringToken(string v) { Type = TokenTypes.String; Value = v; }
}

Or if you want to keep your property Value of type String , you can do this other:

class StringToken : TokenBase
{
    public new string Value
    {
        get { return base.Value.ToString(); }
        set { base.Value = value; }
    }

    public StringToken(string v) { Type = TokenTypes.String; Value = v; }
}  
    
answered by 19.04.2016 в 16:15