Click here to Skip to main content

The Weird and The Wonderful

   

The Weird and The Wonderful forum is a place to post Coding Horrors, Worst Practices, and the occasional flash of brilliance.

We all come across code that simply boggles the mind. Lazy kludges, embarrasing mistakes, horrid workarounds and developers just not quite getting it. And then somedays we come across - or write - the truly sublime.

Post your Best, your worst, and your most interesting. But please - no programming questions . This forum is purely for amusement and discussions on code snippets. All actual programming questions will be removed.

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralPass by value and pass by referencememberSherin Iranimose17 Jul '08 - 6:49 

namespace SampleCSharp
{
public class MyClass
{
public int myVar;
}
 

public class SampleClass
{
private void SampleClass_Load(object sender, EventArgs e)
{
MyClass objMyClass = new MyClass();
objMyClass.myVar = 10;
ChangeMyVar(objMyClass);//IS THIS PASS BY VALUE OR PASS BY REFERENCE

}
public void ChangeMyVar(MyClass objMyClass)
{
objMyClass.myVar = 30;
}
}
}

 
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.

GeneralRe: Pass by value and pass by reference PinmemberSherin Iranimose17 Jul '08 - 18:23 
After the function call its printing 30, That is the matter Smile | :)
 
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE.
 
How to post a question

GeneralRe: Pass by value and pass by reference Pinmemberdarkelv17 Jul '08 - 20:08 
Sherin Iranimose wrote:
After the function call its printing 30

 
So, that's the horror part? *look at forum title* *Disappointed*
GeneralRe: Pass by value and pass by reference PinmemberPaul Conrad18 Jul '08 - 2:55 
For some reason, I sense you are asking a programming question. That is a big NO in this particular forum.
 
"The clue train passed his station without stopping." - John Simmons / outlaw programmer
 
"Real programmers just throw a bunch of 1s and 0s at the computer to see what sticks" - Pete O'Hanlon


GeneralRe: Pass by value and pass by reference PinmemberV.17 Jul '08 - 22:17 
this looks like a programming question.
On top of this forum it's said in big bold red letters:
 
Do not post programming questions in this forum...
 
If it is a question, move it to the C# forum. Btw: objects (including strings) are passed by reference to my knowledge. You should be able to find this on msdn somewhere.
 
V.

Stop smoking so you can: Enjoy longer the money you save.
Moviereview Archive

GeneralRe: Pass by value and pass by reference PinmvpCPallini17 Jul '08 - 23:09 
V. wrote:
objects (including strings) are passed by reference to my knowledge

 
Nope. All parameters are passed by value. you should use the ref keyword to pass a parameter by reference. BTW: passing by value an object implies that called function can actually change object's internal state.
Smile | :)
 
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.

This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke


[My articles]

GeneralRe: Pass by value and pass by reference PinmemberJoe Woodbury19 Jul '08 - 9:12 
This is incorrect. Classes are passed by reference, Structs are passed by value.
 
CPallini wrote:
BTW: passing by value an object implies that called function can actually change object's internal state.
[Smile]

 
This means nothing of the sort. Passing by value simply means that an object in a method is distinct from the original object and any changes made to that object are not reflected in the original.
 

Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke


GeneralRe: Pass by value and pass by reference PinmvpCPallini20 Jul '08 - 3:56 
You are wrong. Whenever you pass an object, a reference to the object's instance is passed by value.
Smile | :)
 
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.

This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke


[My articles]

GeneralRe: Pass by value and pass by reference PinmemberJoe Woodbury19 Jul '08 - 9:07 
This appears to be C# code. Classes are passed by reference by design.
 

Anyone who thinks he has a better idea of what's good for people than people do is a swine.
- P.J. O'Rourke


GeneralRe: Pass by value and pass by reference PinmemberKarstenK21 Jul '08 - 2:42 
I guess it gets passed by reference because it is C#. Roll eyes | :rolleyes:
In C++ by value.
 
Greetings from Germany

AnswerRe: Pass by value and pass by reference PinmemberMitendra Anand21 Jul '08 - 20:58 
Value-type objects such as structs are created on the stack, while reference-type objects such as classes are created on the heap.
 
So it is actually a PASS - BY - REFERENCE
 
Both types of objects are destroyed automatically, but objects based on value types are destroyed when they go out of scope, whereas objects based on reference types are destroyed at an unspecified time after the last reference to them is removed.
 
Happy Coding!
Mitendra
GeneralRe: Pass by value and pass by reference Pinmemberjon_17523 Jul '08 - 12:00 
The object is passed by value, but the what is actually passed since it is a reference object is a pointer to the object. So, the "value" that is actually passed is the pointer not the object and therefore any changes made through the pointer change the original object. What you can't change is the pointer itself. That is the "value". Smile | :)

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


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 24 May 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid