473,399 Members | 2,146 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,399 software developers and data experts.

Optional (or null) output parameters

I'm looking for a way to simulate the following kind of C code:

--- C++ ---
void F( double * array, double * fMean, double * fMedian, double *
fMax, double * fMin );

--- C# ---
void F( double[] array, out double fMean, out double fMedian, out
double fMax, out double fMin );

I've read some of the examples on using overrides to simulate default
values, but that's not exactly what I'm doing here. On the client
side, NULL would be specified to indicate that (for example) mean and
median aren't needed, but max and min are.

Now, I can certainly use use overloads to have 2^4 -1 versions of this
function. This would provide a maximally efficient way to calculate
all these statistics; however, I don't need that, and the code
duplication would kill me. Furthermore, the function above is just an
example of the concept, the real code is more complex, of course.

I could also pass a boolean next to each out parameter indicating
whether to calculate it or not.

I could also pass a structure, etc...

I was kinda hoping I could pass in null for one of those output
parameters, but then I realized there wouldn't be any way to check on
the other side.

I could also declare all the parameters as objects, pass in nulls for
the ones I don't want, unbox the ones which were requested.

Any other options? Which one of the above makes the most sense?

Many Thanks,
-ken
Nov 15 '05 #1
4 2910
Ken,
I could also declare all the parameters as objects, pass in nulls for
the ones I don't want, unbox the ones which were requested.
I would consider using a single Parameter Object. Either an actual object
with multiple fields (Mean, Median, Max, Min, WantMean, WantMedian, WantMax,
WantMin) or an extra enum flags parameter that indicates which values I am
interested in.

Something like:
[Flags]
enum StatsWanted
{
Mean = 1,
Median = 2,
Max = 4,
Min = 8
};

void F( double[] array, StatsWanted statsWanted,
out double fMean, out double fMedian,
out double fMax, out double fMin );

Then when I called it I would include the appropriate bits for what I was
interested in.

Hope this helps
Jay

"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om... I'm looking for a way to simulate the following kind of C code:

--- C++ ---
void F( double * array, double * fMean, double * fMedian, double *
fMax, double * fMin );

--- C# ---
void F( double[] array, out double fMean, out double fMedian, out
double fMax, out double fMin );

I've read some of the examples on using overrides to simulate default
values, but that's not exactly what I'm doing here. On the client
side, NULL would be specified to indicate that (for example) mean and
median aren't needed, but max and min are.

Now, I can certainly use use overloads to have 2^4 -1 versions of this
function. This would provide a maximally efficient way to calculate
all these statistics; however, I don't need that, and the code
duplication would kill me. Furthermore, the function above is just an
example of the concept, the real code is more complex, of course.

I could also pass a boolean next to each out parameter indicating
whether to calculate it or not.

I could also pass a structure, etc...

I was kinda hoping I could pass in null for one of those output
parameters, but then I realized there wouldn't be any way to check on
the other side.

I could also declare all the parameters as objects, pass in nulls for
the ones I don't want, unbox the ones which were requested.

Any other options? Which one of the above makes the most sense?

Many Thanks,
-ken

Nov 15 '05 #2
Hi,
you do not have to use out or ref if all your arguments are reference type.
In C# all reference types are passed by reference by default.
So in the caller you have to declare some objects and assign them values.
For some of the objects you may assign null.
And in your code you may check for null, change the values, etc.
And no overolads :)

I mean:

void caller()
{
int[] res, par1, par2, par3, par4;

par1 = new int[10];
// fill values
par2 = new int[5];
par3 = null;
par4 = null;
res = null;

myfunc(res, par1, par2, par, par4);
if (res != null)
//display results
{}
}

void myfunc(int[] r, int[] a1, int[] a2, int[] a3, int[] a4)
{
if ((a1 != null) && (a2 != null))
{
r = new int[15];
// do calcs
}
else
{
r = new int[10];
//other calcs, or check for a3 and a4, etc...
}
}

home that helps.
Sunny

P.S. watch out for typos, I've write directly in the reader, haven't tested,
but it shoul work that way.

"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
I'm looking for a way to simulate the following kind of C code:

--- C++ ---
void F( double * array, double * fMean, double * fMedian, double *
fMax, double * fMin );

--- C# ---
void F( double[] array, out double fMean, out double fMedian, out
double fMax, out double fMin );

I've read some of the examples on using overrides to simulate default
values, but that's not exactly what I'm doing here. On the client
side, NULL would be specified to indicate that (for example) mean and
median aren't needed, but max and min are.

Now, I can certainly use use overloads to have 2^4 -1 versions of this
function. This would provide a maximally efficient way to calculate
all these statistics; however, I don't need that, and the code
duplication would kill me. Furthermore, the function above is just an
example of the concept, the real code is more complex, of course.

