Hi:
Can someone tell me When to use ByVal? If Byref supposed to be able to the task, then why need Byval?
I read a lot of article about this, but I'm yet to find an answer!
Thanks
Simon:confused:
Printable View
Hi:
Can someone tell me When to use ByVal? If Byref supposed to be able to the task, then why need Byval?
I read a lot of article about this, but I'm yet to find an answer!
Thanks
Simon:confused:
Just to summarise, you should pass by value by default and only pass by reference when it's specifically required. It would only be specifically required if you wanted to be able to assign a new value or object to the parameter within the method and have the original variable contain that new value or refer to that new object after the method completes.
When you pass by value a copy of the variable is created and passed, while when you pass by reference a reference to the variable is passed. In VB6 it was the default to pass by reference to avoid copying the contents of variables that might be large. In .NET, if a variable contains a large amount of data then it must be a poorly designed structure. No well designed structures should be so large that copying them should be an issue. Anything large enough to be considered an issue should be a class, in which case the variable contains only a memory address and copying it is no issue at all.
ByRef and ByVal are VB keywords. In C# we use 'ref' to pass arguments by reference. Passing by value is the default and thus implicit when 'ref' or 'out' are omitted.
C# is criticized for its lack of support for optional parameters. I am not to sure but i have been told you should use a temp variable. Could someone please clarify?
If anything VB should be criticized for it's support of optional parameters. Personally, I think overloading is the preferable way.
Other than that, Huh? Temp vars?, and what does that have to do with this thread in the first place.
-tg
VB.NET is criticized for its support of optional parameters.
Features in VB.NET not in C#:
Quote:
Optionally ignore ref/ByRef behavior for passing arguments. (C# requires a temp variable to do this.)
One has nothing to do with the other. In VB.NET you can enclose a variable in parentheses when passing it to force it to be passed by value, even if the method argument is declared by reference. This is because VB.NET will implicitly create a temporary variable and pass that. Try this code:Quote:
Originally Posted by Hell-Lord
See that var1's value is changed because it is passed by reference, while var2's value is not because it is forced to be passed by value. In C# you have to create that temp variable explicitly:vb.net Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim var1 As Integer = 10 Dim var2 As Integer = 100 MessageBox.Show(String.Format("var1 = {0}; var2 = {1}", var1, var2)) Me.ChangeParameterValue(var1) Me.ChangeParameterValue((var2)) MessageBox.Show(String.Format("var1 = {0}; var2 = {1}", var1, var2)) End Sub Private Sub ChangeParameterValue(ByRef p As Integer) p *= 2 End SubNone of that has anything whatsoever to do with optional parameters or overloading.C# Code:
private void Form1_Load(object sender, EventArgs e) { int var1 = 10; int var2 = 100; MessageBox.Show(String.Format("var1 = {0}; var2 = {1}", var1, var2)); this.ChangeParameterValue(ref var1); int var3 = var2; this.ChangeParameterValue(ref var3); MessageBox.Show(String.Format("var1 = {0}; var2 = {1}", var1, var2)); } private void ChangeParameterValue(ref int p) { p *= 2; }
Thanks for clearing that up ;)
I think a reference to the object, if it is a reference object, is passed to the method, then a copy of the object is created within the method.Quote:
Originally Posted by jmcilhinney
I'm trawling deeply through my memory bank here, but I think all parameters are actually references to objects, the value of the reference is retrieved within the implementation of the method.
If you pass a reference by value the reference is copied. If you pass a reference by reference a reference to the reference is passed.
To actually copy an object it needs to provide a Clone method and it won't happen implicitly.
So the objects themselves are never passed, only references to them, that is what I thought.Quote:
Originally Posted by penagate
A method parameter is a special variable and has a special place in a stack frame, which is the area of memory that basically defines a method call that is constructed every time you call a method. That parameter can contain one of four things:
1. A value, if the parameter is a value type passed by value.
2. A reference to a value, if the parameter is a value type passed by reference. In C/C++ this would be a pointer.
3. A reference, if the parameter is a reference type passed by value. In C/C++ this would be a pointer.
4. A reference to a reference, if the parameter is a reference type passed by reference. In C/C++ this would be a pointer to a pointer.
If it's a value then changing that value has no effect on the original.
If it's a reference to a value then changing the value of the parameter means changing the value of the variable referred to by the parameter, which does affect the original.
If it's a reference then changing a property of the parameter means changing a property of the object referred to by the parameter, which is the same object referred to by the original variable, so that does affect the original. Assigning a new object to the parameter does not assign a new object to the original variable though, so that change doesn't affect the original.
If it's a reference to a reference then there's still only one object so changing a property does affect the original. Assigning a new object to the parameter means assigning a new object to the variable referred to by the parameter, which is the original variable, so this change does affect the original.