Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 225
Default Explicit declaration of variables

On Monday, February 28, 2011 11:56:06 AM UTC, Slim Slender wrote:
Suppose a developer does not explicitly declare variables but just
makes up a variable and gives it a value when he needs it anywhere in
the code. He prefixes his variable names with "s" for string, "i" for
integer, "s" for single, etc. Is there some way I can examine this
code and determine that these variables are actually variant data type
and not the data type suggested by the prifix? Is there a way to
demonstrate or prove that this is not the best practice?


By far the most important reason for declaring variables (IMHO)
is to avoid problems caused by typos in variable names; for example:


myVariable = 1
'..
'... more code
'...
if myVaraible = 1 then ' note misspelt name
' ... do something
End if

Without Option Explicit this will run without
complaint but it will not work as expected and
could be very hard to debug. For that reason
alone I think Option Explicit should be mandatory.

You can use the Watch window to determine at
run time whether a variable is a Variant, but
I can't find a way to do so programatically:
The TypeName function gives the type as
determined by the latest assignment.

MsgBox TypeName(x) ' shows "Empty"
x = 1
MsgBox TypeName(x) ' shows "Integer"
x = "hello"
MsgBox TypeName(x) ' shows "String"



Andrew
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
Explicit declaration of variables Slim Slender[_3_] Excel Programming 1 February 28th 11 02:40 PM
Multiple Variables' Declaration In Loops Faraz A. Qureshi Excel Programming 6 September 11th 09 09:39 AM
Declaring variables - Option Explicit Bijl167[_14_] Excel Programming 2 August 3rd 04 01:13 PM
Correct declaration of variables Bob Phillips[_6_] Excel Programming 1 November 30th 03 03:08 PM
Correct declaration of variables marko Excel Programming 1 November 30th 03 09:56 AM


All times are GMT +1. The time now is 11:52 PM.

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"