vba 在 Visual Basic 中使用冒号将两个语句放在同一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1411711/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 09:59:59  来源:igfitidea点击:

Using colons to put two statements on the same line in Visual Basic

vb.netvbavb6

提问by JaredPar

Is it considered bad practice to use colons to put two statements on the same line in Visual Basic?

在 Visual Basic 中使用冒号将两个语句放在同一行是否被认为是不好的做法?

回答by JaredPar

There is nothing inherently wrong with using the colon to combine statements. It really depends on the context but as long as it doesn't reduce readability, there is nothing wrong with it.

使用冒号来组合语句并没有本质上的错误。这确实取决于上下文,但只要不降低可读性,就没有问题。

As a general rule I avoid using the colon for this purpose. I find it's more readable to have one statement per line. However this is not a colon specific issue. I avoid doing the same with a semi-colon in C# or C++. It's just a personal preference.

作为一般规则,我避免为此目的使用冒号。我发现每行有一个语句更具可读性。然而,这不是冒号特定的问题。我避免在 C# 或 C++ 中用分号做同样的事情。这只是个人喜好。

回答by Smandoli

It's a good practice in moderation, because sometimes readability is enhanced by concatenating two lines:

这是适度的好习惯,因为有时通过连接两行来增强可读性:

  • when the lines are short and intimately related
  • when the lines are short and trivial

    Option Compare Database:  Option Explicit   ''My favorite!
    rsDataSet.Close:          Set rsDataSet= Nothing
    
  • 当台词简短且密切相关时
  • 当线条简短而琐碎时

    Option Compare Database:  Option Explicit   ''My favorite!
    rsDataSet.Close:          Set rsDataSet= Nothing
    

Don't do it if:

如果出现以下情况,请不要这样做:

  • it hurts readability.
  • it complicates debugging. Control structures such as If...Thenneed to stay clean. You'll be glad you kept it simple when it's time to set a break point.
  • it compromises future editing. Often you want to keep sections portable. Moving or re-structuring a block of code is easily hindered by attempts to minimize your code.
  • 它损害了可读性。
  • 它使调试复杂化。控制结构等If...Then需要保持清洁。您会很高兴在设置断点时保持简单。
  • 它会影响未来的编辑。通常,您希望保持部分可移植。尝试最小化代码很容易阻碍移动或重构代码块。

回答by AngryHacker

In general, I'd advise against it, as it makes for busier code.

一般来说,我建议不要这样做,因为它会使代码更繁忙。

However, for simple tasks, there is nothing wrong with it. For instance:

但是,对于简单的任务,它没有任何问题。例如:

for i = 1 to 10: ProcessFoo(i): next

I think a line like this is short enough not to cause confusion.

我认为这样的一行足够短,不会引起混乱。

回答by jrcs3

I'll take the other side. I don't like dense lines of code. It is easier to skim code when lines are not combined.

我会站在另一边。我不喜欢密集的代码行。当行不合并时,更容易浏览代码。

Combining statements also makes it easier to create long functions that still fit on a single screen.

组合语句还可以更轻松地创建仍然适合单个屏幕的长函数。

It isn't a major sin, I just don't like it.

这不是什么大罪,我只是不喜欢它。

I also don't like one line Ifstatements.

我也不喜欢一行If语句。

回答by Oorang

To me you shouldn't say "never do thus", you should just say "If you do this, a possible problem is such and such." Then just weigh the pros and cons for yourself. The pro is brevity/few lines of code. Sometimes this can aid readability. For instance some people use it to do vb.Net declarations:

对我来说,你不应该说“永远不要这样做”,而应该说“如果你这样做,可能的问题就是这样那样。” 然后自己权衡利弊。优点是简洁/几行代码。有时这有助于提高可读性。例如有些人用它来做 vb.Net 声明:

Dim x As Long: x = 1

Or wait loops:

或等待循环:

Do Until IE.ReadyState = READYSTATE_COMPLETE: DoEvents: Loop

But obviously you can really make it rough on someone too:

但很明显,你也可以让某人变得粗暴:

Public Sub DoYouKnowWhatThisDoes()
    MsgBox Example
End Sub

Private Function Example()
    Const s$ = "078243185105164060193114247147243200250160004134202029132090174000215255134164128142"
    Const w% = 3: Const l% = 42: Dim i%, r$: For i = 1 To l Step w: r = r & ChrW$(Mid$(s, i, w) Xor Mid$(s, i + l, w)): Next: Example = r
End Function

Another practical reason that you might not want to use this approach is breakpoints. Breakpoints can only be set by the line. So if you have several things executing on the same line you can't isolate the second thing. It will stop on the first statement. (This is also one of the reasons some people don't like single line ifs.) It just complicates debugging a little.

