Hello Eric,
Your routine is unusual, I must say.
First off, it does not make sense to raise an error at the end of a block of
code that you promptly catch. You are not catching any other errors, just
the one that you, yourself, raised.. That's not what it's for, and the code
is more complicated, difficult to follow.
Secondly, you have no reason to return from within a Finally block.
Drop the try catch block altogether.
Secondly, your design is not clear. What is the method responsible for? If
it is responsible for returning the object from a list where the object is
expect to exist, raise an error and let the calling code catch it. If it
responsible for searching a list and, if found, returning a value, then
return the value or a sentinel value (like NULL) that can be detected in the
calling code (sentinels have fallen out of favor in recent years because too
many times, errors occur when the calling code doesn't check the return).
Of the two, I prefer the idea of raising an error, but that means that you
let the calling code catch it.
Third, Console.WriteLine? Are you writing a console app? If so, this makes
sense. If not, consider using a logging object or a debug message handler.
The problem with Console.WriteLine is that these things tend to linger long
after you need them.
Fourth, what kind of Collection is it? Many collections have the ability to
find members by keys... why not use one of them, rather than having to write
your own search routine at all.
Fifth, why create a new manager? Seems like the responsibility of your
method is to return an employee object, no matter what. However, that seems
like an unlikely responsiblity. You are more likely to introduce a bug this
way.
So, my suggestions lie with the other responders:
public Employee WithID(int EmpID)
{
foreach(Employee employee in personnel)
{
if (employee.EmpID == employee.EmpID)
return employee;
}
throw new ApplicationException("Employee " + EmpID.ToString() + " not
found");
}
Calling code:
Employee emp;
try
{
emp = this.WithID(currentid);
// do something with the employee found
}
catch
{
// decide now if you want to create a new manager object.
emp = new Employee();
}
Hope this helps,
--- Nick Malik
"Eric the half a Bee" <er**@half-bee.org> wrote in message
news:bu**********@news7.svr.pol.co.uk...
Hello
I am trying to implement a search function within a collection of
Employees. I am searching for a specific EmpID number in the
collection, and if it is found, I want to return the Employee. This I can do.
The problem comes if the EmpID is not found in the collection. The only
way to achieve this I can think of is to throw an invalid argument exception:
public Employee WithID(int EmpID)
{
try
{
foreach(Employee employee in personnel)
{
if (employee.EmpID == employee.EmpID)
return employee;
}
throw new ArgumentException();
}
catch (ArgumentException ae)
{
Console.Out.WriteLine("Invalid argument passed. EmpID not in
records.{0}",ae.ToString());
Console.Out.WriteLine(ae);
}
finally
{
return new Manager();
}
}
This of course doesn't work either, as control apparently cannot leave a
finally clause.
Any suggestions?
Thanks