#119616 - 2004-05-14 09:18 PM
variable type problem
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
I have a problem with return variables. I am looking at a function that can return a array or it can return an error code. I am trying to encapsulate all this into a select Case statement. Here is the code I am working with. Code:
Any Ideas? Function HomeMachine($HomeINIFile, $User, $WkStation) $HomeMachine = -1 $delimiter = "!" $wsDelimiter = "," $workstations = GetINI($HomeIniFile,$User, $delimiter) Select Case Ubound($workstations) <> 0 For Each $Line in $workstations $ndx = IofSubStr($Line,$delimiter,1) If $ndx = 0 MessageBox("Substring (" + $delimiter + ") not found in " + $Line ,"substring not found") EndIf $ndx = IofSubStr($Line,$delimiter,$ndx+1) If $ndx = 0 MessageBox("Substring (" + $delimiter + ") not found in " + $Line ,"substring not found") EndIf ;MessageBox("line is " + $line + " | ndx is " + $ndx + " | Len is " + Len($Line),"info") $strUsersWorkstations = Right($Line,Len($Line)-$ndx) $ret = MessageBox($strUsersWorkstations,"Users Workstations") $Workstations = Split($strUsersWorkstations,$wsDelimiter) For Each $lpWkstation in $workstations If $Wkstation = $lpWkstation $HomeMachine = 1; EndIf Next Next Case ($workstations = 2) $HomeMachine = 2 Case ($workstations = 13) $HomeMachine = 13 EndFunction
Function GetINI($File,optional $Section, optional $d) DIM $Rootdata, $sec, $i, $ii, $secdata, $Error DIM $Tarray[0] If not $d $d = "," EndIf If not Exist($file) $GetINI = 2 Exit EndIf $Rootdata = Split(ReadProfileString($file,$section,""),CHR(10)) If UBound($rootdata) = 0 $GetINI = 13 Exit EndIf ReDIM preserve $rootdata[ubound($rootdata)-1] If not $section $i = 0 For Each $sec In $rootdata $secData = GetINI($file,$sec,$d) $error = @ERROR ReDIM preserve $tarray[UBound($tarray)+UBound($secdata)+1] For $ii = 0 To UBound($secdata) $tarray[$i] = $secdata[$ii] $i = $i + 1 Next Next If UBound($tarray) > 0 ReDIM preserve $tarray[$i-1] EndIf Else ReDIM preserve $tarray[ubound($rootdata)] For $i = 0 To UBound($rootdata) $tarray[$i]=$section+$d+$Rootdata[$i]+$d+ReadProfileString($file,$section,$Rootdata[$i]) Next EndIf $GetINI = $tarray Exit($error) EndFunction
Any Ideas?
|
|
Top
|
|
|
|
#119617 - 2004-05-14 09:54 PM
Re: variable type problem
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
You can use UBOUND to test the return variable to see if it is an array. If the variable is an error code (not an array) UBOUND($var) will return -1.
|
|
Top
|
|
|
|
#119618 - 2004-05-14 09:58 PM
Re: variable type problem
|
Howard Bullock
KiX Supporter
   
Registered: 2000-09-15
Posts: 5809
Loc: Harrisburg, PA USA
|
Not sure why you have this:
Code:
$GetINI = $tarray
Exit($error)
I would think that this would only return the errorcode and never your desired array. I would structure the logic to only "Exit($errorcode)" where an actual error occurred. If you always want to return the error code with data. Then you would have to structure a multi-dimensional array or employ some other array technique.
Edited by Howard Bullock (2004-05-14 10:29 PM)
|
|
Top
|
|
|
|
#119619 - 2004-05-14 10:01 PM
Re: variable type problem
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
cha-ching. That got it. Thanks for all the help.
|
|
Top
|
|
|
|
#119620 - 2004-05-14 10:17 PM
Re: variable type problem
|
burnsc
Starting to like KiXtart
Registered: 2004-04-14
Posts: 171
|
I did some playing with the style of logic you are talking about.
In the style of logic I am used to thinking about an error would 'stop' your code execution. Which in some cases is good and in some cases is bad. The style of allowing it to be seperate acutally requires the programmer to follow proper structure or an error could be missed and all could be thought well. I have to admin I had not looked at the method of how kix handles errors until just now. It is flexible, but I am going to have to go back and modify the few scripts I have done to allow for errors in them. I am less safe than I was thinking without those error handlers.
|
|
Top
|
|
|
|
#119621 - 2004-05-14 10:57 PM
Re: variable type problem
|
Bryce
KiX Supporter
   
Registered: 2000-02-29
Posts: 3167
Loc: Houston TX
|
Burnsc,
i just so happened to be posting a reply to another thread about GetINI() when i saw your post, both in the UDF and here.
it seems that you have a different version of code than what is posted in the UDF VS what you are posting here, especially in regards to the exit() return codes. i am seeing "$getini= 2 exit" in your code, and this could be the source of your problems.
GetINI is a recursive UDF, meaning that it calls it self for each section if no section is given in the first call.
I would suggest that you not return error codes in your data return, but set @error to equal what you want the error code to be by using the exit(#) command.
Bryce
|
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Henriques)
and 2516 anonymous users online.
|
|
|