views:

277

answers:

7

I'm familiar with using out to pass in a simple data type for manipulation, but I can't seem to figure out how to pass in this Queue<> without causing a compile error. Any ideas?

Code:

Queue<SqlCommand> insertScriptQueue = new Queue<SqlCommand>();

private void UpdateDefaultIndicator(int newDefaultViewID,
                                    out (Queue<SqlCommand>) insertScriptQueue)

UpdateDefaultIndicator(newViewID, out (Queue<SqlCommand>)insertScriptQueue);
+4  A: 

You shouldn't be initializing an out variable. If you need to modify an in-scope variable, use ref instead.

As Ed points out in his comment, "modify" may not give you the full idea of what's happening here - an out parameter of a reference type will by definition be an initialized object at the end of the function call. As most other answers have pointed out, if you want to pass in an initialized object, ref is the stronger choice.

Mike Burton
You really mean "if you need to modify a variable that is a value type or assign a new object to the parameter for a reference type"
Ed Swangren
I checked the docs, it appears it actually is ok to initialize an out variable; presumably the reference to the old value is discarded when the function returns a new one.
Bruce
+3  A: 
Queue<SqlCommand> insertScriptQueue;

private void UpdateDefaultIndicator(int newDefaultViewID,
                                out Queue<SqlCommand> insertScriptQueue){/*body*/}

UpdateDefaultIndicator(newViewID,out insertScriptQueue);

That works fine for me... What error are you getting?

Erich
+3  A: 

Why do you want a "out" ...here...why dont you return the type instead ? Let method return Queue<> insteasd of void..will that work for you?

Shankar Ramachandran
+5  A: 

You're passing in a reference type. No need to use out.

Gurdas Nijor
What does `out` have to do with passing a reference type? The `out` modifier simply allows you to use an uninitialised variable (which can be a value-type or ref-type) and ensures that it is initialised in the method itself.
LukeH
Okay; no need to use out OR ref in this particular context. He's initialized his variable already before passing it in.
Gurdas Nijor
A: 

make sure that you are assigning insertScriptQueue some kind of value inside of the UpdateDefaultIndicator method

jlech
+2  A: 

The Queue is going to be passed by reference anyway, its not a value type. Just don't use 'out'. UPDATE: Pardon me, I was thinking of 'ref' - but the fact that you're passing a Queue data type in, and not just an unallocated reference, makes me think that you want to be using 'ref' anyway. Except of course that you don't need to use 'ref' because the Queue isn't a value type; its already going to be passed in 'by reference', by default.

Bruce
It's not going to be passed "by reference" - the reference will be passed by value. It's a subtle difference, but an important one. "ref" and "out" can still be important with reference types. See http://pobox.com/~skeet/csharp/parameters.html
Jon Skeet
Thanks! Excellent detail. So while Queue is a reference type, the necessity for a 'ref' parameter depends on whether or not he sets that parameter to a different Queue object in his function, with the expectation of the caller seeing the new object.
Bruce
A: 

The answer to the original question, by the way, is that you're casting to Queue, and the cast is returning an interim reference. That reference is not assignable, and so is not a legal out parameter. Erich's code implements the fix for this problem.

Mike Burton