Assert


Jump to: navigation, search
Visual C# Tutorials
Visual C# .NET Tutorials
Testing and Debugging
edit

Assert is perfect if you want to be informed via a message box if something doesn’t meet your expectations.


Contents

Example

using System;
using System.Windows.Forms;
using System.Diagnostics;
 
namespace debugTesting {
 
  public partial class Form1 : Form {
 
    public Form1() {
 
      InitializeComponent();
    }
 
    private void pass_Click(object sender, EventArgs e) {
 
      int tempValue = 42;
 
      Debug.Assert(tempValue > 0);
 
      MessageBox.Show("I got here... Just as I should.. No Error");
    }
 
    private void fail_Click(object sender, EventArgs e) {
 
      int tempValue = -42;
 
      Debug.Assert(tempValue > 0);
 
      MessageBox.Show("If I get here...Well.. Something's not right");
    }    
  }
}

Result in Development Mode

Image:Assert.gif

Result from Debug Build

Image:AssertDebug.gif

Result from Release Build

Image:AssertRelease.gif


Share this page
  • del.icio.us
  • Facebook
  • Google+
  • StumbleUpon