[C#] Set Up Project For Testing Purposes

AceInfinity

Emeritus, Contributor
Joined
Feb 21, 2012
Posts
1,728
Location
Canada
I always have a project open when i'm on my computer which is there for testing purposes; putting together code as demonstrations or examples for others, testing variation code for performance, ect...

Whether you guys like it or wouldn't use this method, I think it works okay, for basic testing:

I have this bit of code in my main trigger event (maybe a button click half the time):
C#:
#region RunTests From MainMethod()
	#if (RunTEST)
	            Stopwatch sw = Stopwatch.StartNew();
	#endif
	            MainMethod();
	#if (RunTEST)
	            MessageBox.Show(sw.ElapsedMilliseconds.ToString());
	#endif
#endregion

Now we have our MainMethod() which is where all the real code actually goes:
C#:
private MainMethod()
{
	//Some code here
}

Make sure to put this at the ABSOLUTE TOP of your code, there should be NOTHING above these lines in your source:
C#:
#define RunTEST
#undef RunTEST

Now as long as #undef RunTEST reads, we're not actually testing, and no MessageBox shows up with the StopWatch time of how long it takes MainMethod() to execute. But if we comment that out:

C#:
#define RunTEST
//#undef RunTEST

Run it again, and after the call to MainMethod() completes, you'll have your quick little runtime display in a MessageBox :)

Cool stupid simple use of preprocessor keywords in C#.

:beerchug2:
 
Last edited by a moderator:

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top