Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 169
Default How to know if a variable has been reset inside other function

Hi,
My program use a lot of function and sometimes, the variables are reset
because it call a function that use the same variable name. I have sometimes,
Function inside Function inside Function and it's very hard to make all
differents. Is there a way to insure no variable will be reset?? Like a
program compilation or execution option or something else?
Thanks a lot!
--
Alex St-Pierre
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default How to know if a variable has been reset inside other function

If your intention is to use the variable in the function and alter it only
temporarily while the function does it job then the answer is to pass
those variables ByVal.
Default is ByRef and that can leave it in an altered state after the
function
is finished.
Best to look this up in the VBA help.

RBS


"Alex St-Pierre" wrote in message
...
Hi,
My program use a lot of function and sometimes, the variables are reset
because it call a function that use the same variable name. I have
sometimes,
Function inside Function inside Function and it's very hard to make all
differents. Is there a way to insure no variable will be reset?? Like a
program compilation or execution option or something else?
Thanks a lot!
--
Alex St-Pierre


  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 169
Default How to know if a variable has been reset inside other function

Thanks, it's what I needed. I will use ByVal inside function.
If I use twice i as in the example below, i is not reset in the function.
What I understand is that it's only the elements inside function that are
re-formated (with ByRef).

Sub temp()
Dim iValue as integer
iValue = 10
For i = 1 to 10
Temp = myFunction(iValue)
Next i
End Sub

Function myFunction(ByVal iValue as integer)
Dim i as integer
For i = 1 to 3
'treatment...
Next i
End Function

--
Alex St-Pierre


"RB Smissaert" wrote:

If your intention is to use the variable in the function and alter it only
temporarily while the function does it job then the answer is to pass
those variables ByVal.
Default is ByRef and that can leave it in an altered state after the
function
is finished.
Best to look this up in the VBA help.

RBS


"Alex St-Pierre" wrote in message
...
Hi,
My program use a lot of function and sometimes, the variables are reset
because it call a function that use the same variable name. I have
sometimes,
Function inside Function inside Function and it's very hard to make all
differents. Is there a way to insure no variable will be reset?? Like a
program compilation or execution option or something else?
Thanks a lot!
--
Alex St-Pierre



  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default How to know if a variable has been reset inside other function

That's it. Looks you learned something useful there.

RBS

"Alex St-Pierre" wrote in message
...
Thanks, it's what I needed. I will use ByVal inside function.
If I use twice i as in the example below, i is not reset in the function.
What I understand is that it's only the elements inside function that are
re-formated (with ByRef).

Sub temp()
Dim iValue as integer
iValue = 10
For i = 1 to 10
Temp = myFunction(iValue)
Next i
End Sub

Function myFunction(ByVal iValue as integer)
Dim i as integer
For i = 1 to 3
'treatment...
Next i
End Function

--
Alex St-Pierre


"RB Smissaert" wrote:

If your intention is to use the variable in the function and alter it
only
temporarily while the function does it job then the answer is to pass
those variables ByVal.
Default is ByRef and that can leave it in an altered state after the
function
is finished.
Best to look this up in the VBA help.

RBS


"Alex St-Pierre" wrote in
message
...
Hi,
My program use a lot of function and sometimes, the variables are reset
because it call a function that use the same variable name. I have
sometimes,
Function inside Function inside Function and it's very hard to make all
differents. Is there a way to insure no variable will be reset?? Like a
program compilation or execution option or something else?
Thanks a lot!
--
Alex St-Pierre




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 5,939
Default How to know if a variable has been reset inside other function

Just to elaborate... You should always pass by value unless you have reason
not to (IMO). It makes debugging a pile easier since you know the values of
variables can not be changed by other subs or functions. You should note
however that there is a penalty to pass by value and that is that you are
passing a copy of the variable and there is some (ususally very small)
overhead in creating the copy to be passed. The amount of overhead depends on
the variable that you are passing and in the case of strings it can be a fair
bit of overhead. So if you are in the need for speed and you are passing
strings in a loop you should probably pass by reference. Just my 2 cents...
--
HTH...

