Overloading with same param types

  • Thread starter Thread starter Brian
  • Start date Start date
B

Brian

Does anyone have a good way to handle the following situation...

public void FilterResults(int a, int b)

I need to filter based on none, one, or both parameters, so they both
need to be "optional". Overloading is eliminated as a possible
solution because both parameters are the same type. Params is
possible, but it doesn't seem like I can keep track of which params are
passed in (which is which)...Any ideas?

Thanks, Brian
 
You could created a struct for each of the parameters that only contains
the single named int value. Then create overloads of the routines that
accept the structs by name. It causes a bit of overhead to setup for
the call and to deal with it but it would give you a lightweight way to
get the functionality you need.

Have A Better One!

John M Deal, MCP
Necessity Software
 
I usually abandon overloading at this point and simply change the name:

public void FilterResults()

public void FilterResultsOnA(int a)

public void FilterResultsOnB(int b)

public void FilterResults(int a, int b)

At least this way the method name tells the reader what the call is
doing.
 
I usually abandon overloading at this point and simply change the name:

public void FilterResults()

public void FilterResultsOnA(int a)

public void FilterResultsOnB(int b)

public void FilterResults(int a, int b)

At least this way the method name tells the reader what the call is
doing.
 
Hi,

I totally agree with you and I think that this is the best way of doing it.

Cheers,
 
Back
Top