473,407 Members | 2,315 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

c# search and return values

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


Nov 15 '05 #1
3 5862
Hmm, how about linking Employee objects with their IDs in a Hashtable?
So before getting an employee, you can just check whether the key is
already there, and you can throw an exception.

I don't get what you're saying when you say control cannot leave
finally. What do you mean?

Cheers
Elder

Eric the half a Bee wrote:
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


Nov 15 '05 #2
Why not returning null to signal non-existance.
It doesn't sound right (to me) in this situation to throw an exception.
Also, you should not handle the exception within your search routine,
but let the exception pass to the caller of the search routine.
Like this the exception does not make much sense.

I would do something like this:

public Employee WithID(int EmpID)
{
foreach (Employee employee in personnel)
{
if (employee.EmpID == EmpID)
return employee.
}

// use one of these three !
return null; // to signal non-existance through the return value.
throw new ArgumentException(.....); // to signal non-existance by
throwing an exception.
return new Manager(EmpID); // return a new employee for EmpID.
}

The last option (returning a new employee) is not a good solution. You
don't know if your search
function returned an existing employee or a new employee. While (probably)
you still have to insert
it in personnel.
I should just return null.

Greetings,
Bram.
"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


Nov 15 '05 #3
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


Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: todd | last post by:
here is a search tool SP I wrote. How many times have you wanted to search all your Stored procs or views (in a database) for a keyword but couldn't!? Well now you can! THis can makes life a...
2
by: Ryan | last post by:
I'm looking for a stored procedure (or query) to search an entire database for a specific string value and replace it with another. I'm sure I saw an SP for this a while back by someone, but cannot...
10
by: Craig Keightley | last post by:
I have the following array: function showCPUs(){ //name brandID dualCore dualProcessor var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=; var cpuItem=;
28
by: joshc | last post by:
If I have an array of data that I know to be sorted in increasing order, and the array is less than 50 elements, and I want to find the first element greater than a certain value, is a simple...
8
by: Someone | last post by:
Hi, I wish to use a dictionary.com search box and want the results of a search to open in a new window. Is there a way to do this using their code? The copy and paste code is provided below. ...
6
by: zfareed | last post by:
<code> #include <iostream> #include <fstream> const int MAX_LENGTH = 10; using namespace std;
5
gekko3558
by: gekko3558 | last post by:
I am writing a simple binary search tree (nodes are int nodes) with a BSTNode class and a BST class. I have followed the instructions from my C++ book, and now I am trying to get a remove method...
13
by: jfarthing | last post by:
Hi everyone! I am using the script below to search a db. If the is more than one match in the db, all goes well. But if there is only one match in the db, nothing gets displayed. Any...
0
by: Atos | last post by:
Binary search is used to locate a value in a sorted list of values. It selects the middle element in the array of sorted values, and compares it with the target value; that is the key we are...
6
Kelicula
by: Kelicula | last post by:
Why?: One commonly used algorithm is the binary search. If you don't already know it, you should read on. Very helpful. Saves much CPU. Reduces computations exponentially. When searching...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.