Jim Thomlinson


"RB Smissaert" wrote:

That's it. Looks you learned something useful there.

RBS

"Alex St-Pierre" wrote in message
...
Thanks, it's what I needed. I will use ByVal inside function.
If I use twice i as in the example below, i is not reset in the function.
What I understand is that it's only the elements inside function that are
re-formated (with ByRef).

Sub temp()
Dim iValue as integer
iValue = 10
For i = 1 to 10
Temp = myFunction(iValue)
Next i
End Sub

Function myFunction(ByVal iValue as integer)
Dim i as integer
For i = 1 to 3
'treatment...
Next i
End Function

--
Alex St-Pierre


"RB Smissaert" wrote:

If your intention is to use the variable in the function and alter it
only
temporarily while the function does it job then the answer is to pass
those variables ByVal.
Default is ByRef and that can leave it in an altered state after the
function
is finished.
Best to look this up in the VBA help.

RBS


"Alex St-Pierre" wrote in
message
...
Hi,
My program use a lot of function and sometimes, the variables are reset
because it call a function that use the same variable name. I have
sometimes,
Function inside Function inside Function and it's very hard to make all
differents. Is there a way to insure no variable will be reset?? Like a
program compilation or execution option or something else?
Thanks a lot!
--
Alex St-Pierre






  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 2,452
Default How to know if a variable has been reset inside other function

That's one approach.
I do opposite as speed is often important and many of my functions are used
in loops.
It saves me worrying about functions possibly slowing things down.
When I write the function I just check it doesn't alter any variables that
it shouldn't.
ByRef is the default, so it saves a bit of coding as well.

RBS

"Jim Thomlinson" wrote in message
...
Just to elaborate... You should always pass by value unless you have
reason
not to (IMO). It makes debugging a pile easier since you know the values
of
variables can not be changed by other subs or functions. You should note
however that there is a penalty to pass by value and that is that you are
passing a copy of the variable and there is some (ususally very small)
overhead in creating the copy to be passed. The amount of overhead depends
on
the variable that you are passing and in the case of strings it can be a
fair
bit of overhead. So if you are in the need for speed and you are passing
strings in a loop you should probably pass by reference. Just my 2
cents...
--
HTH...

Jim Thomlinson


"RB Smissaert" wrote:

That's it. Looks you learned something useful there.

RBS

"Alex St-Pierre" wrote in
message
...
Thanks, it's what I needed. I will use ByVal inside function.
If I use twice i as in the example below, i is not reset in the
function.
What I understand is that it's only the elements inside function that
are
re-formated (with ByRef).

Sub temp()
Dim iValue as integer
iValue = 10
For i = 1 to 10
Temp = myFunction(iValue)
Next i
End Sub

Function myFunction(ByVal iValue as integer)
Dim i as integer
For i = 1 to 3
'treatment...
Next i
End Function

--
Alex St-Pierre


"RB Smissaert" wrote:

If your intention is to use the variable in the function and alter it
only
temporarily while the function does it job then the answer is to pass
those variables ByVal.
Default is ByRef and that can leave it in an altered state after the
function
is finished.
Best to look this up in the VBA help.

RBS


"Alex St-Pierre" wrote in
message
...
Hi,
My program use a lot of function and sometimes, the variables are
reset
because it call a function that use the same variable name. I have
sometimes,
Function inside Function inside Function and it's very hard to make
all
differents. Is there a way to insure no variable will be reset??
Like a
program compilation or execution option or something else?
Thanks a lot!
--
Alex St-Pierre





Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reset public variable Alex St-Pierre Excel Programming 5 January 31st 06 03:20 PM
Using a range variable inside a excel function Michael Excel Discussion (Misc queries) 2 November 14th 05 02:52 PM
Range variable not being reset Henry Stock Excel Programming 1 November 8th 05 06:23 PM
variable inside a formula? Phil Excel Programming 5 July 28th 04 12:04 AM
variable that keeps its value until reset Jamie Martin[_2_] Excel Programming 2 September 30th 03 05:57 AM


All times are GMT +1. The time now is 07:33 AM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"