473,978 Members | 30,061 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(obje ct 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 13187
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(obje ct 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(obje ct 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(obje ct 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.co m>
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******** ******@TK2MSFTN GP09.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(obje ct 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.co m>
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(obje ct a, object b, out object
x) {
object result=FooWorke r(a, b, out x);
return result;
}

public object FooWorker(objec t 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(obje ct 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(objec t a, object b, out object x)
{
if(someConditio n)
{
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*******@yaho o.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(obje ct a, object b, out object
x) {
object result=FooWorke r(a, b, out x);
return result;
}

public object FooWorker(objec t 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(obje ct 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
16956
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 create a parameter query in a stored procedure, but how do I use the result set of a parameter query in a select query (in the same or another sp)? In short, if a select query contains a result table that is generated as a parameter query, how do I...
5
36438
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 variable with the "ref" syntax then it gets passed as a reference to the object and any changes to
6
16292
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 be used as the type of a parameter." for this code:
11
8159
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 number) which is the last line of the following sub routine: ' procedure modifies elements of array and assigns ' new reference (note ByVal) Sub FirstDouble(ByVal array As Integer()) Dim i As Integer
2
1585
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 little confusion. ***************CODE BEGINS************************************ class MyFrame1(wx.Frame): def __init__(self, *args, **kwds): ..... ..... def OnEdit(self, event):
10
3949
by: amazon | last post by:
Our vender provided us a web service: 1xyztest.xsd file... ------------------------------------ postEvent PostEventRequest ------------------------------------- authetication authentication eventname string source string ID string
0
1263
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 (I need help in passing this)) Postev.postEvent(auth(), "Test", "1234567", tdt, parameter()) For the first parameter authentication I have: Private Function auth() As ws.Authentication
7
3324
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 object is a reference type? my code is not proving that. I have a web project i created from a web service that is my object: public class ExcelService : SoapHttpClientProtocol {
12
2618
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 by value temporaries are created?(I'm not touching yet the cases where standard allows optimizations from the side of implementations to avoid copying) If so, please quote part of the standard that says that. Assuming it is true, I can imagine two...
4
2823
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 objects should be passed by reference using the ref keyword generally speaking because as the writer of code you are conveying your intentions that an Object should/can be modified by your function.
0
10172
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11823
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11413
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10085
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8464
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7617
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6561
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
5160
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.