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 9255139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:38:48+00:00 2026-06-18T11:38:48+00:00

I have a class that follows the Command Pattern. It has 2 methods which

  • 0

I have a class that follows the Command Pattern.
It has 2 methods which are Execute, and CanExecute which checks whether to invoke Execute or not (they derive from ICommand).
CanExecute invokes a few methods that check that all required services are running, the version is correct, etc.

After CanExecute is invoked, it may fail and return false and I need to know why. Is it because of a bad version, services, missing file, etc.

What is the best strategy to know what is the problem

One option is whenever a required condition fails I can throw an exception that will describe the error in the message field. However the possibility that it will fail is expected and you shouldn’t use exceptions for regular flow of control. So I’m really not sure.

Thank you.

  • 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-06-18T11:38:50+00:00Added an answer on June 18, 2026 at 11:38 am

    You can use a collection of “reasons” that will tell the users of the class why CanExecute returned false. The reasons can be a simple IEnumerable<string>.

    public bool CanExecute() {
      var messages = new List<string>();
    
      if (!Condition1) {
        messages.Add("Missing Condition1");
      }
    
      ...
    
      Messages = messages;
      return messages.Count == 0;
    }
    
    public IEnumerable<string> Messages { get; private set; }
    

    Then, client code can show the collection of messages to end-users.

    UPDATE:

    You can also associate new commands with the messages to give the users ways to fix the problems found. In this case, instead of an IEnumerable<string>, you can create your own class that encapsulates that information:

    public class Message {
      public string Text { get; set; }
      public ICommand Command { get; set; }
    }
    
    ...
    
    public bool CanExecute() {
      var messages = new List<Message>();
    
      if (!Condition1) {
        messages.Add(
          new Message { 
            Text = "Missing Condition1", 
            Command = new FixCondition1Command() 
          }
        );
      }
    
      ...
    
      Messages = messages;
      return messages.Count == 0;
    }
    
    public IEnumerable<Message> Messages { get; private set; }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class that inherits from a generic dictionary as follows: Class myClass
I have a program with a class that contains a public enum, as follows:
Hey we have a library class (lib/Mixpanel) that calls delayed job as follows: class
I have a Class that has a private variable with a public setter/getter function:
I'm getting an anonymous class at compile-time that I'm not expecting. Relevant code follows,
I have read that using command pattern is one of the most popular ways
so i've begun google's python class, and have not had that much difficulty with
I have class that extend FragmentActivity in it I add fragment to layout as
I have class that looks like this: class A { public: class variables_map vm
I have class that represents users. Users are divided into two groups with different

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.