-
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?
-
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]
-
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.
-
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.
-
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.
-
I can only suggest using a CASE-SELECT construct to determine the function to call based on the value contained in map(x).
-
Each object can have different data, so you could make your functions to behave according to the data
-
-
-
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 :)