Wrox Programmer Forums
|
C# 4.0 aka C# 2010 General Discussion Discussions about the C# 4.0, C# 4, Visual C# 2010 language and tool not related to any specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 4.0 aka C# 2010 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 18th, 2011, 06:00 AM
Authorized User
 
Join Date: Jul 2007
Posts: 29
Thanks: 2
Thanked 0 Times in 0 Posts
Default Novice logic question.

I am writing an application which performs a series of steps in the order specified:
1. Read a file (xml)
2. Create Directory
3. Create files in that directory
4. Some other step
5. Some more steps
If any step fails none of the remaining steps are performed.
All the steps listed will be methods of a Perform class which will be instantiated in the Main method.
A straightforward way to do this is make all the methods in the Perform class as bool and check their return value in a if loop like so:
if(1stStep)
Code:
{
 if(2ndStep)
 {
  if(3rdStep)
  // And so on ...
 }
}
This loop will grow when more steps are added.
Can someone suggest/guide in implementing this in any other way?

Regards.
 
Old November 18th, 2011, 06:53 AM
Wrox Author
 
Join Date: Sep 2010
Posts: 175
Thanks: 3
Thanked 53 Times in 53 Posts
Default

If the methods return false when they fail, you could do
Code:
if (!CreateFilesInThatDirectory())
{
  return;
}
if (!SomeOtherSteps())
{
  return;
}
This way you immediately return from the method in case and don't have nested if.

However, having errors from these methods should be some "exceptional" cases. You should throw exceptions in case of an error. That's a better way to deal with this.
__________________
Christian
CN innovation
Visit my blog at: csharp.christiannagel.com
Follow me on twitter: @christiannagel
The Following User Says Thank You to ChristianNagel For This Useful Post:
codehelp (November 18th, 2011)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Novice in C# -Incrementing Employee Class GoBlue C# 4.0 aka C# 2010 General Discussion 2 May 1st, 2014 01:38 AM
What is AJAX novice looking for? daxianjin Ajax 0 January 13th, 2008 10:13 PM
novice to ado.net ganeshshmg ADO.NET 2 October 13th, 2006 05:33 PM
Novice User - Please Help, no-one else to ask! KarenWilliams Dreamweaver (all versions) 2 October 25th, 2005 05:29 AM
Mixing Data access logic and business logic polrtex BOOK: Professional Jakarta Struts 0 December 15th, 2003 07:19 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.