473,396 Members | 1,846 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 an out parameter

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 a coordinator for several other classes. FooManager
basically passes its parameters, including an output parameter 'x', on to
FooWorker, a worker method in another class. FooWorker does the actual work
and assigns to x. When FooWorker is done, it returns to FooManager, which
returns to the caller. It looks like this:

public object FooManager(object a, object b, out object x)
{
... (do some work)...

object result = FooWorker(a, b, out x);
return result;
}

Here's my problem: The C# compiler is throwing this compile-time error: "The
out parameter 'dictionary' must be assigned to before control leaves the
current method." I understand what it's saying-- it wants me to do something
with op before I send it to FooWorker. And from that it sounds like I can't
pass out parameters along from coordinator objects to worker objects.

Is there any way to pass an out parameter in the manner I've described? I'm
stuck with using an out parameter, for reasons I won't go into. Besides, I'm
out of aspirin...

Thanks for your help.

--
Dave Veeneman
Chicago
Nov 15 '05 #1
8 13160
I think I can solve the problem by passing x as a 'ref' parameter, instead
of as an 'out' parameter. Any thoughts? Thanks.

--
Dave Veeneman
Chicago
Nov 15 '05 #2
Just an idea, if you have not tried it (also did you try
to use ref instead of out?)

public object FooManager(object a, object b, out object x)
{
... (do some work)...
object y;

object result = FooWorker(a, b, out y);
x = y;
return result;
}

-----Original Message-----
Can I pass a method pass one of its out parameters to another method? C# istelling me I can't.

Let's say I have two methods, FooManager and FooWorker. FooManager is partof a class that acts as a coordinator for several other classes. FooManagerbasically passes its parameters, including an output parameter 'x', on toFooWorker, a worker method in another class. FooWorker does the actual workand assigns to x. When FooWorker is done, it returns to FooManager, whichreturns to the caller. It looks like this:

public object FooManager(object a, object b, out object x) {
... (do some work)...

object result = FooWorker(a, b, out x);
return result;
}

Here's my problem: The C# compiler is throwing this compile-time error: "Theout parameter 'dictionary' must be assigned to before control leaves thecurrent method." I understand what it's saying-- it wants me to do somethingwith op before I send it to FooWorker. And from that it sounds like I can'tpass out parameters along from coordinator objects to worker objects.
Is there any way to pass an out parameter in the manner I've described? I'mstuck with using an out parameter, for reasons I won't go into. Besides, I'mout of aspirin...

Thanks for your help.

--
Dave Veeneman
Chicago
.

Nov 15 '05 #3
Dave Veeneman <da****@nospam.com> wrote:
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 a coordinator for several other classes. FooManager
basically passes its parameters, including an output parameter 'x', on to
FooWorker, a worker method in another class. FooWorker does the actual work
and assigns to x. When FooWorker is done, it returns to FooManager, which
returns to the caller. It looks like this:

public object FooManager(object a, object b, out object x)
{
... (do some work)...

object result = FooWorker(a, b, out x);
return result;
}

Here's my problem: The C# compiler is throwing this compile-time error: "The
out parameter 'dictionary' must be assigned to before control leaves the
current method." I understand what it's saying-- it wants me to do something
with op before I send it to FooWorker. And from that it sounds like I can't
pass out parameters along from coordinator objects to worker objects.


No, it's not saying that, but it's difficult to know *exactly* what
it's saying as none of the code that you've provided uses a variable
called "dictionary". Could you provide a short but *complete* example
which demonstrates the problem?

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

It works just fine. I think you misunderstand what the compiler told you.
The error means that you have to to give the *out* parameter value before
the excution leaves the method body. In your case that means that your
FooWorker method doesn't assign any value to x. You don't have to initialize
x before calling FooWorker. That is how *out* parameters works. And this is
one of the differences between *out* nad *ref* parameters. But you have to
give any *out* parameters value before method exits. That's why the compiler
won't tell you anything for FooManager because it knows that FooWorker will
give *x* value because *x* is passed as *out* parameter. So the problem is
in FooWorker. Check out FooWorker and make sure that you don't exit the body
before setting *x*. Of course it doesn't apply for the casses when you
terminate the method execution by throwing exception.

