0

If I have a basic master page and content page and I want to use some property or method of the master page from the content page, such as:

string something = this.Master.MasterPageFile;

Code Contracts static analysis will complain with:

warning : CodeContracts: Possibly calling a method on a null reference 'this.Master'

How can I get Code Contracts to stop complaining whenever I want to access the master page?

R.

[Edit]

Take a look at this?

public partial class Manage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Master.ShowFeedback("Test.", true);
    }

   [ContractInvariantMethod]
   private void ObjectInvariants()
   {
       Contract.Invariant(this.Master != null);
   }
}

I don't want to have to do the following every time I want to call Showfeedback():

Contract.Assume(this.Master != null)
this.Master.ShowFeedback("Test.", true);

Does that make sense?

Richard
  • 5,810
  • 6
  • 29
  • 36

1 Answers1

0

You can add it as an invariant to your class:

[ContractInvariantMethod]
private void Invariants()
{
    Contract.Invariant(Master != null);
}
porges
  • 30,133
  • 4
  • 83
  • 114
  • I already tried that but Code Contracts came back with warning : CodeContracts: invariant unproven: this.Master != null – Richard Jan 20 '11 at 02:12
  • At the end of which method? The constructor? – porges Jan 20 '11 at 02:22
  • I edited my code to show an example. Strange thing is, sometimes that call will not cause a warning. But say I access a method before making the call, then the warning will definitely fire. Confused. – Richard Jan 20 '11 at 03:56
  • That's rather weird behaviour. Perhaps you should ask about it on the Code Contracts forum :) – porges Jan 23 '11 at 00:17