On Fri, 14 Jan 2011 18:30:45 -0600, "mp" <(E-Mail Removed)> wrote:
>this is a method in a class to provide access to Excel
>where _xlApp etc are private member vars for the respective objects
>they shouldn't ever be null by the time this is called...but just in case...
>
>public Range GetRange(string rangename)
> {
> Range rng=null ;
> if (_xlApp != null)
> {if (_xlWorkBook != null)
> {if(_xlWorkSheet!=null)
> {
> rng = (Range)_xlWorkSheet.get_Range(rangename,
>System.Reflection.Missing.Value);
> _xlRange = rng;
> }
> }
> }
> return rng;
> }
>
>would it be advised to provide an else for each if
>if (_xlApp != null)
>{}
>else
>{throw exception or ???}
>that would make the layered if clauses messy
>
The code is already pretty messy
>or better to
>if (_xlApp == null){...throw excep and bail}
>if (_xlWorkBook == null){...throw excep and bail}
>if (_xlWorkSheet == null){...throw excep and bail}
>...aok so get the range...
>
Are all those nested if statements really needed? It appears the only
value which needs to be checked for null is the _xlWorkSheet member.
I'm guessing if that member isn't null then neither are the _xlApp and
_xlWorkBook members.
public Range GetRange(string rangename)
{
Range rng=null ;
// validate argument as well
if(_xlWorkSheet!=null && !String.IsNullOrEmpty(rangename))
{
rng = (Range)_xlWorkSheet.get_Range(rangename,
System.Reflection.Missing.Value);
_xlRange = rng;
}
return rng;
}
Another consideration should be the need to raise an exception. I
would suggest evaluating the possibility of simply returning null. The
caller can check the return value before proceeding.
regards
A.G.