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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T00:14:46+00:00 2026-05-22T00:14:46+00:00

I understand the use of CanExecute() and Execute() , but I’m wondering about the

  • 0

I understand the use of CanExecute() and Execute(), but I’m wondering about the following scenario:

public class MyViewModel : NotificationObject
{
    public MyViewModel()
    {
        FooCommand = new DelegateCommand(DoFoo, CanDoFoo);
    }

    public Bar MyBar { get; set; }

    public DelegateCommand FooCommand { get; private set; }

    public Boolean CanDoFoo()
    {
        return (MyBar != null)
    }

    public void DoFoo()
    {
        MyBar.BarFunc(); //Potential for a NullReferenceException
    }
}

Basically, a consuming view could decide to call directly into the DoFoo method (obviously breaking the point of the ICommand interface) and cause a NullReferenceException. This may be a bit subjective, but I’m hoping for a “standard” way of doing it.

Do we:

  1. Prevent the possible NullReferenceException by doing a if (MyBar != null) first?
  2. Prevent the possible NullReferenceException by verifying that CanDoFoo() returns true?
  3. Assume that the consuming view is behaving properly and has already verified that it can call into the DoFoo() method?

As a side note, the primary reason I asked this is because when I was writing unit tests I realized that someone could break my ViewModel by calling Execute() methods without calling their CanExecute() counterparts? Obviously in my unit tests I check to see that I can execute the method before doing so, but consuming views might decide to ignore that.


Update: (Scenario 2)

As an expansion to this question, I also want to add in the scenario where the DoFoo() method does not break in terms of exceptions, but could break logically?

public class MyViewModel : NotificationObject
{
    public MyViewModel()
    {
        FooCommand = new DelegateCommand(DoFoo, CanDoFoo);
    }

    public Int32 Age { get; set; }

    public DelegateCommand FooCommand { get; private set; }

    public Boolean CanDoFoo()
    {
        return (Age >= 21)
    }

    public void DoFoo()
    {
        ProvideAlcohal(Age);
    }
}

This second scenario does not actually break (the command can process fine), however, it breaks down logically. So, do we validate the business logic a second time by calling CanDoFoo() or assume the consuming view is behaving? (Remember, this only breaks the business logic).

Basically it boils down to this… Do we put in preventive measures to ensure the consuming view is not shooting itself in the foot by misbehaving?

  • 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-22T00:14:47+00:00Added an answer on May 22, 2026 at 12:14 am

    Any implementation of command calling in WPF or Silverlight will do this, so you don’t have to worry about it from the UI system…

    But it is a public method. Checking for null is one of the fastest things you can do. It doesn’t hurt and it is much more safe since you will throw a null exception without the guard clause.

    Semantically, CanExecute may be implemented differently than a null check, so I would just do the null check in the Execute method, not necessarily check CanExecute.

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

Sidebar

Related Questions

I understand how to use Protoype in standard Javascript with Node.js and modules, but
I'm thinking about trying MongoDB to use for storing our stats but have some
I have tried to understand and use urlencode, but the problem can't be solved.
I fully understand the use of list in lisp but I 've got problem
I am trying to understand and use the PSR-0 Autoloader. But it doesn't work.
I am reading about MessageContract & MessageHeader and trying to understand the use case
Trying to understand the use of verifySet etc... but unless I do a workaround
I'm trying to learn Perl and understand something about use and modules. (Assume use
For eg. public string conString =@Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\\..\\PersonDatabase.mdb; I don't understand its use.What am I
I am struggling to understand the use of class.forName. Actually i am working on

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.