Page 1 of 2 12>
Topic Options
#157844 - 2006-02-24 03:23 PM Declare and Assign Variables at once
Arend_ Offline
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
#157845 - 2006-02-25 12:32 PM Re: Declare and Assign Variables at once
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
are you using option explicit?
as if not, you may use implicit declaration.
_________________________
!

download KiXnet

Top
#157846 - 2006-02-27 02:42 PM Re: Declare and Assign Variables at once
Arend_ Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
I use opion explicit to check wether I have loose vars or not. but without option explicit I can declare and assign at the same time ?
Top
#157847 - 2006-02-27 02:45 PM Re: Declare and Assign Variables at once
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Assigning a value to the variable will create it implicitly in the global scope if you do not have the "Explicit" option set.
Top
#157848 - 2006-02-27 04:16 PM Re: Declare and Assign Variables at once
Arend_ Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
Ugh of course.. was too early for me when I answered that.
Top
#157849 - 2006-02-28 05:03 AM Re: Declare and Assign Variables at once
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11164
Loc: Boston, MA, USA
You could always have the best of both worlds:
Code:

$a='abcd'

$=setoption('explicit','on')

dim $b

_________________________
There are two types of vessels, submarines and targets.

Top
#157850 - 2006-02-28 08:44 AM Re: Declare and Assign Variables at once
Arend_ Offline
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 Offline
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
#157852 - 2006-02-28 09:56 AM Re: Declare and Assign Variables at once
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
anyways, saying that having to declare and initialise variables separately increases the line count of a script is not true.

the amount of lines has always been in kixtart coding totally about the scripters liking.
no matter how complex your script, you can always write it in single line.

and true words there richie.
even though implicit declaration works, it's not the best way to go.
imho, it works for short simple scripts but gradually as the complexity of the script grows, every global variable adds to the pain of the scripter exponentially.
specially when implicitly declared.
_________________________
!

download KiXnet

Top
#157853 - 2006-02-28 05:27 PM Re: Declare and Assign Variables at once
Arend_ Offline
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
#157854 - 2006-02-28 06:06 PM Re: Declare and Assign Variables at once
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
how about:
Code:

Dim $var $var = "string"



I see only one line.
_________________________
!

download KiXnet

Top
#157855 - 2006-03-01 08:45 AM Re: Declare and Assign Variables at once
Arend_ Offline
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 Offline
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
#157857 - 2006-03-10 12:25 PM Re: Declare and Assign Variables at once
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
the problem of assigning and dimming at once is exactly what explicit is trying to remove.
loose variables lying around the scripts as the scripter doesn't want to keep scrolling to the top so he just dims the vars where ever he is at that point.

also, the difference with kixtart is that the vars don't need to be initialized to be of some type like with many others. it's a blessing and a curse but weights more on the plus side what comes to easiness of use.

that's also why ppl can see scripts written in kixtart that have a block of variables dimmed on a single line consisting of some dozens of vars.

but like doc said, it's not a big deal.
and I can see it being useful specially if the scripter or the code viewer is used to other languages and their extensive initialization blocks.
_________________________
!

download KiXnet

Top
#157858 - 2006-03-10 01:10 PM Re: Declare and Assign Variables at once
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
included in the masterlist.
Top
#177590 - 2007-07-05 09:45 AM Re: Declare and Assign Variables at once [Re: Lonkero]
Arend_ Offline
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
#177605 - 2007-07-05 12:25 PM Re: Declare and Assign Variables at once [Re: Arend_]
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
apronk, that wouldn't make much sense as kixtart is autotype.
so, if you do:
 Code:
DIm $Shell As String = CreateObject("WScript.Shell") 
vartypename($Shell)


it would still return object.
_________________________
!

download KiXnet

Top
#177607 - 2007-07-05 01:33 PM Re: Declare and Assign Variables at once [Re: Lonkero]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
Strong Types. Cool. No "autocasting" problems anymore.
 Code:
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
 Code:
Dim $strNumber = "500"
Dim $intNumber = 500

Top
#177615 - 2007-07-05 03:44 PM Re: Declare and Assign Variables at once [Re: Lonkero]
Arend_ Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1895
Loc: Hilversum, The Netherlands
 Originally Posted By: Jooel
apronk, that wouldn't make much sense as kixtart is autotype.
so, if you do:
 Code:
DIm $Shell As String = CreateObject("WScript.Shell") 
vartypename($Shell)


it would still return object.


Autotype should be optional enabled off course.
SetOption("AutoType",Off")
That way you will HAVE to assign types to your vars when turned on.
If turned on, Kix should give error if a type is not declared.

Top
#177619 - 2007-07-05 04:10 PM Re: Declare and Assign Variables at once [Re: Arend_]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
You got my two cents on this one.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
Page 1 of 2 12>


Moderator:  Lonkero, ShaneEP, Jochen, Radimus, Glenn Barnas, Allen, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 302 anonymous users online.
Newest Members
Raoul, Timothy, Jojo67, MaikSimon, kvn317
17875 Registered Users

Generated in 0.075 seconds in which 0.024 seconds were spent on a total of 14 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org