473,396 Members | 1,859 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,396 software developers and data experts.

Passing null as an "out" parameter

Hi there,

Is it possible to pass null to a function taking an "out" (or "ref")
parameter in C#. I'd like to do something like the following (which doesn't
compile of course). Thanks in advance.

int SomeValue;
Func(null, SomeValue); // Any way to pass null for either arg?

void Func(out int Param1, out int Param2)
{
if (Param1 != null)
{
Param1 = Whatever1();
}

if (Param2 != null)
{
Param2 = Whatever2();
}
}
Aug 23 '06 #1
7 14581
I don't think this is an out vs. ref issue...

Your code won't compile in C# 1.x because null is only an acceptable
value for reference types (ones based on a class, rather than anything
like int, float, or a struct). I'm not overly familiar with the C# 2.0
compiler, but I believe it'll let you have nullable value types if you
append ? to the type name. Does your code work if you use "int?"
rather than "int" ?

Eq.
Aug 23 '06 #2
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.
Aug 23 '06 #3
I don't think this is an out vs. ref issue...
>
Your code won't compile in C# 1.x because null is only an acceptable value
for reference types (ones based on a class, rather than anything like int,
float, or a struct). I'm not overly familiar with the C# 2.0 compiler, but
I believe it'll let you have nullable value types if you append ? to the
type name. Does your code work if you use "int?" rather than "int" ?
Thanks for the response. Perhaps it was a bad example then since the args
will be reference types in practice (I'm coming from the C++ world where no
such issues exist). If they are reference types then is this doable? Thanks
again.
Aug 23 '06 #4
"Marc Gravell" <ma**********@gmail.comwrote in message
news:ux**************@TK2MSFTNGP05.phx.gbl...
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).
Aug 23 '06 #5

John Layton wrote:
"Marc Gravell" <ma**********@gmail.comwrote in message
news:ux**************@TK2MSFTNGP05.phx.gbl...
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.

Aug 23 '06 #6
Thanks for the suggestion. It looks like I have little choice but it's a
pain IMO :) Anyway, thanks again.
Aug 23 '06 #7
"int?" is a C# shortcut for the 2.0 generic (immutable) struct
Nullable<int>; Nullable<T(in general) sports a struct T and a bool
(.HasValue) to indicate if it has a value or should be considered null. Note
that since it is a struct it never really *is* null (in the object sense),
but that the compiler infers null equality via the .HasValue property.

Note, however, that you could achieve a similar effect in 1.1 using "magic
numbers", such as int.MinValue to mean "ignore this".

To be honest, however, I'm not really loving this whole approach anyway - it
seems (more than) a bit unsightly; perhaps I'd go for something a whole lot
simpler (ignore names):

public class SomethingResult { // or struct, depending
public int SomeValue;
public float SomeOtherValue;
public bool SomethingElse;
}

[Flags]
pulic enum QueryOptions {
None = 0, SomeValue = 1, SomeOtherValue = 2, SomethingElse = 4, All = 7
}

public SomethingResult DoSomething(QueryOptions options) {
SomethingResult result = new SomethingResult();
if((options & QueryOptions.SomeValue) == QueryOptions.SomeValue)
result.SomeValue = // do something
if((options & QueryOptions.SomeOtherValue) == QueryOptions.SomeOtherValue)
result.SomeOtherValue= // do something
if((options & QueryOptions.SomethingElse ) == QueryOptions.SomethingElse)
result.SomethingElse = // do something
}

You can now just call DoSomething() specifying what you want, and then
inspect the returned object to get the values (only looking at the ones you
asked for; the rest will default to null/0/false). No hastle with ref / out;
no setting up 20 dummy parameters just because you want to read one value;
very friendly, and compatible with both 1.1 and 2.0 runtimes.

Marc

Aug 24 '06 #8

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

Similar topics

1
by: Jim | last post by:
I have a class with several public member variables which I pass to a library function as an "out" parameter. I noticed that when the function returned, all the data was "lost". Do classes used...
8
by: Dave Veeneman | last post by:
Can I pass a method pass one of its out parameters to another method? C# is telling me I can't. Let's say I have two methods, FooManager and FooWorker. FooManager is part of a class that acts as...
1
by: mei xiao | last post by:
Hi, In my web service method (written in C#), I have a method like public string myMethod(out int test1, string test1) but when I use a web client to access this method, I got a run time...
1
by: Alex Chan | last post by:
Hi Group, I have written a window service wtih SAP.NET Connector which is to fulfil request from SAP client. SAP client will call a function exposed by my window service that has a big "out"...
14
by: Pollux | last post by:
I'm having a problem with something I thought was quite simple. I have a function in a C DLL. Let's call it SomeFunction. This is the prototype for SomeFunction: void SomeFunction(char *...
5
by: web1110 | last post by:
This code public void ReturnArray(out string strArray) { string theArray=new string{"aaa", "bbb", "ccc"}; try { strArray=theArray; }
11
by: dahuzizyd | last post by:
Hi all: I think I had a problem with using out parameter , why the instance of 'SubClass' can't convert to 'BaseClass' ? my code is : ---------------------------------------------- using System;...
5
by: Hendri Adriaens | last post by:
Hi, I want to write a method to read the first column from a textfile and output them in 2 arrays to the main program. I don't know the length of the column beforehand. I use out to avoid...
6
kaleeswaran
by: kaleeswaran | last post by:
hi! i am new to myql procedure.. i can get the output value from the procdure if i use select option..but i could't get the value while i am using the out parameter.it shows null.. when i try...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.