Click here to Skip to main content
16,019,427 members
Home / Discussions / C#
   

C#

 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
User987435-Jan-18 13:18
professionalUser987435-Jan-18 13:18 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff5-Jan-18 21:13
professionalBillWoodruff5-Jan-18 21:13 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton6-Jan-18 4:14
mvaMarc Clifton6-Jan-18 4:14 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff6-Jan-18 7:40
professionalBillWoodruff6-Jan-18 7:40 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton8-Jan-18 3:03
mvaMarc Clifton8-Jan-18 3:03 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
BillWoodruff8-Jan-18 5:18
professionalBillWoodruff8-Jan-18 5:18 
GeneralRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
User9874315-Jan-18 4:10
professionalUser9874315-Jan-18 4:10 
AnswerRe: Is it wrong to pass an object into a method, change something and then pass it back to the caller? Pin
Marc Clifton6-Jan-18 3:56
mvaMarc Clifton6-Jan-18 3:56 
Except for method chaining, why would pass the same object back to the caller? Since the object is passed by reference, the caller's reference is updated. So, in your example:
SomeObj myobj = new SomeObj ( someparam = "Some Value" );
myobj = DoSomething( myobj );

the assignment is unnecessary and potentially confusing -- the reader of the code might wonder if myobj is not the same instance that is returned from the call DoSomething in which you passed in myobj

Of course, if SomeObj is struct, then it's passed by value, and yeah, you have to return the "value" for the caller to see the change.

I must say, I'm amused by all this because your question hit the bullseye of how messy method calling can be.

As a digression, think of higher level method as a workflow:
var stuffWorkflowNeedsToKnow = InitializeStuff();
DoStep1(stuffWorkflowNeedsToKnow);
DoStep2(stuffWorkflowNeedsToKnow);
DoStep3(stuffWorkflowNeedsToKnow);

And a further digression: Class-less Coding - Minimalist C# and Why F# and Function Programming Has Some Advantages
Latest Article - Code Review - What You Can Learn From a Single Line of Code

Learning to code with python is like learning to swim with those little arm floaties. It gives you undeserved confidence and will eventually drown you. - DangerBunny

Artificial intelligence is the only remedy for natural stupidity. - CDP1802

QuestionC# Progammer Forum for Beginners? Pin
User987432-Jan-18 21:27
professionalUser987432-Jan-18 21:27 
AnswerRe: C# Progammer Forum for Beginners? Pin
Ralf Meier2-Jan-18 21:55
mveRalf Meier2-Jan-18 21:55 
AnswerRe: C# Progammer Forum for Beginners? Pin
OriginalGriff2-Jan-18 22:09
mveOriginalGriff2-Jan-18 22:09 
GeneralRe: C# Progammer Forum for Beginners? Pin
Pete O'Hanlon2-Jan-18 22:15
mvePete O'Hanlon2-Jan-18 22:15 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff2-Jan-18 22:36
mveOriginalGriff2-Jan-18 22:36 
GeneralRe: C# Progammer Forum for Beginners? Pin
Pete O'Hanlon2-Jan-18 22:43
mvePete O'Hanlon2-Jan-18 22:43 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff2-Jan-18 22:49
mveOriginalGriff2-Jan-18 22:49 
GeneralRe: C# Progammer Forum for Beginners? Pin
Dave Kreskowiak3-Jan-18 2:23
mveDave Kreskowiak3-Jan-18 2:23 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff3-Jan-18 2:33
mveOriginalGriff3-Jan-18 2:33 
GeneralRe: C# Progammer Forum for Beginners? Pin
Dave Kreskowiak3-Jan-18 4:18
mveDave Kreskowiak3-Jan-18 4:18 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff3-Jan-18 4:46
mveOriginalGriff3-Jan-18 4:46 
GeneralRe: C# Progammer Forum for Beginners? Pin
Dave Kreskowiak3-Jan-18 4:56
mveDave Kreskowiak3-Jan-18 4:56 
GeneralRe: C# Progammer Forum for Beginners? Pin
User987432-Jan-18 22:36
professionalUser987432-Jan-18 22:36 
GeneralRe: C# Progammer Forum for Beginners? Pin
Pete O'Hanlon2-Jan-18 22:40
mvePete O'Hanlon2-Jan-18 22:40 
GeneralRe: C# Progammer Forum for Beginners? Pin
User987432-Jan-18 23:09
professionalUser987432-Jan-18 23:09 
GeneralRe: C# Progammer Forum for Beginners? Pin
User987432-Jan-18 22:59
professionalUser987432-Jan-18 22:59 
GeneralRe: C# Progammer Forum for Beginners? Pin
OriginalGriff2-Jan-18 23:25
mveOriginalGriff2-Jan-18 23:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.