John said:
Marc Gravell said:
Well, since you look at the values inside the method, you probably mean
"ref", in which case something like:
int? binMe = null, someValue = 0;
Func(ref binMe, ref someValue); // Any way to pass null for either arg?
Console.WriteLine(binMe);
Console.WriteLine(someValue);
static void Func(ref int? Param1, ref int? Param2) {
if (Param1 != null) Param1 = 7; // random
if (Param2 != null) Param2 = 5; // random
}
It *looks* like you are trying to decide which values to lookup (perhaps
from a DB); in which case, another option is some kind of bitmask, perhaps
in a [Flags] enum.
Thanks for the response. I'm not sure what the "?" in "int?" does however
(haven't seen this before but will look it up). In any event, passing bit
flags is inconvenient
In the general case, when one has a function that
returns multiple out arguments and a client is only interested in a subset
of them, the ability to simply pass "null" for the others is very convenient
(and saves you from having to declare variables unnecessarily).
The short answer: no.
However, there is a way to use Marc's bit masks but not pollute your
client code. It involves writing a lot of overloads, and won't work if
several (or all) of your arguments are the same type, but what about
this:
[Flags]
private enum ReturnType = {
ReturnCustomer = 0x01,
ReturnSupplier = 0x02,
...
};
private void Fetch(ReturnType whatToReturn, out Customer cust, out
Supplier supp)
{
if ((whatToReturn & ReturnType.ReturnCustomer) != 0)
{
cust = ... ;
}
if ((whatToReturn & ReturnType.ReturnSupplier) != 0)
{
supp = ... ;
}
}
public void Fetch(out Customer cust, out Supplier supp)
{
Fetch(ReturnType.Customer | ReturnType.Supplier, out cust, out
supp);
}
public void Fetch(out Customer cust)
{
Supplier dummySupplier;
Fetch(ReturnType.Customer, out cust, out dummySupplier);
}
public void Fetch(out Supplier supp)
{
Customer dummyCustomer;
Fetch(ReturnType.Supplier, out dummyCustomer, out supp);
}
As I said, it doesn't work for all scenarios, but it does hide the
declaration of unnecessary variables inside overloaded methods.