HTH
B\rgds
100

"Dave Veeneman" <da****@nospam.com> wrote in message
news:Oq**************@TK2MSFTNGP09.phx.gbl...
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 a coordinator for several other classes. FooManager basically passes its parameters, including an output parameter 'x', on to
FooWorker, a worker method in another class. FooWorker does the actual work and assigns to x. When FooWorker is done, it returns to FooManager, which
returns to the caller. It looks like this:

public object FooManager(object a, object b, out object x)
{
... (do some work)...

object result = FooWorker(a, b, out x);
return result;
}

Here's my problem: The C# compiler is throwing this compile-time error: "The out parameter 'dictionary' must be assigned to before control leaves the
current method." I understand what it's saying-- it wants me to do something with op before I send it to FooWorker. And from that it sounds like I can't pass out parameters along from coordinator objects to worker objects.

Is there any way to pass an out parameter in the manner I've described? I'm stuck with using an out parameter, for reasons I won't go into. Besides, I'm out of aspirin...

Thanks for your help.

--
Dave Veeneman
Chicago

Nov 15 '05 #5
The actual code was pretty complex-- It involved a collection manager
calling a factory method that returned an object for the collection. The
factory method also needed to update an object dictionary, which was passed
through several levels of the hierarchy. I figured nobody would want to wade
through all that stuff, so I tried to abstract away as much of the detail
from my question as I could.

In any event, switching from an 'out' parameter to a 'ref' parameter solved
the problem nicely. Thank you for your help.

--
Dave Veeneman
Chicago
Nov 15 '05 #6
Dave Veeneman <da****@nospam.com> wrote:
The actual code was pretty complex-- It involved a collection manager
calling a factory method that returned an object for the collection. The
factory method also needed to update an object dictionary, which was passed
through several levels of the hierarchy. I figured nobody would want to wade
through all that stuff, so I tried to abstract away as much of the detail
from my question as I could.

In any event, switching from an 'out' parameter to a 'ref' parameter solved
the problem nicely. Thank you for your help.


It may have solved the compilation problem, but it *does* mean that
there's a return path which could return without the value being set -
this is likely to be a potential bug, and it's worth tracking it down.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #7
The following code compiles without error for me. I
would guess that the problem you are having is that
somewhere else in your elided FooManager code you have an
early return from the routine where x has not been
definitely set.

class Test {
public object FooManager(object a, object b, out object
x) {
object result=FooWorker(a, b, out x);
return result;
}

public object FooWorker(object a, object b, out object
x) {
x=3;
return "hello";
}

static void Main(string[] args) {
}
}

-----Original Message-----
Can I pass a method pass one of its out parameters to another method? C# istelling me I can't.

Let's say I have two methods, FooManager and FooWorker. FooManager is partof a class that acts as a coordinator for several other classes. FooManagerbasically passes its parameters, including an output parameter 'x', on toFooWorker, a worker method in another class. FooWorker does the actual workand assigns to x. When FooWorker is done, it returns to FooManager, whichreturns to the caller. It looks like this:

public object FooManager(object a, object b, out object x) {
... (do some work)...

object result = FooWorker(a, b, out x);
return result;
}

Here's my problem: The C# compiler is throwing this compile-time error: "Theout parameter 'dictionary' must be assigned to before control leaves thecurrent method." I understand what it's saying-- it wants me to do somethingwith op before I send it to FooWorker. And from that it sounds like I can'tpass out parameters along from coordinator objects to worker objects.
Is there any way to pass an out parameter in the manner I've described? I'mstuck with using an out parameter, for reasons I won't go into. Besides, I'mout of aspirin...

