Results 1 to 10 of 10

Thread: General Question About Functions in VB

  1. #1

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Hi all.

    Does anyone know if you can use pointers to functions in VB? Or anything similar?

    What I want ideally is to have a UDT containing a variable which somehow can be used to call the correct function relevant to the other variables in the type. Does that make sense?

    What I really want is something like:

    Code:
    dim map(1 to 10) as ReportType
    call setMap    'a function to setup the array map()
                   'a bit like a constructor I guess
    for x = 1 to 10
       map(x).function 'would be map[x]->function in C I think
    next x
    Maybe I can do something fancy using a class instead of a type. I've never really done OOP in VB so I don't know.


    Since I'm on the subject, does anyone know if you can use any pointers in VB?
    Harry.

    "From one thing, know ten thousand things."

  2. #2
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Yep, you would have to go for classmodules instead:

    Add a classmodule

    add your properties there as you add public variables or properties to a form.

    make your methods

    Function yourfunction
    blabla
    End function

    to instance your class you use new keyword

    Dim map(1 to 10) as new classname

    or

    dim map(1 to 10) as classname
    ..
    set map(1) = new classname
    set map(2) = new classname
    ..

    To call the function could do the following
    for x = 1 to 10
    map(x).yourfunction
    next x


    [Edited by kedaman on 08-09-2000 at 08:49 AM]
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  3. #3
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    VB passes arrays by ref, (pointer to first element), so a generic function could populate the array for you.

    You can also use the AddressOf keyword in VB. I'm not sure but I think it allows you to pass Function Pointers. Have a look at Help, I'm off to read up on it now. If no-one else posts I will respond further.

  4. #4
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    oops, just read up Help on Function Pointers...and...
    Seems I was looking at passing pointers to DLL's

    Quote from MSDN:

    "Basic to Basic" function pointers are not supported. Pointers to Visual Basic functions cannot be passed within Visual Basic itself. Currently, only pointers from Visual Basic to a DLL function are supported.


  5. #5

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    kedaman, the thing is I want to be able to call the function through a variable, not the function's name. Or something else that works is fine.

    I want

    map(1).function

    and

    map(2).function

    to call different functions, deending on the data contained in map(1) and map(2).

    Thanks for your help Steven, can you think of any way to get around this?

    I should also mention that I'm using Excel VBA here, not actual VB. Not by choice, it's just all they've given me to work with.
    Harry.

    "From one thing, know ten thousand things."

  6. #6
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184
    I can only suggest using a CASE-SELECT construct to determine the function to call based on the value contained in map(x).

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    Each object can have different data, so you could make your functions to behave according to the data
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  8. #8
    Addicted Member
    Join Date
    Jul 2000
    Location
    Scotland
    Posts
    184

    Talking

    thats what I said

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    ah, sorry
    Use
    writing software in C++ is like driving rivets into steel beam with a toothpick.
    writing haskell makes your life easier:
    reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
    To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.

  10. #10

    Thread Starter
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Hmm, yes I suppose that's as close as I'm going to get.

    Thanks a lot guys, you've given me some ideas and I'll go try them out now
    Harry.

    "From one thing, know ten thousand things."

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