Page 2 of 2 <12
Topic Options
#119339 - 2004-05-12 10:16 PM Re: Define constants with kixtart
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Rad,

Did some tweaks with your code (XP)..
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(_Desktop ,"readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','Desktop')")
const(_StartMenu ,"readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','Start Menu')")
const(_Favorites ,"readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','Favorites')")
const(_MyDocuments ,"readvalue(_HKCUSMWCV+'\Explorer\Shell Folders','Personal')")
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: " _OS
? "SP: " _CSD
? "Admin: " _ADMIN
? "Current Control Set: " _HKLMSCCS
? "Current Version: " _HKCUSMWCV
? "Desktop: " _Desktop
? "Start Menu: " _StartMenu
? "Favorites: " _Favorites
? "My Documents: " _MyDocuments
? "All StartMenu: " _allstartmenu
? "All Desktop: " _alldesktop
? "LDAP: " _LDAP
get $

Function Const($Constant,$Value)
Exit Execute('Function ' + $Constant + ' $' + $Constant + '=' + $Value + ' EndFunction')
EndFunction



Thanks,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#119340 - 2004-05-12 10:32 PM Re: Define constants with kixtart
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
So what am I missing here?

Why is this better or where is it more useful?

const(_OS ,split(@ProductType)[1])
 vs.
$OS =split(@ProductType)[1]

etc...

Top
#119341 - 2004-05-12 11:08 PM Re: Define constants with kixtart
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
I would think programming styles would be the big reason to use something like this, for example if you use NoVarsInStrings and Explicit.

Using this Const() UDF would hardcode a value into a function call, as seem by some of the examples, that value could be a complex process.

Otherwise, you would need to DIM every constant variable that you use, and would have also be sure to reuse those variables later on in your script.

Bryce

Top
#119342 - 2004-05-13 12:01 AM Re: Define constants with kixtart
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
I see it as a way to guarantee that a value is... well constant since everytime it is used it is re-evaluated.

realistically, we all do it the way you are doing it now.. we always have been. But this promises to ensure that we do not accidentally reuse a var somewhere a screw something up.

At least until Ruud provides real consts

I still think it uses too much overhead... creating all those functions for each const and then calling that funct each time a const is used.
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#119343 - 2004-05-13 12:36 AM Re: Define constants with kixtart
NTDOC Administrator Offline
Administrator
*****

Registered: 2000-07-28
Posts: 11628
Loc: CA
GLOBAL $VAR is pretty much a CONSTANT unless you DIM it within another scope, and it does not have that overhead associated with it.

I see just as many possible issues of messing up the scope with this method as I do with $VAR method.

Having some real constants might be useful in some cases, thus your $VAR or this UDF method could not overwrite them accidentally, but your also then starting to lose some of the freeform of the language that most of us really enjoy.

Top
#119344 - 2004-05-13 01:37 AM Re: Define constants with kixtart
Bryce Offline
KiX Supporter
*****

Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
I see something like this really coming into play where you want a dynamic constant ...... What I mean is....you are expect a given type of data to be returned, but that actual data value can change over time.

I agree that the global variable is much less overhead. but when dealing with simple values, I had to loop into the 100k mark to stat to really notice a difference in @msecs between the 2 methods.

Has anyone ever have any dealings with FORTH?

Top
#119345 - 2004-05-13 05:20 AM Re: Define constants with kixtart
Radimus Moderator Offline
Moderator
*****

Registered: 2000-01-06
Posts: 5187
Loc: Tampa, FL
I had to do something like this to detect opening RAS connections... I had to spawn a new shell and pass a value back
_________________________
How to ask questions the smart way <-----------> Before you ask

Top
#119346 - 2004-05-13 10:12 AM Re: Define constants with kixtart
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
hmm...
the overheat...
and the errors...
and the const symbol...

k, first, I think ! is better for const

@macro
$var
!const

personal opinion...

anyways, the errors and the overheat are due to same problem.
instead of passing the value to the const, we pass the code with which it can be evaluated.
so, basically, the udf would need to be more like:

Function Const($Constant,$Value)
Exit Execute('Function ' + $Constant + ' $' + $Constant + '=' + iif(vartype($Value)=8,'"'+$Value+'"',$Value) + ' EndFunction')
EndFunction

I admit, this goes little more complex supporting many vartypes, but this way the enum is done only once and if long query is passed as value, this way the runtime reduces a lot.
_________________________
!

download KiXnet

Top
Page 2 of 2 <12


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

Who's Online
0 registered and 657 anonymous users online.
Newest Members
M_Moore, BeeEm, min_seow, Audio, Hoschi
17883 Registered Users

Generated in 0.105 seconds in which 0.07 seconds were spent on a total of 13 queries. Zlib compression enabled.

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