Thanks for your help.

--
Dave Veeneman
Chicago
.

Nov 15 '05 #8
100
Hi,
The other possibility for the error is that you give *x* value inside *if*
statement, in a *case* clause or inside a loop.
For example

public object FooWorker(object a, object b, out object x)
{
if(someCondition)
{
x = ....
}
-- OR --
siwtch(...)
{
case ... :
break;
case ... :
x = ...
break
......
}

--- OR ---

for(int i = 0; i < 10; i++)
{
x = ....
}
.....
}
}

In all cases above you'll have the compiler reporting the error.

HTH
B\rgds
100

"Corey Kosak" <ko*******@yahoo.com> wrote in message
news:00****************************@phx.gbl...
The following code compiles without error for me. I
would guess that the problem you are having is that
somewhere else in your elided FooManager code you have an
early return from the routine where x has not been
definitely set.

class Test {
public object FooManager(object a, object b, out object
x) {
object result=FooWorker(a, b, out x);
return result;
}

public object FooWorker(object a, object b, out object
x) {
x=3;
return "hello";
}

static void Main(string[] args) {
}
}

-----Original Message-----
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 a coordinator for several other

classes. FooManager
basically passes its parameters, including an output

parameter 'x', on to
FooWorker, a worker method in another class. FooWorker

does the actual work
and assigns to x. When FooWorker is done, it returns to

FooManager, which
returns to the caller. It looks like this:

public object FooManager(object a, object b, out

object x)
{
... (do some work)...

object result = FooWorker(a, b, out x);
return result;
}

Here's my problem: The C# compiler is throwing this

compile-time error: "The
out parameter 'dictionary' must be assigned to before

control leaves the
current method." I understand what it's saying-- it

wants me to do something
with op before I send it to FooWorker. And from that it

sounds like I can't
pass out parameters along from coordinator objects to

worker objects.

Is there any way to pass an out parameter in the manner

I've described? I'm
stuck with using an out parameter, for reasons I won't

go into. Besides, I'm
out of aspirin...

Thanks for your help.

--
Dave Veeneman
Chicago
.

Nov 15 '05 #9

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

Similar topics

3
by: WGW | last post by:
Though I am a novice to MS SQL server (2000 I believe), I can do almost! everything I need. Maybe not efficiently, but usefully. However, I have a problem -- a complex query problem... I can...
5
by: Andy | last post by:
Hi Could someone clarify for me the method parameter passing concept? As I understand it, if you pass a variable without the "ref" syntax then it gets passed as a copy. If you pass a...
6
by: wiredless | last post by:
What is the advantage of passing an interface type? according to UML (visio) when i reverse engineer existing code to a UML diagram I get the error "UMLE00046: sample : - An interface cannot...
11
by: John Pass | last post by:
Hi, In the attached example, I do understand that the references are not changed if an array is passed by Val. What I do not understand is the result of line 99 (If one can find this by line...
2
by: diffuser78 | last post by:
I wrote a small app in wxPython using wxGlade as designer tool. wxGlade brings and writes a lot of code by itself and thats where my confusion started. Its about parameter passing. Here is my...
10
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication...
0
by: amazon | last post by:
I have web service that acceping following parameters.. postev.PostEvent(authentication as ws.authentication, name as string,id as string, exdate as date, parameter() as ws.nameparametervaluepair...
7
by: TS | last post by:
I was under the assumption that if you pass an object as a param to a method and inside that method this object is changed, the object will stay changed when returned from the method because the...
12
by: dave_dp | last post by:
Hi, I have just started learning C++ language.. I've read much even tried to understand the way standard says but still can't get the grasp of that concept. When parameters are passed/returned...
4
by: Deckarep | last post by:
Hello fellow C# programmers, This question is more about general practice and convention so here goes: I got into a discussion with a co-worker who insisted that as a general practice all...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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.