|
-
May 12th, 2002, 05:19 AM
#1
Thread Starter
Fanatic Member
About ByRef
Hi, im really confused and i cant work out why this does this.
VB Code:
Private Sub Form_Load()
Dim a As Long
a = 10
c = f(a)
MsgBox a
End Sub
Private Function f(b As Long) As Long
b = b + 1
End Function
Can someone please explain why a = 11 after this code is done?
i thought that you had to use ByRef keyword if you wanted to change the original value??
ie
VB Code:
Private Function f(ByRef b as long) as long
How is it possible that 'a' variable changes when all im doing is passing the 'a' value to the function?
-
May 12th, 2002, 05:26 AM
#2
ByRef is ByDefault.
-
May 12th, 2002, 05:29 AM
#3
Thread Starter
Fanatic Member
Ok thats totally disturbed me now, from 3 years of college to learning Visual Basic i always thought ByVal was default and ive always worked as if it was.
How i never ran into a problem before today i totally dont know
-
May 12th, 2002, 05:34 AM
#4
Hyperactive Member
I think this might help
VB Code:
Private Sub Form_Load()
Dim a As Long
a = 10
c = f(a)
MsgBox a
End Sub
Private Function f(b As Long) As Long
' At this point a = b and b = a so when you add 1 to b you are adding 1 to a
b = b + 1
End Function
Looking at what you had this looks like what you were tiring to accomplish.
VB Code:
Private Sub Form_Load()
Dim a As Long
a = 10
c = f(a)
MsgBox a
MsgBox f(a)
MsgBox c
End Sub
Private Function f(b As Long) As Long
f = b + 1
End Function
-
May 12th, 2002, 05:40 AM
#5
Thread Starter
Fanatic Member
Thanks but i solved it by
VB Code:
Private Function f( ByVal b as long) as long
b=b+1
End Function
that keeps the original variable 'a' as 10
its just that i always thought ByVal was default and im suprised that ive never had any problems as i always thought it was ByVal
-
May 12th, 2002, 05:54 AM
#6
Thou art doomed.
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
|