Pass by value and pass by reference
-
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.
-
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.
by value.
This signature was proudly tested on animals.
-
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.
It's passed by the lower intestinal tract.
Deja View - the feeling that you've seen this post before.
-
It's passed by the lower intestinal tract.
Deja View - the feeling that you've seen this post before.
Ah, so it is by value.
BDF A learned fool is more a fool than an ignorant fool. -- Moliere
-
Ah, so it is by value.
BDF A learned fool is more a fool than an ignorant fool. -- Moliere
I think by volume. :)
Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
-
I think by volume. :)
Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
Although not one of the choices in the original post, I think it's a good answer given that the OP is a load of, umm, excrement.
BDF A learned fool is more a fool than an ignorant fool. -- Moliere
-
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.
What's the point of your post? :|
"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
-
I think by volume. :)
Chris Meech I am Canadian. [heard in a local bar] In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
Chris Meech wrote:
think by volume.
...and alot of it too :laugh:
"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
-
by value.
This signature was proudly tested on animals.
So If I print the value anywhere inside the Load function will only print 10. But it is not.... Its 30 after function call.
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE. How to post a question
-
What's the point of your post? :|
"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
After the function call its printing 30, That is the matter :)
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE. How to post a question
-
Ah, so it is by value.
BDF A learned fool is more a fool than an ignorant fool. -- Moliere
by the lower intestinal tract.
Then how its by value??? Once its passed, you wont get the original....... :laugh:
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE. How to post a question
-
After the function call its printing 30, That is the matter :)
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE. How to post a question
-
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.
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 -
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 ArchiveV. 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. :)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] -
After the function call its printing 30, That is the matter :)
EVEN THE WORD IMPOSSIBLE SAYS I M POSSIBLE. How to post a question
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
-
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.
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
-
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. :)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]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
-
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
You are wrong. Whenever you pass an object, a reference to the object's instance is passed by value. :)
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] -
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.
-
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.
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