Page 1 of 2 12 LastLast
Results 1 to 15 of 18
  1. #1
    Lounger
    Join Date
    Mar 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Semantic (XL2003)

    Hi all,

    Beeing a c programmer I'm a little confused; because while this is allowed i c:

    int a = 2, b = 4;
    bool bResult;
    bResult = (a > [img]/forums/images/smilies/cool.gif[/img];

    // Result is F.

    and:

    int f()
    { return -1; }

    int iResult = 0;
    if((iResult = f()) > 17)
    {}
    else
    {}

    // Evaluates to F.

    The same in VBA seems to generate exceptions:

    Dim bResult as Boolean
    bResult = (a > [img]/forums/images/smilies/cool.gif[/img]

    and

    If((iResult = f) > 17) then
    Else
    End If


    Am I dooing anything wrong?

    Thanks in advance.

  2. #2
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Semantic (XL2003)

    Have you switched " require variable declaration" on? (tools, options, editor)

    If you have this line in VB(A):

    Dim A, B as Integer

    You Dim A as a Variant and B as an integer. The correct syntax is:

    Dim A As Integer, B As Integer

    I tend to prefer:

    Dim A As Integer
    Dim B As Integer
    ...

    VB wants a full word, so use False, not just f

    Not sure what (bResult=f)>17 is supposed to evaluate to. bResult is either True or False, so the comparison too is either True or False and never greater than 17 ?!?!?

    Without seeing all relevant parts of your code and the error messages you get it is hard to judge what's wrong though.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  3. #3
    Lounger
    Join Date
    Mar 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Semantic (XL2003)

    I know that I have to use True and False (T/F is only in comments) and for the other part, it says: (iResult=f)>17 and not (bResult=f)>17... *S*

  4. #4
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Semantic (XL2003)

    It might help if you expalined what you are doing and what doesn't work (I don't know anything about c). I get no errors with the code itself (in XL97). What "exceptions" are you getting?

    This section:
    Dim bResult as Boolean
    bResult = (a > [img]/forums/images/smilies/cool.gif[/img]

    will put in bResult a True if a>b otherwise it will place a False in the variable. What results are you getting?

    Your second construction

    If((iResult = f) > 17) then
    Else
    End If

    will always go to the "ELSE" part no matter what f or result is.

    If Result = f (ie TRUE) you are comparing to see:
    True>17 which is false, so the code goes to the ELSE

    If Result <> f (ie Result = f is FALSE) you are comparing to see:
    False>17 which is also false, so the code goes to the ELSE portion of the code.

    Steve

  5. #5
    Lounger
    Join Date
    Mar 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Semantic (XL2003)

    OK perfect; I've switched 'require variable declaration' on now - it was'nt. And for my confusions - I think that I understand now seeing the answers. It IS in fact allowed to make assignments in if's and to avoid silly constructions like:

    if(something > 0) Then
    bMyResult = True
    else
    bMyResult = False
    Endif

    and instead use:

    bMyResult = (something > 0)

    Thank you for your kind help!

    Sincerely,
    Michael.

  6. #6
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Semantic (XL2003)

    As Steve said: your test is rather pointless, because it always evaluates to false.
    I must say I dislike the idea of comparing an integer to a boolean, even if VB seems to accept this.

    It is better to implicitly convert the things to the same type before doing math with them.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  7. #7
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Semantic (XL2003)

    I agree.

    In XL97-VB, it also seems more sensitive to comparing "unlike things" and is more likely to yield "type mismatch" when you compare variables of different types...

    Steve

  8. #8
    Lounger
    Join Date
    Mar 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Semantic (XL2003)

    OK I don't know what's going on here? I just posted an answer and it seems to have been removed(???) - But anyway, what I said was - Please: Read what I posted to begin with. I was comparing int and int OR bool and bool and nothing else. I'm not confusing any types here! My concern was only some simple semantic.

  9. #9
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Semantic (XL2003)

    Please read your private messages ("PostBox" in the Lounge menu)

  10. #10
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Semantic (XL2003)

    I did read your post.
    iResult=False sounded like a type mixup to me, so that is why I responded that way.
    I apologize if I was wrong.
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  11. #11
    Lounger
    Join Date
    Mar 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Semantic (XL2003)

    I didn't mean to be unpolite - sorry if I was. I think you or I confused what '=' was meaning? Is it a compare OR an assignment? In c/c++ the problem never arise because we have: '=' as assign and '==' as compare op.

    So you can have:

    if( ( b = func()) == 100 ) ...

    My problem was the same in VBA - could I have

    if( (b = func () ) = 17) ' Or > or < or ...

    Semantic...

    Sincerely,
    Michael.

  12. #12
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Semantic (XL2003)

    In VBA, = is used both for assignment and for comparison. It's sloppy, but that's the way it has been since the beginnings of BASIC in the '70s of last century. An= is interpreted as assignment ONLY in a line of the form

    a = b

    If there is something before the a, such as If, the = is automatically interpreted as comparison. So in

    If (iResult = f) > 17 Then

    the return value of f is NOT assigned to iResult. Instead, iResult is compared to the return value of f, resulting in a boolean: True if iResult equals the return value of f, False otherwise. This boolean is then compared to the integer 17.

  13. #13
    Platinum Lounger
    Join Date
    Feb 2001
    Location
    Weert, Limburg, Netherlands
    Posts
    4,812
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Semantic (XL2003)

    I see your point with the = versus ==.

    [quibble]
    But still, if iResult=False was meant as an assignment, and even if it were a comparison (which it is in the if clause), it is still a type mixup, either comparing, or assigning a boolean value to an integer variable.
    [/quibble]
    Jan Karel Pieterse
    Microsoft Excel MVP, WMVP
    www.jkp-ads.com
    Professional Office Developers Association

  14. #14
    Lounger
    Join Date
    Mar 2005
    Posts
    32
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Semantic (XL2003)

    Thank you for all your kind answers. Now I follow - and 'no' I did _not_ assign any boolean to iResult. I mixed up if's interpretation of '=' - in VBA we must do things line-by-line so to speak unlike in c.

  15. #15
    WS Lounge VIP sdckapr's Avatar
    Join Date
    Jul 2002
    Location
    Pittsburgh, Pennsylvania, USA
    Posts
    11,225
    Thanks
    14
    Thanked 342 Times in 335 Posts

    Re: Semantic (XL2003)

    <P ID="edit" class=small>(Edited by sdckapr on 15-Apr-05 06:58. Corrected mistake)</P>In VB the "=" acts as either depending on "context"

    A=12
    will put 12 in the variable A

    if( (b = func () ) = 17)

    is valid in VB. It will first do the comparison in parentheses:
    b = func ()

    If they are the same it will yield a TRUE, if they are different it will yield a false.

    But in either case, it will then compare this boolean to 17 and it will always be false. since neither true or false are equal to 17 (note they are also neither < 17, but they are both >17.

    Are you trying to do:
    b = func () ' to assign in the variable
    If b = 17 then

    Or you could just use (without the extra variable)
    If func () = 17 then

    Steve

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •