#157844 - 2006-02-24 03:23 PM
Declare and Assign Variables at once
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
It would be nice to be able to declare a variable and assign a value o it in one command. For instance Code:
Dim $Variable = "string"
This saves many lines of code especially when declaring ALOT of variables. Maybe someone could add this to the Masterlist of suggestions ?
|
Top
|
|
|
|
#157850 - 2006-02-28 08:44 AM
Re: Declare and Assign Variables at once
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
Thats's a good workaround for most scripts thx However I'm working on a new UDF which requires ALOT of variables and I want to keep the code as nice and tidy as possible, with option explicity it adds at least 10 more lines of code hence why I suggested this feature. But thanks for your suggestion I will use this in my main scripts
|
Top
|
|
|
|
#157851 - 2006-02-28 09:25 AM
Re: Declare and Assign Variables at once
|
Richard H.
Administrator
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
Quote:
You could always have the best of both worlds:
Not quite true - $a is declared in the global scope, there is no way of implicitly declaring a variable in the local scope.
This means that if you routinely use this trick to avoid having to explicitly declare variables you may well have implicitly declared the same variable twice in different files or functions.
As you don't have "Explicit" set on, you will not get an error about re-declaring the variable and you will screw up the value of the originally declared variable.
The same will apply if you forget to declare a variable which has already been (implicitly) declared as global - the global variable will be used and you will not get any runtime interpreter errors however your script may not function as intended.
|
Top
|
|
|
|
#157853 - 2006-02-28 05:27 PM
Re: Declare and Assign Variables at once
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
Quote:
anyways, saying that having to declare and initialise variables separately increases the line count of a script is not true.
hmm, how would this go into one line Code:
Dim $var $var = "string"
Besides, it is a simple thing to implement, and it saves time at least. When you decide to use a variable of which you already know what info it is gonna hold it IS far easier to use Code:
Dim $var = "string"
[edit] post fixed by jooel
Edited by Jooel (2006-03-10 01:13 PM)
|
Top
|
|
|
|
#157855 - 2006-03-01 08:45 AM
Re: Declare and Assign Variables at once
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
Fair enough But you'd have to agree that typing the var 2 times is a bit annoying to say the least and not very practical. Especially since most scripting languages support declaration and assigning at once.
|
Top
|
|
|
|
#157856 - 2006-03-01 05:47 PM
Re: Declare and Assign Variables at once
|
NTDOC
Administrator
Registered: 2000-07-28
Posts: 11624
Loc: CA
|
Quote:
Fair enough Especially since most scripting languages support declaration and assigning at once.
Well one would also agree that we don't want to turn this into another scripting language like Ruby/Perl/VBS.
I'm not trying to squash the idea, just saying I can take it or leave it as I don't write too many LARGE scripts that make that much of a difference to me.
|
Top
|
|
|
|
#177590 - 2007-07-05 09:45 AM
Re: Declare and Assign Variables at once
[Re: Lonkero]
|
Arend_
MM club member
Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
|
One step further Declare as type and assign at once. For instance:
Dim $var As String = "New string" DIm $Shell As Object = CreateObject("WScript.Shell")
|
Top
|
|
|
|
#177607 - 2007-07-05 01:33 PM
Re: Declare and Assign Variables at once
[Re: Lonkero]
|
Witto
MM club member
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
Strong Types. Cool. No "autocasting" problems anymore.
Dim $RC
$RC = SetOption("StrongType", "On")
Dim $strNumber As String = "500"
Dim $intNumber As Integer = 500
; $strNumber + $intNumber ? ; This line would give an error
Cast($strNumber, "Integer") + $intNumber ?
But anyway, I agree, declare and assign at once would be nice
Dim $strNumber = "500"
Dim $intNumber = 500
|
Top
|
|
|
|
Moderator: Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart
|
1 registered
(Allen)
and 496 anonymous users online.
|
|
|