|
-
Sep 11th, 2007, 10:40 AM
#1
Thread Starter
Addicted Member
Byref and ByVal
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
-
Sep 11th, 2007, 03:30 PM
#2
I wonder how many charact
-
Sep 11th, 2007, 06:59 PM
#3
Re: Byref and ByVal
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.
-
Sep 14th, 2007, 09:19 AM
#4
Re: Byref and ByVal
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.
-
Sep 14th, 2007, 06:57 PM
#5
Re: Byref and ByVal
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?
-
Sep 14th, 2007, 09:07 PM
#6
Re: Byref and ByVal
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
-
Sep 14th, 2007, 09:12 PM
#7
Re: Byref and ByVal
VB.NET is criticized for its support of optional parameters.
Features in VB.NET not in C#:
Optionally ignore ref/ByRef behavior for passing arguments. (C# requires a temp variable to do this.)
-
Sep 14th, 2007, 09:42 PM
#8
Re: Byref and ByVal
 Originally Posted by Hell-Lord
VB.NET is criticized for its support of optional parameters.
Features in VB.NET not in C#:
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:
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 Sub
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:
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;
}
None of that has anything whatsoever to do with optional parameters or overloading.
-
Sep 14th, 2007, 09:46 PM
#9
Re: Byref and ByVal
Thanks for clearing that up
-
Sep 18th, 2007, 07:02 AM
#10
Hyperactive Member
Re: Byref and ByVal
 Originally Posted by jmcilhinney
When you pass by value a copy of the variable is created and passed
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.
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.
-
Sep 18th, 2007, 07:19 AM
#11
Re: Byref and ByVal
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.
-
Sep 18th, 2007, 07:23 AM
#12
Hyperactive Member
Re: Byref and ByVal
 Originally Posted by penagate
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.
-
Sep 18th, 2007, 08:40 AM
#13
Re: Byref and ByVal
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|