Results 1 to 11 of 11

Thread: A better way....

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    2

    Question

    Is there a more efficient way to write this code:

    If num >= 2000 And num <= 2035 Then
    lblResponse.Caption = "correct"
    Call correct
    Else
    If num >= 2100 And num <= 2135 Then
    lblResponse.Caption = "correct"
    Call correct
    Else
    If num >= 2200 And num <= 2235 Then
    lblResponse.Caption = "correct"
    Call correct
    Else

    .....and the code goes on and on (for a couple pages)with numbers ending always with
    ##00 and ##35.

    TIA

  2. #2
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    How about a loop?
    Code:
    For i = 2000 to 6000 Step 100
        If num >= i And num <= (i+35) Then
            lblResponse.Caption = "correct"
            Correct
        End If
    Next i
    It may not be more efficient, but it looks quite nice
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  3. #3
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    If Right(num, 2) >= 0 And Right(num, 2) <= 35 Then
        lblResponse.Caption = "correct"
    Call correct
    End If
    [Edited by HeSaidJoe on 10-04-2000 at 05:42 PM]
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  4. #4
    Hyperactive Member barrk's Avatar
    Join Date
    Sep 2000
    Location
    My own little world
    Posts
    274
    Try this:

    n = 2000
    do while n <= 5000 'or however far you need to go
    If num >= n And num <= (n + 35) Then
    lblResponse.Caption = "correct"
    Call correct
    end if

    n = n + 100
    loop

  5. #5
    Guest
    Try this:

    Code:
    If (num Mod 1000 >= 0) And (num Mod 1000 <= 35) Then
        lblResponse.Caption = "correct" 
        Call correct 
    End If
    I think it's the most efficient way...

    HeSaidJoe: Aren't you using Evil Type Conversion ?
    Parksie: I don't think you understood the question?

    Enjoy...

  6. #6
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    My code does exactly the same as the rest...so what do you think the question was?

    Although ntropee is now spoilt for choice with about 3 different methods
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  7. #7
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Say what?
    HeSaidJoe: Aren't you using Evil Type Conversion ?

    If you are looking for a number between 0 and 35 as the
    2 right digits of any number why wouldn't it work?
    Anytime num is passed it checks the last 2 digits and if
    they fall within 0 to 35 then correction is called.
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  8. #8
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    I think he means that you're converting from number to string to number, rather than comparing numbers.

    RobIII - Instruction-wise, + is faster than Mod, since less CPU instructions are needed (1 as opposed to about 4). Also, the compiler can optimise "through-the-loop".
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  9. #9
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    that's all...who cares..it works and it's less than oodles and ooldles of pages.

    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

  10. #10
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Yeah. In these times of 1GHz processors, I think smaller, more readable code is more important than sheer speed. Anyway, there won't be much in it with so few runs through the loop (you'd need to take thousands before there was much difference).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  11. #11

    Thread Starter
    New Member
    Join Date
    Jul 2000
    Posts
    2

    Talking thanks

    Thanks for the response..... i cut down my code from 3 pages to just a couple lines.

    I love this forum : )

Posting Permissions

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



Click Here to Expand Forum to Full Width