Results 1 to 7 of 7

Thread: Sub routine Tables

  1. #1
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Sub routine Tables

    I was thinking about creating a table which contains a list of sub/Functions routines.

    Is that possible? I wanted to avoid having multiple IF's or using SELECT CASE, this should make script execution faster and smaller.

    Pseudo Code...
    In the example I create the table of routines then check a value, this value is also my offset for my table, it then calls the routine.
    All that with one check.
    [code=thinbasic]
    DIM Routines(3) AS DWORD

    Routines(1) = Test1,Test2,Test3

    IF ButtonClicked > 0 THEN CALL Routines(ButtonClicked)

    SUB Test1()
    ... Do something ...
    END SUB

    SUB Test2()
    ... Do something ...
    END SUB

    SUB Test3()
    ... Do something ...
    END SUB
    [/code]
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  2. #2
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,779
    Rep Power
    10

    Re: Sub routine Tables

    Should work but with string. See following:

    [code=thinbasic]
    DIM Routines(3) AS string
    dim ButtonClicked as long = 1
    dim FunctionToCall as string

    Routines(1) = "Test1", "Test2", "Test3"


    IF ButtonClicked > 0 THEN
    FunctionToCall = Routines(ButtonClicked) '---Compose the name as preferred
    CALL FunctionToCall
    end if

    SUB Test1()
    msgbox 0, "Test 1"
    END SUB

    SUB Test2()
    msgbox 0, "Test 2"
    END SUB

    SUB Test3()
    msgbox 0, "Test 3"
    end sub
    [/code]

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  3. #3
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,779
    Rep Power
    10

    Re: Sub routine Tables

    You can also have parameters if you need some:

    [code=thinbasic]
    randomize

    dim ButtonClicked as long = rnd(1, 3)
    dim FunctionToCall as string


    IF ButtonClicked > 0 THEN
    FunctionToCall = "Test" & ButtonClicked '---Compose the name as preferred
    CALL FunctionToCall(timer)
    end if

    SUB Test1(optional byval MyParam as ext)
    msgbox 0, "Test 1" & $crlf & "MyParam equal to: " & MyParam
    END SUB

    SUB Test2(optional byval MyParam as long)
    msgbox 0, "Test 2"
    END SUB

    SUB Test3(optional byval MyParam as long)
    msgbox 0, "Test 3"
    end sub
    [/code]
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  4. #4
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Sub routine Tables

    thanks Eros I will test when I get home tonight.
    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  5. #5
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,779
    Rep Power
    10

    Re: Sub routine Tables

    OK, perfect.
    While I was checking ... worth to mention also function_exists utility. So above example become:
    [code=thinbasic]
    randomize

    dim ButtonClicked as long = rnd(1, 5)
    dim FunctionToCall as string


    IF ButtonClicked > 0 THEN
    FunctionToCall = "Test" & ButtonClicked '---Compose the name as preferred

    '---Check if function name exists. If yes, call it, otherwise error.
    if function_exists(FunctionToCall) then
    CALL FunctionToCall(timer)
    else
    msgbox 0, FunctionToCall & " does not exists."
    end if
    end if

    SUB Test1(optional byval MyParam as ext)
    msgbox 0, "Test 1" & $crlf & "MyParam equal to: " & MyParam
    END SUB

    SUB Test2(optional byval MyParam as long)
    msgbox 0, "Test 2"
    END SUB

    SUB Test3(optional byval MyParam as long)
    msgbox 0, "Test 3"
    end sub
    [/code]

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

  6. #6
    thinBasic MVPs
    Join Date
    May 2007
    Location
    UK
    Posts
    1,427
    Rep Power
    159

    Re: Sub routine Tables

    thanks works perfect for me, seems quite obvious how to do it now I see your examples.

    Home Desktop : Windows 7 - Intel Pentium (D) - 3.0 Ghz - 2GB - Geforce 6800GS
    Home Laptop : WinXP Pro SP3 - Intel Centrino Duo - 1.73 Ghz - 2 GB - Intel GMA 950
    Home Laptop : Windows 10 - Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz, 2401 Mhz, 2 Core(s), 4 Logical Processor(s) - 4 GB - Intel HD 4400
    Work Desktop : Windows 10 - Intel I7 - 4 Ghz - 8GB - Quadro Fx 370

  7. #7
    thinBasic author ErosOlmi's Avatar
    Join Date
    Sep 2004
    Location
    Milan - Italy
    Age
    57
    Posts
    8,779
    Rep Power
    10

    Re: Sub routine Tables

    Good.

    I will implement a bit CALL help. It is not clear immediately the power of this statement and the sequence it uses to decide what to call.
    The string expression containing the name of the function to call can contain any function name: script user function, declared API name functions, thinBasic keywords. Just a bit of attention must be taken into considerations about parameters.

    Ciao
    Eros
    www.thinbasic.com | www.thinbasic.com/community/ | help.thinbasic.com
    Windows 10 Pro for Workstations 64bit - 32 GB - Intel(R) Xeon(R) W-10855M CPU @ 2.80GHz - NVIDIA Quadro RTX 3000

Similar Threads

  1. error routine
    By marino in forum Samples for help file
    Replies: 1
    Last Post: 31-08-2010, 13:02

Members who have read this thread: 0

There are no members to list at the moment.

Posting Permissions

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