Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3271614
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T18:44:43+00:00 2026-05-17T18:44:43+00:00

Use case: I’m using data templates to match a View to a ViewModel. Data

  • 0

Use case: I’m using data templates to match a View to a ViewModel. Data templates work by inspecting the most derived type of the concrete type provided, and they don’t look at what interfaces it provides, so I have to do this without interfaces.

I’m simplifying the example here and leaving out NotifyPropertyChanged, etc., but in the real world, a View is going to bind to the Text property. For simplicity, imagine that a View with a TextBlock would bind to a ReadOnlyText and a View with a TextBox would bind to WritableText.

class ReadOnlyText
{
    private string text = string.Empty;

    public string Text
    {
        get { return text; }
        set
        {
            OnTextSet(value);
        }
    }

    protected virtual void OnTextSet(string value)
    {
        throw new InvalidOperationException("Text is readonly.");
    }

    protected void SetText(string value)
    {
        text = value;
        // in reality we'd NotifyPropertyChanged in here
    }
}

class WritableText : ReadOnlyText
{
    protected override void OnTextSet(string value)
    {
        // call out to business logic here, validation, etc.
        SetText(value);
    }
}

By overriding OnTextSet and changing its behavior, am I violating the LSP? If so, what’s a better way to do it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T18:44:43+00:00Added an answer on May 17, 2026 at 6:44 pm

    LSP states that a subclass should be substiutable for it’s superclass (see stackoverflow question here). The question to ask yourself is, “Is writeable text a type of readonly text?” The answer is clearly “no”, in fact these are mutually exclusive. So, yes, this code violates LSP. However, is writable text a type of readable text (not readonly text)? The answer is “yes”. So I think the answer is to look at what it is you’re trying to do in each case and possibly to change the abstraction a bit as follows:

    class ReadableText
    {
        private string text = string.Empty;
        public ReadableText(string value)
        {
            text = value;
        }
    
        public string Text
        {
            get { return text; }
        }
    }          
    
    class WriteableText : ReadableText
    {
        public WriteableText(string value):base(value)
        {
    
        }
    
        public new string Text
        {
            set
            {
                OnTextSet(value);
            }
            get
            {
                return base.Text;
            }
        }
        public void SetText(string value)
        {
            Text = value;
            // in reality we'd NotifyPropertyChanged in here       
        }
        public void OnTextSet(string value)
        {
            // call out to business logic here, validation, etc.       
            SetText(value);
        }
    }     
    

    Just to be clear, we’re hiding the Text property from the Readable class using the new keyword on the Text property in the Writeable class.
    From http://msdn.microsoft.com/en-us/library/ms173152(VS.80).aspx:
    When the new keyword is used, the new class members are called instead of the base class members that have been replaced. Those base class members are called hidden members. Hidden class members can still be called if an instance of the derived class is cast to an instance of the base class.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.