R
Roger Tranchez
Hello,
I have a question about the infamous GOTO statement and the way to return a
result from a sub:
I have a sub that has to make some calls to external COM methods, and
because these methods can fail I have to check them to be running ok, like
this:
sub method(byval param1 as integer, ...., BYREF result as String)
dim ...
..
..
result=callCOM1(params)
if result=-1 then 'bad result
result="callCOM1 failed, dude!"
GOTO ENDPOINT
..
..
result=callCOM2(params)
if result=-1 then 'bad result
result="callCOM2 failed, dude!"
GOTO ENDPOINT
..
..
ENDPOINT:
'make some dispose, and additional tasks
end sub
Then, the CALLER function will check the RESULT var and will proceed as
required.
I choosed a sub instead of a function as I think that a function have to
return other information than "function executed ok or not", as datasets,
application objects etc.
The second doubt is the GOTO: you know, it is a bad wound in code, but I
use it as a fast way to exit the sub.
How would you change all this to be more structured and aesthetic ? maybe
creating a sub where all the dispose is done ? --> then I'd have to pass all
the vars to be disposed or make them public/friend 8-( ...
Thanks in advance,
Roger Tranchez
MCTS
..NET 2005 and DB developer
I have a question about the infamous GOTO statement and the way to return a
result from a sub:
I have a sub that has to make some calls to external COM methods, and
because these methods can fail I have to check them to be running ok, like
this:
sub method(byval param1 as integer, ...., BYREF result as String)
dim ...
..
..
result=callCOM1(params)
if result=-1 then 'bad result
result="callCOM1 failed, dude!"
GOTO ENDPOINT
..
..
result=callCOM2(params)
if result=-1 then 'bad result
result="callCOM2 failed, dude!"
GOTO ENDPOINT
..
..
ENDPOINT:
'make some dispose, and additional tasks
end sub
Then, the CALLER function will check the RESULT var and will proceed as
required.
I choosed a sub instead of a function as I think that a function have to
return other information than "function executed ok or not", as datasets,
application objects etc.
The second doubt is the GOTO: you know, it is a bad wound in code, but I
use it as a fast way to exit the sub.
How would you change all this to be more structured and aesthetic ? maybe
creating a sub where all the dispose is done ? --> then I'd have to pass all
the vars to be disposed or make them public/friend 8-( ...
Thanks in advance,
Roger Tranchez
MCTS
..NET 2005 and DB developer