tags:

views:

258

answers:

9

Basically like using the out keyword. But the problem is with the out keyword, you still have to declare a variable outside the method call, like:

double result;
Double.TryParse ( "76", out result );

where as I want something like:

Double.TryParse ( "76", out result );

and there I get a new variable called result.

Is there a way to do this (by using out or not)?

+2  A: 

No, you have to declare the variable first because you are passing it to the method, the only difference is that you are explicitly requiring the method to fill it out before returning.

Otávio Décio
A: 

What's the problem with declaring it before the call?

frou
Basically I don't want lots of variables laying around just to be able to pass it to a method. It becomes more apparent when you use method that has 5, 6 out paramaters.
Joan Venge
What do you mean by "laying around"? If you "use method that has 5, 6 out paramaters" then you must pass those variables, whether you "use" them or not. If you don't want them then don't pass them. I do not understand the problem.
Dour High Arch
It's simple. There are languages that supports multiple returns like C# but not necessarily requires you to pass uninitialized variables, as this is just routine work that doesn't do any good, unless you have an already initialized variables to pass which can still be done by those languages.
Joan Venge
But in the end you will still end with 5, 6 local variables. I think there isn't much difference. And, you don't have choice anyway since C# doesn't support this.
lacop
Yes, but this would prevent the variables to be present outside the call, separately declared. Either way the method call with passed arguments is gonna be present.
Joan Venge
Can anyone explain to me why you would ever want 5 out params? Seems like a smell.
John Sheehan
I still don't understand what you're asking; please post sample code "that supports multiple returns ... but not necessarily requires you to pass uninitialized variables".
Dour High Arch
A: 

Why are you trying to do this? There's no reason not to declare it before the method call, save the few keystrokes.

JoshBerke
Basically I don't want lots of variables laying around just to be able to pass it to a method. It becomes more apparent when you use method that has 5, 6 out paramaters.
Joan Venge
I can understand that but I don't know of a way to do what you want.
JoshBerke
A: 

Is there any case with a sence for your szenario?

In order to use the variable after the call to your function, you have to know the type of the variable. Otherwise you would ignore the type-safety of the compiler and could easily run into runtime-errors.

BeowulfOF
The compiler could infer the type the same way it does when using var
JoshBerke
The compiler can do type inferencing, the human reader might not be able to.
Rob
But it's the same issue you get by using var not sparingly.Also it could very well be something like this:Double.TryParse("76", out double result);
Joan Venge
+1  A: 

Also, you can "rethrow" it, that is, you pass it up the stack as an out again. For example:

public void MyMethod(out int MyParam)
{
    SomeMethod(out MyParam);
}
DrJokepu
A: 

variables live on the stack, not the heap, so the notion of calling a function to create new variables - while an interesting idea and not impossible in other languages - implies that you don't understand the difference between a variable and an object in C#.

  • a variable's storage is allocated on the stack, and the variable only exists for the duration of the execution of the function that declares the variable

this is distinct from a 'member variable' which is a field belonging to an object.

the 'out' keyword in C# means that a variable will be passed into the function 'by reference', and the function is expected to (required to) set the variable's value.

in general, 'out' parameters should be avoided; if you need to pass several values out of a function, they probably belong to a missing object!

EDIT: in C# an 'out' parameter is similar to a 'ref' parameter in that they are both call-by-reference parameters, as opposed to the more typical call-by-value parameter.

Steven A. Lowe
No you are wrong. There are languages that can return multiple values without explicitly declaring them outside the function.
Joan Venge
Surely this is just syntactical sugar, which is what I am asking here.
Joan Venge
@[Joan Venge]: please read more carefully. There may be such languages, but C# is not one of them. And no, it isn't syntactical sugar, it's fundamental to the memory-management model. Variables live on the stack.
Steven A. Lowe
@[Joan Venge]: and don't call me Shirley ;-)
Steven A. Lowe
Steven, whether C# does this or not is what I am asking here. It's clear that it doesn't have support for it, but this kind of feature can be implemented as a syntactical sugar, instead of completely changing the memory model of the language.
Joan Venge
@[Joan Venge]: now I must call BS on that statement. There is simply no way in C# to create a function that 'returns' a local variable. Perhaps there's a language barrier in operation here, but what you seem to be asking for makes no sense. If you just want to get rid of local vars use a struct
Steven A. Lowe
Shirley :), you are right, this feature doesn't exist. But what I was suggesting was that if C# designers wanted, they could easily implement this kind of functionality without changing C#'s memory model.
Joan Venge
@[Joan Venge]: there is no way to make a function that creates local variables without radically altering the language. If you mean 'make a function return multiple values' that is trivial - just return an array.
Steven A. Lowe
A: 

No, you have to wrap it in another method that declares a local variable and then returns it.

With the specific example you gave, you can just use:

Double d = Double.Parse( "76" );
bozag
Yes but the problem isn't with this method in particular but the ability to do multiple returns without declaring variables separately.
Joan Venge
+1  A: 

You can use C# tuples to compose multiple variables into one.

Rinat Abdullin
Nice shameless plug there. I'd vote you up if I could understand what you were saying in that page, though.
TraumaPony
+1  A: 

A variable must be declared before it is used and a method with more than a few parameters gets smelly quick. Output parameters especially.

I think what you want to do is declare a class that contains your five or six variables and pass that as the argument to the method:

class Args
{
  public int One { get; set; }
  public int Two { get; set; }
  public int Three { get; set; }
  public int Four { get; set; }
  public int Five { get; set; }
}

Then a method taking it as an argument:

public void Foo(Args args)
{
  // Modify members of args here.
}

And use it like this:

Args args = new Args();

Foo(args);

// Do stuff with results in args.
Andrew Kennan
Thanks but it's the 3rd party libraries I use that have sometimes many out parameters, otherwise I wouldn't use out that much.
Joan Venge