I have the follow code but it is awkward. How could I better structure it? Do I have to make my consuming class implement IDisposable and conditionally construct the network acc
The using statement is a shortcut to avoid "finally" blocks and should only be used when it makes the code easier to follow. In your case I would write the following code. It may not be as brief as some of the other versions, but is much more straight forward.
protected void ValidateExportDirectoryExists()
{
Core.NetworkAccess access = useNetworkAccess ? new Core.NetworkAccess(username, password, domain) : null;
try
{
CheckExportDirectoryExists()
}
finally
{
if (access != null)
{
access.Dispose();
}
}
}