您可能不想使用这种方法的另一个实际原因是断点。断点只能由行设置。因此,如果您在同一行上执行了几件事,则无法隔离第二件事。它将在第一个语句处停止。(这也是有些人不喜欢单行 ifs 的原因之一。)它只是使调试复杂化了一点。

I usually don't use colons in production code for this reason. However I do use them to improve the brevity of "copy/paste" code that I post in forums and elsewhere. YMMV:)

由于这个原因,我通常不在生产代码中使用冒号。但是,我确实使用它们来改进我在论坛和其他地方发布的“复制/粘贴”代码的简洁性。天啊:)

回答by Dan Henderson

I realize this is a very old question, but it was the first result on my Google search, so I hope I can be forgiven for chiming in here.

我意识到这是一个非常古老的问题,但它是我谷歌搜索的第一个结果,所以我希望我能在这里插话得到原谅。

There is one situation (exactly what led me here in fact) in which this approach is not only useful, it's the only way to accomplish the desired result: the Immediate window. Any code you want to execute in the Immediate window must all be on one line. So in order to use any form of Do, Case, For, While, or With in the Immediate window, you will need to use colons.

在一种情况下(实际上正是将我带到这里的原因)这种方法不仅有用,而且是实现所需结果的唯一方法:立即窗口。您要在立即窗口中执行的任何代码都必须在一行上。因此,为了在立即窗口中使用任何形式的 Do、Case、For、While 或 With,您需要使用冒号。

回答by Tony Toews

I've only ever used it when I'm clsoing a recordset and setting the variable to nothing. I figure one line instead of two gives me more lines of code on the screen and doesn't detract from readability.

我只在关闭记录集并将变量设置为空时才使用它。我认为一行而不是两行在屏幕上给了我更多的代码行并且不会降低可读性。

I've seen it used in simple select cases such as the following but that would be as far as I would go.

我已经看到它在简单的选择情况下使用,例如以下,但就我而言。

 Select Case success
      Case ERROR_FILE_NO_ASSOCIATION: msg = "no association"
      Case ERROR_FILE_NOT_FOUND: msg = "file not found"
      Case ERROR_PATH_NOT_FOUND: msg = "path not found"
      Case ERROR_BAD_FORMAT:     msg = "bad format"

from http://vbnet.mvps.org/index.html?code/system/findexecutable.htm

来自http://vbnet.mvps.org/index.html?code/system/findexecutable.htm

And even then I would've lined up the "msg =" portion.

即便如此,我也会排列“msg =”部分。

回答by Mike Woodhouse

It isconsidered bad practice in most of the sites at which I have worked. And by most of the VB developers with whom I have worked. And in my head. If I see it, I will admit that I would almost certainly change it. I say "almost" because I admit there's a possibility that I could find a piece of code that looked better that way. I don't expect to see it in my lifetime, though.

在我工作过的大多数站点中,这认为是不好的做法。以及与我共事过的大多数 VB 开发人员。在我的脑海里。如果我看到它,我会承认我几乎肯定会改变它。我说“几乎”是因为我承认我有可能找到一段看起来更好的代码。不过,我不希望在我有生之年看到它。

I also really don't like one-line **If**s either.

我也真的不喜欢一行 ** If**s。

Both are most likely hangovers from the days of VGA (640x480) monitors; that's no excuse these days.

两者都很有可能是 VGA (640x480) 显示器时代的宿醉;这些天这不是借口。

回答by VikingProgrammer

I've never seen this mentioned in an official capacity at any of the companies which I've worked. But I do think that using colons excessively can start to make your code less readable and more of a pain to maintain.

在我工作过的任何一家公司,我从未见过以官方身份提及此事。但我确实认为过度使用冒号会使您的代码可读性降低,并且维护起来更麻烦。

I do tend to use these myself at times, for example when checking for cancel in one of my recent projects:

我自己有时确实倾向于使用这些,例如在我最近的一个项目中检查取消时:

If _bCancel Then Status = CancelProcess() : Return Status

Putting this in kept my code readable than the alternative IF block.

将其放入可以使我的代码比替代 IF 块更易读。

But it can be taken too far, I've recently inherited a project which is replete with examples of taking colon usage too far :

但它可以走得太远,我最近继承了一个项目,该项目充满了使用冒号的例子:

    Select Case GetStringValue(Index).Trim.ToLower
        Case "yes", "y" : GetBooleanValue = True
        Case "no", "n" : GetBooleanValue = False
        Case Else : GetBooleanValue = Nothing
    End Select

Personally I find the above to be a bit much.

我个人觉得上面说的有点多。

回答by DontPanic345

I like this one

我喜欢这个

Using pro As New Process() : With pro

        ...

    End With
End Using