views:

131

answers:

5

Hi,

I have very a basic question. I have a MethodB returning Integer. I have a MethodA where I want to pass the value retrieved from MethodB.

Is it a right way (the coding style, not the syntax) to pass MethodB to MethodA as mentioned below?

MethodA(MethodB());

Thanks.

+7  A: 

You are not passing the method 'MethodB' but the value that it returns.
In fact, your code is equivalent to:

int i = MethodB();
MethodA(i);

It's perfectly ok to do it, as long as your code remains readable.

Paolo Tedesco
+2  A: 

Yes - MethodB will be called first and return a value which will then be used as the parameter for the call to MethodA

barrylloyd
+4  A: 

Your code sample is completely valid. The returned value of MethodB is what will be passed into MethodA.

Ardman
+1  A: 

It's fine but reads poorly and you can't easily set a breakpoint on MethodB's return. I strongly prefer splitting it up:

 var bResult = MethodB();
 MethodA(bResult);
Jamie Ide
+1  A: 

Yes, it's okay. In fact I'd say it's a great idea, especially if the method names are appropriate.

displayName(getName());
Jesse J