GOTO语句和返回结果的方式 [英] GOTO statement and return results way

查看:289
本文介绍了GOTO语句和返回结果的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,


我有一个关于臭名昭着的GOTO声明的问题以及从sub返回

结果的方法:

我有一个子程序必须调用外部COM方法,并且

因为这些方法可能会失败我必须检查它们运行正常,比如

这个:


子方法(byval param1为整数,....,BYREF结果为String)


dim ... < br $> b $ b ..

..

result = callCOM1(params)

如果result = -1则''bad结果

result =" callCOM1 failed,dude!"

GOTO ENDPOINT

..

..

result = callCOM2(params)

如果result = -1则''结果不好

result =" callCOM2 failed,dude!" ;

GOTO ENDPOINT

..

..

终点:


''进行一些处理,以及额外的任务


end sub


然后,CALLER函数将检查结果var并且将继续作为

需要。

我选择了一个sub而不是一个函数,因为我认为函数必须

返回其他信息比功能执行好或不执行,作为数据集,

应用程序对象等。


第二个疑问是GOTO:你知道,它是代码中有一个坏的伤口,但是我用
作为退出子的快速方法。


你如何改变所有这些以使其更加结构化美学?也许

创建一个所有处置完成的子? - 然后我必须通过所有

将要处置的变量或让他们公开/朋友8-(...


谢谢提前,


Roger Tranchez

MCTS

..NET 2005和DB开发人员

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

推荐答案

Roger,


你想要的是1970年以前。


Just创建一个你所谓的端点功能,然后调用那个结尾然后使用

返回具有相同的效果。


Cor


" Roger Tranchez< ru ***** @ community.nospamschreef in bericht

news:52 ********** ************************ @ microsof t.com ...
Roger,

What you want is from before 1970.

Just Create an, what you call "Endpoint function", call that end then use
"Return" that has the same effect.

Cor

"Roger Tranchez" <ru*****@community.nospamschreef in bericht
news:52**********************************@microsof t.com...

您好,


我有一个关于臭名昭着的GOTO声明的问题以及返回的方式

a

来自sub:


我有一个sub,必须调用外部COM方法,并且

因为这些m方法可能会失败我必须检查它们是否正常运行,例如

这个:


子方法(byval param1 as integer,....,BYREF结果为字符串)


昏暗...





result = callCOM1( params)

如果result = -1那么''结果不好

result =" callCOM1 failed,dude!"

GOTO ENDPOINT





result = callCOM2(params)

如果result = -1则''结果不好

result =" callCOM2 failed,dude!"

GOTO ENDPOINT






终点:


''进行一些处理,以及额外的任务


end sub


然后,CALLER函数将检查RESULT var并继续执行



我选择了一个sub而不是我认为的函数一个函数必须

返回除函数执行正常或不正常之外的其他信息,作为数据集,

appl ication对象等


第二个疑问是GOTO:你知道,这是一个很糟糕的代码,但是我用b $ b来快速使用它退出子。


你会如何改变这一切,使其更具结构性和美感?也许

创建一个所有处置完成的子? - 然后我必须通过

所有

要处置的变量或让他们公开/朋友8-(...


在此先感谢,


Roger Tranchez

MCTS

.NET 2005和DB开发人员



第二个疑问是GOTO:你知道,它在代码中是一个坏的伤口,但是我
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 ?



当我有一些函数按顺序执行并且我想退出

第一次失败时,我使用两种结构中的一种 - ''嵌套如果''或''做一次' '。我

喜欢''嵌套if''当我的功能很少时,我更喜欢''做一次''当我有很多时候。


假设函数是x1,x2和x3,''嵌套if''是:


x1

如果成功Th en

x2

如果成功则

x3

如果成功则

' '处理所有成功

结束如果

结束如果

结束如果


''做一次'':




x1

如果失败然后退出做

x2

如果失败然后退出运行

x3

如果失败然后退出运行

''在此处理所有成功

循环直到真


这些构造编译成与你的代码类似的''goto

endpoint'',但是源代码避免了goto'和标签'。我更喜欢他们而不是

goto''因为我认为他们更容易理解为新人或

你回到代码后不看它持续了几个月。

When I have some functions to perform in sequence and I want to quit on the
first failure, I use one of two constructs - ''nested if'' or ''do once''. I
prefer ''nested if'' when I have few functions, I prefer ''do once'' when I have
many.

Assuming the functions are x1, x2, and x3, ''nested if'' is:

x1
If Success Then
x2
If Success Then
x3
If Success Then
'' processing here for all success
End If
End If
End If

''Do Once'':

Do
x1
If Fail Then Exit Do
x2
If Fail Then Exit Do
x3
If Fail Then Exit Do
'' processing here for all success
Loop Until True

These constructs compile to code that is similar to yours with ''goto
endpoint'', but the source code avoids goto''s and label''s. I prefer them over
goto''s because I think they are easier to understand for a new person or for
you when you come back to the code after not looking at it for a few months.




-

Roger Tranchez

MCTS

..NET 2005和DB开发人员

" AMercer"写道:

--
Roger Tranchez
MCTS
..NET 2005 and DB developer
"AMercer" wrote:

第二个疑问是GOTO:你知道,这是代码中的一个坏伤,但我

使用它作为退出子的快速方式。

你会如何改变这一切,使其更具结构性和美感?
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 ?



当我有一些函数按顺序执行并且我想退出

第一次失败时,我使用了两种结构中的一种 - ''嵌套if''或''做一次''。我没有'b $ b'喜欢''嵌套if''当我的功能很少时,我更喜欢''做一次''当我有很多的时候。

假设函数是x1,x2和x3,''嵌套if''是:


x1

如果成功那么

x2

如果成功则

x3

如果成功则

''在这里处理一切顺利

结束如果

结束如果

结束如果


''做一次' ':




x1

如果失败然后退出做

x2

如果失败然后退出运行

x3

如果失败然后退出运行

''在这里处理所有成功

Loop Until True


这些构造编译成与你的代码类似的''goto

endpoint'',但源代码避免了转到'和标签的。我更喜欢他们而不是

goto''因为我认为他们更容易理解为新人或

你回到代码后不看它持续了几个月。


When I have some functions to perform in sequence and I want to quit on the
first failure, I use one of two constructs - ''nested if'' or ''do once''. I
prefer ''nested if'' when I have few functions, I prefer ''do once'' when I have
many.

Assuming the functions are x1, x2, and x3, ''nested if'' is:

x1
If Success Then
x2
If Success Then
x3
If Success Then
'' processing here for all success
End If
End If
End If

''Do Once'':

Do
x1
If Fail Then Exit Do
x2
If Fail Then Exit Do
x3
If Fail Then Exit Do
'' processing here for all success
Loop Until True

These constructs compile to code that is similar to yours with ''goto
endpoint'', but the source code avoids goto''s and label''s. I prefer them over
goto''s because I think they are easier to understand for a new person or for
you when you come back to the code after not looking at it for a few months.


这篇关于GOTO语句和返回结果的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