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

  • SEARCH
  • Home
  • 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 254927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:53:17+00:00 2026-05-11T21:53:17+00:00

In the following sample, the writer of the derived class will be expected to

  • 0

In the following sample, the writer of the derived class will be expected to call base.Add(). If it happens 1st, the base can do one sort of code. If it happens last, the base can do another kind of logic (see sample). I doesn’t seem possible to have it both ways. And easy fix would be do stop calling the base method at all because the base will never know if its being called first, last or in the middle or twice!

What is the object oriented way to deal with this? Should I just plain stop putting code into base methods because I will never know the pre and post-conditions?

EDIT: The goal is to have a business object class that does CRUD operations. The repetitious code would be moved to the base class. For example, checking to see if before adding a record, the business object’s id is 0 and checking that after saving, the business object’s id is >0.

namespace StackOverFlowSample
{
    class BusinessObjectBase
    {
        private bool _isNew;
        private int _id;
        public virtual void Add(string newAccount)
        {
            //Code that happens when subclasses run this method with the 
            //same signature

            //makes sense if base is called 1st
            if(_isNew && _id>0) throw new InvalidOperationException("Invalid precondition state");

            //makes sense if bae is called 2nd
            if (!_isNew && _id == 0) throw new InvalidOperationException("Invalid post condition state");
        }
    }
    class BusinessObject : BusinessObjectBase {
        public override void Add(string newAccount)
        {
            //doesn't make sense, because base will need to be called again.
            base.Add(newAccount);//pre validation, logging

            //Save newAccount to database

            //doesn't make sense, because base has already been called
            base.Add(newAccount);  //post validation, logging
        }
    }
}
  • 1 1 Answer
  • 1 View
  • 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-11T21:53:17+00:00Added an answer on May 11, 2026 at 9:53 pm

    If you want a safe way to introduce pre- and post condition checks, you can make Add non-virtual, and instead have another method (AddInternal or something like that) that the derived classes can (or must?) override:

    namespace StackOverFlowSample
    {
        abstract class BusinessObjectBase
        {
            private bool _isNew;
            private int _id;
    
            protected abstract void AddInternal(string newAccount);
            public void Add(string newAccount)
            {
                if(_isNew && _id>0) throw new InvalidOperationException("Invalid precondition state");
                AddInternal(newAccount);    
                if (!_isNew && _id == 0) throw new InvalidOperationException("Invalid post condition state");
            }
        }
        class BusinessObject : BusinessObjectBase {
            protected override void AddInternal(string newAccount)
            {
                //Save newAccount to database
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following sample code. class bar is derived from the base class
I have the following simple class class base { public: int x; base &set(int
I am trying to write a base class, and a set of N derived
I am trying to write a sample program that can call use the main
Here is my problem. I have a base class and a derived class which
I have the following sample code and noticed that if I attempt to use
I wrote the following sample code to see how ARC works @property (nonatomic, weak)
In the following sample, I build a query to do a bulk insert into
I have the following sample code which doesn't seem to want to run. import
I have the following sample data: Id Name Quantity 1 Red 1 2 Red

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.