#119319 - 2004-05-11 11:29 AM
Define constants with kixtart
|
Michel
Lurker
Registered: 2004-05-11
Posts: 1
Loc: Belgium
|
Hello,
Is there a keyword to define a constant with kixtart (const, ...) or do I have to use variables ?
Thanks !
|
Top
|
|
|
|
#119322 - 2004-05-11 03:17 PM
Re: Define constants with kixtart
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
There are a set of Constants, but with KiXForms and not KiXtart itself -
InitKFConstants() - Initializes KiXforms constants
Kent
|
Top
|
|
|
|
#119324 - 2004-05-12 01:27 PM
Re: Define constants with kixtart
|
Richard H.
Administrator
   
Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
|
No, there is no way to define a constant.
You must use a variable, and declare it as GLOBAL.
What you need to do is decide on a naming convention which differentiates between constants and variables, so that you don't accidentally bash them in your code.
KiXtart is case insensitive (mostly), so you cannot rely on giving the variables all upper case names to protect them, although it does make them stand out in your code.
When I want to define constants I do this:
- If the constant is only going to be used by functions or script in a called library, then I prefix the variable name with an underscore then the scope (library name) then another underscore.
- If the constant is to be visible across all scopes, I define it in the start of the main script and prefix the variable name with two underscores. This is exactly the same as the previous example, but with a null (or global) scope.
For example, suppose I define a variable to be the name of the server which hosts all my printers. This is a general constant which may be used by all my routines, so I define it as: Code:
Global $__PRINTSERVER
However, I have a library of network routines which I call into scripts as I need them. The constants are define at the head of the library file like this: Code:
If Not IsDeclared($_Network_ISLOADED) Global $_Network_ISLOADED Global $_Network_NETMASK Global $_Network_DOMAINNAME $_Network_NETMASK="255.255.252.0" ... EndIf
As the scope is "Network", there is no danger that I will accidentally clash with the global constants, or constants declared in any other file.
That's just the way I do it. You can make up your own variable naming convention, and as long as you stick to it your variables will be constant
|
Top
|
|
|
|
#119325 - 2004-05-12 01:37 PM
Re: Define constants with kixtart
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
I use something like:
Code:
$OS = split(@ProductType)[1] $CSD = val(right(@csd,1)) $ADMIN = ingroup('@wksta\'+sidtoname('S-1-5-32-544'))-1+@INWIN $HKLMSCCS ="HKLM\SYSTEM\CurrentControlSet" $HKCUSMWCV ="HKCU\Software\Microsoft\Windows\CurrentVersion" $HKLMSMWCV ="HKLM\Software\Microsoft\Windows\CurrentVersion" $HKLMSMWNTCV ="HKLM\Software\Microsoft\Windows NT\CurrentVersion" $HKCUSMWNTCV ="HKCU\Software\Microsoft\Windows NT\CurrentVersion" $Desktop =readvalue($HKCUSMWCV+"\Explorer\Shell Folders",'Desktop') $StartMenu =readvalue($HKCUSMWCV+"\Explorer\Shell Folders",'StartMenu') $Favorites =readvalue($HKCUSMWCV+"\Explorer\Shell Folders",'Favorites') $MyDocuments =readvalue($HKCUSMWCV+"\Explorer\Shell Folders",'MyDocuments') $alldesktop =expandenvironmentvars(readvalue($HKLMSMWCV+"\Explorer\User Shell Folders","Common Desktop")) $allstartmenu=expandenvironmentvars(readvalue($HKLMSMWCV+"\Explorer\User Shell Folders","Common Start Menu")) $LDAP ="LDAP://"+@domain+"/"+GetObject("LDAP://rootDSE").Get("defaultNamingContext")
|
Top
|
|
|
|
#119326 - 2004-05-12 04:51 PM
Re: Define constants with kixtart
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
how about something like this??
Code:
$list = "Const1=2",
"Const2=10",
"Const3=14",
"Const4=1000",
"Const5=400",
"Const6=32",
"Const7=11",
"Const8=84"
BuildConst($list)
? Const1
? Const2
? Const3
? Const4
? Const5
? Const6
? Const7
? Const8
Function BuildConst($constlist)
for each $Const in $constlist
$ = execute('Function ' + split($const,"=")[0] + ' $$' + split($const,"=")[0] + '=' + split($const,"=")[1] + ' endfunction')
;? $const
next
endfunction
Edited by Bryce (2004-05-12 04:52 PM)
|
Top
|
|
|
|
#119328 - 2004-05-12 06:57 PM
Re: Define constants with kixtart
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
Quote:
clever... I would leave the for-each part of and make it const() udf. const(this) const(that)
but indeed, nice thought.
hmm you are right, that does sound better.
|
Top
|
|
|
|
#119329 - 2004-05-12 07:10 PM
Re: Define constants with kixtart
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
so, something like this...
Code:
Function Const($Constname,$ConstData)
Dim $exit,$
$exit = setoption("NoVarsInStrings","on")
$ = execute('Function ' + $constname + ' $' + $constname + '=' + $constdata + ' endfunction')
$exit = setoption("NoVarsInStrings","$exit")
exit($)
endfunction
a example...
Code:
Const("allways1",1)
? "The value of allways1 = " + allways1
Edited by Bryce (2004-05-12 07:11 PM)
|
Top
|
|
|
|
#119330 - 2004-05-12 07:12 PM
Re: Define constants with kixtart
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
Here is Bryce's code with Lonk's suggestions. Also NoVarsInStrings and Explicit friendly...
Code:
$nul = SetOption("NoVarsInStrings","On") $nul = SetOption("Explicit","On") Const(TopCenter,2) TopCenter ? Function Const($Constant,$Value) Dim $nul $nul = Execute('Function ' + $Constant + ' $' + $Constant + '=' + $Value + ' EndFunction') EndFunction
|
Top
|
|
|
|
#119332 - 2004-05-12 08:48 PM
Re: Define constants with kixtart
|
Chris S.
MM club member
   
Registered: 2002-03-18
Posts: 2368
Loc: Earth
|
Great idea, Bryce! Here's an example setting some KiXforms constants. I would suggest that those who use this take Richard's suggestion to use underscores ("_") when defining constants to avoid clashing with KiXtart commands.
Code:
$nul = SetOption("NoVarsInStrings","On") $nul = SetOption("Explicit","On") Const(_None,0) Const(_Top,1) Const(_Bottom,2) Const(_Left,4) Const(_Right,8) Const(_TopBottomLeft,7) Const(_TopLeftRight,13) Const(_BottomLeftRight,14) Const(_All,15) _None ? _Top ? _Bottom ? _Left ? _Right ? _TopBottomLeft ? _TopLeftRight ? _BottomLeftRight ? _All ? _Left + _Right ? Function Const($Constant,$Value) Exit Execute('Function ' + $Constant + ' $' + $Constant + '=' + $Value + ' EndFunction') EndFunction
|
Top
|
|
|
|
#119333 - 2004-05-12 09:01 PM
Re: Define constants with kixtart
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
Lookie here... from out of nowhere Bryce hits solid gold
|
Top
|
|
|
|
#119334 - 2004-05-12 09:23 PM
Re: Define constants with kixtart
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
this errors out though...
const(_HKCUSMWCV ,"HKCU\Software\Microsoft\Windows\CurrentVersion") const(_Desktop ,readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','Desktop'))
? _HKCUSMWCV ? _Desktop
Function Const($Constant,$Value) Exit Execute('Function ' + $Constant + ' $' + $Constant + '=' + $Value + ' EndFunction') EndFunction
Quote:
ERROR : unknown command [Users\Desktop]! Script: C:\Documents and Settings\wheelerc\Desktop\const.kix Line : 0
|
Top
|
|
|
|
#119335 - 2004-05-12 09:36 PM
Re: Define constants with kixtart
|
Kdyer
KiX Supporter
   
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
Rad,
If we change your code a bit, we see:
Code:
const(_Desktop,readvalue('HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders','Desktop'))
;? _HKCUSMWCV
? _Desktop
? _Desktop only shows as a 1..
Kent
|
Top
|
|
|
|
#119336 - 2004-05-12 09:48 PM
Re: Define constants with kixtart
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
this works... (wrapping with quotes) Code:
break on
const(_OS ,split(@ProductType)[1]) const(_CSD ,val(right(@csd,1))) const(_ADMIN ,ingroup('@wksta\'+sidtoname('S-1-5-32-544'))-1+@INWIN) const(_HKLMSCCS ,"HKLM\SYSTEM\CurrentControlSet") const(_HKCUSMWCV ,"HKCU\Software\Microsoft\Windows\CurrentVersion") const(_HKLMSMWCV ,"HKLM\Software\Microsoft\Windows\CurrentVersion") const(_HKLMSMWNTCV ,"HKLM\Software\Microsoft\Windows NT\CurrentVersion") const(_HKCUSMWNTCV ,"HKCU\Software\Microsoft\Windows NT\CurrentVersion") const(_Desktop ,"readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','Desktop')") const(_StartMenu ,"readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','StartMenu')") const(_Favorites ,"readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','Favorites')") const(_MyDocuments ,"readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','MyDocuments')") const(_alldesktop ,"expandenvironmentvars(readvalue(_HKLMSMWCV+'\Explorer\User Shell Folders','Common Desktop'))") const(_allstartmenu,"expandenvironmentvars(readvalue(_HKLMSMWCV+'\Explorer\User Shell Folders','Common Start Menu'))") const(_LDAP ,'"LDAP://"+@domain+"/"+GetObject("LDAP://rootDSE").Get("defaultNamingContext")')
? _OS ? _CSD ? _ADMIN ? _HKLMSCCS ? _HKCUSMWCV ? _Desktop ? _alldesktop ? _LDAP
Function Const($Constant,$Value) Exit Execute('Function ' + $Constant + ' $' + $Constant + '=' + $Value + ' EndFunction') EndFunction
|
Top
|
|
|
|
#119337 - 2004-05-12 09:50 PM
Re: Define constants with kixtart
|
Radimus
Moderator
   
Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
|
now, my only concern with this is that every time this is used it is re-evaluated...
? processor/memory/time use???
|
Top
|
|
|
|
#119338 - 2004-05-12 10:14 PM
Re: Define constants with kixtart
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
wow! talk about a group effort! 
yeah, everytime you call one of these it will be called just like any function. For example, your above post basicly add's this code into the executed script.
Code:
Function _OS $_OS=XP EndFunction Function _CSD $_CSD=1 EndFunction Function _ADMIN $_ADMIN=1 EndFunction Function _HKLMSCCS $_HKLMSCCS=HKLM\SYSTEM\CurrentControlSet EndFunction Function _HKCUSMWCV $_HKCUSMWCV=HKCU\Software\Microsoft\Windows\CurrentVersion EndFunction Function _HKLMSMWCV $_HKLMSMWCV=HKLM\Software\Microsoft\Windows\CurrentVersion EndFunction Function _HKLMSMWNTCV $_HKLMSMWNTCV=HKLM\Software\Microsoft\Windows NT\CurrentVersion EndFunction Function _HKCUSMWNTCV $_HKCUSMWNTCV=HKCU\Software\Microsoft\Windows NT\CurrentVersion EndFunction Function _Desktop $_Desktop=readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','Desktop') EndFunction Function _StartMenu $_StartMenu=readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','StartMenu') EndFunction Function _Favorites $_Favorites=readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','Favorites') EndFunction Function _MyDocuments $_MyDocuments=readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','MyDocuments') EndFunction Function _alldesktop $_alldesktop=expandenvironmentvars(readvalue(_HKLMSMWCV+'\Explorer\User Shell Folders','Common Desktop')) EndFunction Function _allstartmenu $_allstartmenu=expandenvironmentvars(readvalue(_HKLMSMWCV+'\Explorer\User Shell Folders','Common Start Menu')) EndFunction Function _LDAP $_LDAP="LDAP://"+FLAGSHIPCO+"/"+GetObject("LDAP://rootDSE").Get("defaultNamingContext") EndFunction XP
yeah it is adding processing cycles, but nothing that i would wory about to much... that _ldap one might be a big one... but seems to be very fast on my computer.
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 202 anonymous users online.
|
|
|