I could also pass a boolean next to each out parameter indicating
whether to calculate it or not.

I could also pass a structure, etc...

I was kinda hoping I could pass in null for one of those output
parameters, but then I realized there wouldn't be any way to check on
the other side.

I could also declare all the parameters as objects, pass in nulls for
the ones I don't want, unbox the ones which were requested.

Any other options? Which one of the above makes the most sense?

Many Thanks,
-ken


Nov 15 '05 #3
Unfortunately, fundamental types like double are value types and hence
cannot be null. There are several ways round it.

1) Pass the doubles as objects. An object can be null, or you can pass a
double which will be boxed into the object. On the downside this is messy
and not type safe.

2) Create a class (hence a reference type) that encapsulates a double (a bit
like the Java class Double). This will give you type safety, and the ability
to pass a null object.

Those are the most direct translations of the C++. Personally I'd return a
structure and pass flags to say what needs to be calculated.

Regards,

Jasper Kent.
"Ken Durden" <cr*************@hotmail.com> wrote in message
news:18**************************@posting.google.c om...
I'm looking for a way to simulate the following kind of C code:

--- C++ ---
void F( double * array, double * fMean, double * fMedian, double *
fMax, double * fMin );

--- C# ---
void F( double[] array, out double fMean, out double fMedian, out
double fMax, out double fMin );

I've read some of the examples on using overrides to simulate default
values, but that's not exactly what I'm doing here. On the client
side, NULL would be specified to indicate that (for example) mean and
median aren't needed, but max and min are.

Now, I can certainly use use overloads to have 2^4 -1 versions of this
function. This would provide a maximally efficient way to calculate
all these statistics; however, I don't need that, and the code
duplication would kill me. Furthermore, the function above is just an
example of the concept, the real code is more complex, of course.

I could also pass a boolean next to each out parameter indicating
whether to calculate it or not.

I could also pass a structure, etc...

I was kinda hoping I could pass in null for one of those output
parameters, but then I realized there wouldn't be any way to check on
the other side.

I could also declare all the parameters as objects, pass in nulls for
the ones I don't want, unbox the ones which were requested.

Any other options? Which one of the above makes the most sense?

Many Thanks,
-ken

Nov 15 '05 #4
Sunny <su******@icebergwireless.com> wrote:
you do not have to use out or ref if all your arguments are
reference type.
You do sometimes.
In C# all reference types are passed by reference by default.


No they're not. There's a difference between passing a reference by
value and passing a value by reference. (Or a reference by reference.)

Calling the default C# reference type parameter passing mechanism "pass
by reference" is just wrong, and can be a source of confusion.

See http://www.pobox.com/~skeet/csharp/parameters.html

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

1
by: Bari Allen | last post by:
I have a Stored procedure in SQL, that works, when tested in SQL, with one input & several output parameters, as follows: CREATE PROCEDURE myProcedure @MyID int , @First varchar(80) OUTPUT ,...
12
by: Gus | last post by:
Hi, I´m triying to invoke a member using reflection, but this member requiere two parameters, the first patrameter is a string and the second is a reference to a dataset (output parameter). The...
2
by: Jim Owen | last post by:
The following code does not operate properly, and neither I nor the three ..Net experts can figure out why. It's a seeming mystery. The code is simple. A SqlCommand is executed against a stored...
4
by: Janaka | last post by:
Hi this is kind of an ASP.NET/ADO.NET question. I've got a stored procedure on SQL Server that returns a results set. It also sets 3 output parameters in a seperate Select statement. When...
1
by: Garth Wells | last post by:
Using an example in the Jan 2006 release of the Enterprise Library, I came up with the code shown below to create a DAL method for returning several columns of a single row. I place the output...
0
by: IamtheEvster | last post by:
Hi All, I am currently using PHP 5 and MySQL 5, both on Fedora Core 5. I am unable to call a MySQL stored procedure that returns output parameters using mysql, mysqli, or PDO. I'm having a...
1
by: Richard | last post by:
I have a typed dataset in asp.net 2.0, that returns 2 output parameters. Unfortunately I have not been able to capture these values: Private Sub GetValues() Dim ta As New...
5
by: CyberSoftHari | last post by:
I am trying to get an output Parameters value from a stored procedure using sqlDataSource in asp.net 2.0. But I only get a null value for the output Parameters. Can someone Point me to get value? ...
3
by: leesquare | last post by:
Hello, I need some help getting output values from my stored procedures when using adodbapi. There's an example testVariableReturningStoredProcedure in adodbapitest.py, and that works for my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.