This has happened to me numerous times that while debugging, I press F11 (instead of F10) and accidentally step into a generic method or a property which will not provide me with any information which will help in debugging the code.
For example if there is the following property,
private bool isincorporated;public bool IsIncorporated{get { return isincorporated; }set { isincorporated = value; }}
And during debugging, a line is encountered such as:
bool CompanyIncorporated = objCompany.IsIncorporated;
The debugger will enter the property, step through the relevant line and return to the calling function. This activity will seldom provide any information which will help in debugging.
To prevent this, there is nifty little attribute called
[System.Diagnostics.DebuggerStepThrough]
Just add this attribute to your method or property and Visual Studio will automatically step over it.
I had huge help from this attribute while working with Silverlight Validations. I had bound a Silverlight TextBox with a property and I had it throw a Custom Exception when that particular property (TextBox) was blank. So essentially my XAML looked like:
<TextBox Name="txtCompanyName" Text="{BindingCompanyName, Mode=TwoWay, ValidatesOnExceptions=True}"/>
And the CompanyName property was implemented in the following way:
private string companyname;
public string CompanyName
{
get { return companyname; }
set
{
if (value != "")
{
companyname = value;
}
else
{
throw new Exception("Company Name cannot be blank.");
}
}
}
So whenever the TextBox was blank, Visual studio would throw an exception and I would find it very difficult to debug further. So what I did was, I added the
[System.Diagnostics.DebuggerStepThrough] attribute to the set method of the property and Visual Studio stopped throwing the exception explicitly.
So my Final code looked like this:
private string companyname;
public string CompanyName
{
get { return companyname; }
[System.Diagnostics.DebuggerStepThrough]
set
{
if (value != "")
{
companyname = value;
}
else
{
throw new Exception("Company Name cannot be blank.");
}
}
}
And now I could debug away happily!
No comments:
Post a Comment