Something about overloading really bugs me

  • Thread starter Thread starter Ronald S. Cook
  • Start date Start date
R

Ronald S. Cook

As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.


This just seems like a cruddy workaround... to have most everything as part
of your one overloaded function, but then to have 1 or 2 stragglers that
have to be named separately.

Is this just life and I have to deal with it, or is there a better approach?

Thanks,
Ron
 
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.

This just seems like a cruddy workaround... to have most everything as part
of your one overloaded function, but then to have 1 or 2 stragglers that
have to be named separately.

Is this just life and I have to deal with it, or is there a better approach?

Thanks,
Ron

If that sort of overloading were allowed, how would the compiler
choose which method to call? It has no idea what the string you pass
in is, so there's no way it could call the correct method.

Chris
 
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.

This just seems like a cruddy workaround... to have most everything as part
of your one overloaded function, but then to have 1 or 2 stragglers that
have to be named separately.

Is this just life and I have to deal with it, or is there a better approach?

Thanks,
Ron

Besides bad example EmployeeNumber would probably be an integer.
Faced with this same problem I created an enum containing firstname
and lastname and based off of what name it is, passed it to the
correct database function.
 
Chris Dunaway said:
If that sort of overloading were allowed, how would the compiler
choose which method to call? It has no idea what the string you pass
in is, so there's no way it could call the correct method.

Wouldnt it be good if the compiler just could simply read our minds :D

in the above example you could have a string to see if it was a name or a
number.
assuming no employee is called 4 or something

Colin =^.^=
 
As mentioned, how would the compiler know which one to call when you asked
for

myobject.GetEmployeeList("Harrison");


Do you want
Harrison Ford
or
John Harrison
Mary Harrison
???


That's magic fairy dust stuff, where you want to computer to read your
mind....which it cannot do.

...

If it bothers you , you can do a simple enum

public enum EmployeeSearchType
{
ByLastName ,
ByFirstName
}


GetEmployeeList ( EmployeeSearchType est , string name )
{

}



myobject.GetEmployeeList(EmployeeSearchType.ByFirstName , "Harrison");
//I get Harrison Ford


myobject.GetEmployeeList(EmployeeSearchType.ByLastName , "Harrison");
//I get John Harrison and Mary Harrison
 
Thanks guys (for putting me in my place mostly).. I appreciate all the
responses.
 
Thanks guys (for putting me in my place mostly).. I appreciate all the
responses.

in message

Think we're done bashing your mindset... =)

I've had lots of things done with overloaded methods but its very nice
when working within the intellisense because you can type Search(blah,
blah) and the compiler knows which method you're working with rather
than one of 100 methods which the developer has to go through and
choose. The method used my sloan is the one that I used on my last
search procedure and it worked fairly well. I wrote a private method
behind the lines to pick FirstName/LastName and throw it into the main
search but its not that bad working with methods that aren't public.
 
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.

This just seems like a cruddy workaround... to have most everything as part
of your one overloaded function, but then to have 1 or 2 stragglers that
have to be named separately.

Is this just life and I have to deal with it, or is there a better approach?

I've never come across that deficiency but I can see your point. The
way I would handle it would be to make one overload that took two
parameters, one the string, and the second one being some kind of
indicator variable (bool or enum) that indicated which of the two sub-
functions was being invoked. Then, in the shared constructor, handle
both situations, using the second parameter to decide which of the two
functions was being invoked.
 
Ronald S. Cook said:
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.


This just seems like a cruddy workaround... to have most everything as
part of your one overloaded function, but then to have 1 or 2 stragglers
that have to be named separately.


are you sure you need overloads at all?

GetEmployeeList(string s, int qtype)
{
if (qtype == 1)
sql = "SELECT * FROM emps WHERE empNo = " + s;
else
sql = "SELECT * FROM emps WHERE empLastName = " + s;
}

IF you're after something of that sort, it's probably a little cleaner than
overloading GetEmployeeList anyway ...
 
Yeah..

a switch/case statement on the enum type to call a different private method
makes things nice...and maintainable.


So hopefully the thread gave you options for a solution.
 
Ronald S. Cook said:
As I understand it, one can't overload a function when types/combinations
match.

E.g.

GetEmployeeList(string EmployeeNumber)
GetEmployeeList(string EmployeeLastName)

would not be able to be overloaded and you'd have to create a separate
function for one of them.

That's not what overloads are for.

Overloads are for default arguments mainly, sometimes also for handling the
same data represented with a different type.

All overloads should perform the same operation. In your example, there are
two different operations:

"Find the employee with the given employee number"
"Find all employees with the given last name"
This just seems like a cruddy workaround... to have most everything as
part of your one overloaded function, but then to have 1 or 2 stragglers
that have to be named separately.

Is this just life and I have to deal with it, or is there a better
approach?

The better approach is to not use overloads at all in this scenarion, then
everything is consistent.
 
You can use user defined types for your two string pipes by putting them
in objects, such as by making a class or struct.
Here is an example of both:

public class enumb
{
public string enumber;
public enumb(string st) {enumber=st;}
}
public struct enam { public string elstnam;}

public static void getemployeelist (enumb e)
{Console.WriteLine(e.enumber);}
public static void getemployeelist( enam e ) {
Console.WriteLine(e.elstnam); }

static void Main(string[] args)
{
enumb e1=new enumb("42");
enam e2= new enam();
e2.elstnam = "Smith";
getemployeelist(e1);
getemployeelist(e2);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top