| Author |
Message |
victorf
Joined: 01 Jan 2006
Location: Schenectady, New York
|
|
Posted: 29 January 2006, 20:52 PM Post subject: Question on Booleans and string passing |
|
|
Here are 3 questions
1. If UserFldElUnits is defined as
| Code: |
dim UserFldElUnits as Boolean
Is this a legal statement:
if UserFldElUnits then
Call PutLine(CStr(UserFldEl) + " Ft/Min")
else
Call PutLine (CStr(UserFldEl) + " M/Min")
End if
|
or do I have to say:
| Code: |
if UserFldElUnits = True then
Call PutLine(CStr(UserFldEl) + " Ft/Min")
else
Call PutLine (CStr(UserFldEl) + " M/Min")
End if
|
2. If subroutine PutLine is defined:
| Code: |
Sub PutLine(ByRef InBuf as String)
|
can the string be passed as I show above?
3. can a CByte() be used to convert a Boolean to a
byte?
Any enlightenment will be appreciated.
Vic |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Location: Austin, TX
|
|
Posted: 29 January 2006, 23:05 PM Post subject: |
|
|
My first response would be "what does the compiler say about this code". So that's what I tried and here are your answers:
1. The '= true' is implicit and is not needed. However strings are concatentatred using the & operator - not the + operator. See section 2.4.6 of the manual: http://www.zbasic.net/doc/ZBasicRef/ZBasicRef25.html
2. No - you need to assign the result to a variable and pass that variable into the function call. If not you get a compiler error about invalid use of expression for a ByRef parameter. Remember ByVal passes the value on the stack which can either be an intermediate expression or from a variable and ByRef passes a reference which is the address of a variable. See section 2.14 of the manual http://www.zbasic.net/doc/ZBasicRef/ZBasicRef45.html
3. This page from the reference manual should answer your question: http://www.zbasic.net/doc/ZBasicSysLib/ZBasicSysLib30.html. |
|
| Back to top |
|
 |
victorf
Joined: 01 Jan 2006
Location: Schenectady, New York
|
|
Posted: 31 January 2006, 1:42 AM Post subject: |
|
|
Sorry, I had my Pascal hat on I guess
| Quote: |
2. No - you need to assign the result to a variable and pass that variable into the function call.
|
I think I knew this but was hoping I could get away with my way just to save declaring a variable in the subroutine that the expression came from.
Anyway, thank you for taking the time to respond to my questions.
Vic |
|
| Back to top |
|
 |
spamiam
Joined: 13 Nov 2005
|
|
Posted: 31 January 2006, 14:46 PM Post subject: |
|
|
| victorf wrote: | ...was hoping I could get away with my way just to save declaring a variable in the subroutine that the expression came from.
|
I would have tried to pass a concatenation just the way you did (using the & operator of course). In some dialects of BASIC it probably will work the way you intended.
I guess this is not one of them.
-Tony |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Location: Austin, TX
|
|
Posted: 31 January 2006, 16:37 PM Post subject: |
|
|
| spamiam wrote: | I would have tried to pass a concatenation just the way you did (using the & operator of course). In some dialects of BASIC it probably will work the way you intended.
I guess this is not one of them. |
Less sophisticated versions of Basic may only support ByVal parameters, so this will always work. However they don't support arrays for example that need to be passed by reference.
ZBasic is based on Microsoft's Visual Basic and supports both ByVal and ByRef parameters. In other languages that you may be more used to such as C these are passing parameters by value and by address. In any case it is important to match the type otherwise you will get garbage. The checking in ZBasic will tell you when you have made an error as it did this in case.
Of course another way for Vic to change his solution is to have PutLine subroutine take a ByVal string parameter but there are pros and cons to this approach depending on the rest of his program. |
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Location: Portland, OR
|
|
Posted: 01 February 2006, 16:49 PM Post subject: |
|
|
| Quote: | | I think I knew this but was hoping I could get away with my way just to save declaring a variable in the subroutine that the expression came from. |
Parameter passing 'ByRef' is implemented by passing the address of the referent variable to the procedure. The compiler could support the passing of a value other than a variable by automatically creating a temporary variable to hold the value and then passing the address of the temp. Although BasicX doesn't support this, it appears that VB does. |
|
| Back to top |
|
 |
|