Page 1 of 1 1
Topic Options
#119616 - 2004-05-14 09:18 PM variable type problem
burnsc Offline
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 Offline
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.
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#119618 - 2004-05-14 09:58 PM Re: variable type problem
Howard Bullock Offline
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)
_________________________
Home page: http://www.kixhelp.com/hb/

Top
#119619 - 2004-05-14 10:01 PM Re: variable type problem
burnsc Offline
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 Offline
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 Offline
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
#119622 - 2004-05-15 05:29 AM Re: variable type problem
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
BTW, EXIT is a command. not a function, thus correct syntaxt is EXIT 0, as is documented in the KiXtart Manual.
_________________________
There are two types of vessels, submarines and targets.

Top
#119623 - 2004-05-15 03:41 PM Re: variable type problem
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Ja, waddup widdat? Trying to force precedence on a single expression?

Borrowing from the manual:
Quote:

Operator precedence
When several operations occur in an expression, KiXtart evaluates and resolves each part of the expression in a predetermined order. This predetermined order is known as operator precedence.

Parentheses can be used to override the order of precedence and force some parts of an expression to be evaluated before other parts. Operations within parentheses are always performed before those outside the parentheses. Within parentheses standard operator precedence is maintained.

The precedence of operators affects the grouping and evaluation of operands in expressions. Expressions with higher-precedence operators are evaluated first.



_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
Page 1 of 1 1


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

Who's Online
1 registered (Henriques) and 2516 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.168 seconds in which 0.098 seconds were spent on a total of 12 queries. Zlib compression enabled.

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