Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 301
  • Last Modified:

When to Dim variables - before or after 'On Error'

Within a procedure/function or property:  Is it best to Dim variables before or after the 'On Error' statement?

For example:

Public Sub func()
   Dim sBuffer As String

   On Error Goto EH
   :
   :
   Exit Sub
EH:
   :

End Sub

or

Public Sub func()
   On Error Goto EH

   Dim sBuffer As String
   :
   :
   Exit Sub

EH:
   :
End Sub

Thanks.
0
halfondj
Asked:
halfondj
4 Solutions
 
TomLaw1999Commented:
Convention seems to be to dim varibles first.
0
 
learning_t0_pr0gramCommented:
I always dim the variables first...
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
I always put my On Error at the top since it is only one line and it makes it very clear that Error trapping is occuring within the Function/Sub.
0
 
DaPenCommented:
There are times when having the "On Error" command running with in your code can prevent errors from being passed properly (ActiveX Controls).  Also, sometimes you have to specifically place an "On Error" to a specific handler around a piece of code while the rest of the sub routine uses a different (generic) handler.  An example of this might be when the generic handler runs it always calls the specific handler code but under particular situations you only need to run the specific handler code.  All this being said, the best place to put an "On Error" statement is before the code that is going to use it.  "DIM" statements are not normally considered executable code and therefore placing an "On Error" before them would be of no use.
0
 
halfondjAuthor Commented:
Thanks for all the answers.  Since there doesn't seem to be a wrong answer, I split the points.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now