VB.NET 中如何命名成员变量?

发布于 2024-09-04 18:03:02 字数 667 浏览 4 评论 0 原文

我一般不会对变量命名、代码格式等问题进行主观争论。所以我无意在这里展开争论。

我刚刚遇到这篇(旧)博客文章建议不要为成员变量名称添加前缀:

请勿不要使用成员前缀 变量(_、m_、s_ 等)。如果你 想要区分本地和 您应该使用的成员变量 “这。”在 C# 和“我”中。在 VB.NET 中。

对于 C#,是的,我明白了:成员变量可以采用小驼峰命名法,公共属性/方法可以采用 PascalCase 命名法。但 VB.NET 不区分大小写,因此您不能真正为私有成员指定与公共属性相同的名称,除非首字母小写。

我通常在成员变量前加上下划线,但有人告诉我这不是惯用的。

所以我真的很好奇:如何在 VB.NET 中命名成员变量?有没有“标准”的方式?

我问这个问题并不是因为我相信有一个“正确”的方法,或者因为我特别想改变我的风格,当然也不是因为我想告诉别人他们是“错的”。就像我说的,我只是好奇。

I am generally not one to engage in subjective arguments over matters like variable naming, code formatting, etc. So I have no intention of starting an argument here.

I just came across this (old) blog post which recommends not prefixing member variable names:

Do not use a prefix for member
variables (_, m_, s_, etc.). If you
want to distinguish between local and
member variables you should use
"this." in C# and "Me." in VB.NET.

For C#, yeah, I get it: member variables can be lower camelCase, and public properties/methods can be PascalCase. But VB.NET is case-insensitive, so you can't really give a private member the same name as a public property except with a lower case first letter.

I've generally prefixed member variables with an underscore, but I've been told that's not idiomatic.

So really I'm just curious: how do you name your member variables in VB.NET? And is there a "standard" way?

I'm not asking because I believe there's a "right" way or because I particularly want to change my style, and certainly not because I have any desire to tell others they're "wrong." Like I said, I'm just curious.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

靑春怀旧 2024-09-11 18:03:02

这是个人喜好,尽管人们普遍支持有一些区别。即使在 C# 中,我也不认为存在一种广泛使用的约定。

杰夫·普罗西斯

根据个人喜好,我通常在私有字段前添加下划线[在 C# 中]...此约定在 .NET 框架中使用较多,但并未在整个框架中使用。

来自 .NET Framework 设计指南第二版第 73 页。

杰弗里·里希特

我将所有字段设为私有,并在实例字段前加上“m_”前缀,在静态字段前加上“s_”[在 C# 中]

来自 .NET Framework 设计指南 第二版第 47 页。 Anthony Moore (BCL 团队) 也认为使用“m_”和“s_”值得考虑,第 48 页。

It's personal preference, although there's widespread support for having some distinction. Even in C# I don't think there's one widely used convention.

Jeff Prosise says

As a matter of personal preference I typically prefix private fields with an underscore [in C#] ... This convention is used quite a lot in the .NET framework but it is not used throughout.

From the .NET Framework Design Guidelines 2nd Edition page 73.

Jeffrey Richter says

I make all my fields private and I prefix my instance fields with "m_" and my static fields with "s_" [in C#]

From the .NET Framework Design Guidelines 2nd Edition page 47. Anthony Moore (BCL team) also thinks using "m_" and "s_" is worth consideration, page 48.

旧街凉风 2024-09-11 18:03:02

我正在像你一样这样做。

Private _myVar as Object

Public Property MyVar() As Object
    Get
        Return Me._myVar 
    End Get
    Set(ByVal value As Object)
        Me._myVar = value
    End Set
End Property

在构造函数中

Public Sub New(myVar as object)
   Me._myVar = myVar
End Sub

但我认为这是一个品味问题。

I'm doing it like you.

Private _myVar as Object

Public Property MyVar() As Object
    Get
        Return Me._myVar 
    End Get
    Set(ByVal value As Object)
        Me._myVar = value
    End Set
End Property

And in constructor

Public Sub New(myVar as object)
   Me._myVar = myVar
End Sub

But I think that's a matter of taste.

紙鸢 2024-09-11 18:03:02

我个人使用 m_ 作为成员变量。

尽管 VS 2010 中具有自动属性,但我最近编写的任何新代码都不需要这样做。

I personally use m_ for member variables.

Although with automatic properties in VS 2010 I haven't needed to for any new code I've written recently.

極樂鬼 2024-09-11 18:03:02

我不喜欢用下划线开始行/名称,因为这总是看起来好像该行被额外的空格缩进了:它只会使代码不平衡。此外,对于我的口味来说,单独的下划线太不显眼了:我更喜欢标识符清晰可见。

因此,我定期在后缀下划线(例如 example_)和前缀 m_ 之间循环。我无法决定我更喜欢哪一个,因为我实际上都不喜欢。但反对前缀下划线的论点部分也适用于后缀下划线。

但正如您所说,某种的区别是必要的

正如我在其他地方说过的,我在 C# 中仅区分大小写方面也有过非常糟糕的经历 - 很容易混淆名称,因此写入私有变量而不是属性。如果属性检查或转换设置值,这一点很重要。

因此,我也更喜欢在 C# 中使用某种前缀。

I don’t like starting a line/name with an underscore since that always looks as if the line were indented by an additional space: it just makes the code unbalanced. Additionally, a lonely underscore is too inconspicuous for my taste: I prefer the identifiers to be clearly distinct.

Therefore, I periodically cycle between suffix underscore (e.g. example_) and prefix m_. I can’t decide which of those I prefer since I actually like neither. But the argument against prefix underscores partially also applies to suffix underscores.

But as you’ve remarked, some kind of distinction is necessary.

And as I’ve remarked elsewhere, I’ve had very bad experiences with case-only distinction in C# as well – it’s just too easy to confuse the names, and hence write into a private variable instead of the property. This matters if the property either checks or transforms the set value.

For that reason, I prefer to use some kind of prefix in C# as well.

闻呓 2024-09-11 18:03:02

我唯一一次使用前缀是在公共属性的私有后备存储中。在这些情况下,名称在其他方面是相同的,并且大多数情况下,我唯一引用前缀名称的地方是在其关联的属性内。当我最终可以使用 VB.Net 自动实现的属性时,我什至不需要这样做。

当我不能只使用自动实现的属性时,我也会在 C# 中执行此操作。 _ 前缀比仅根据大小写改变名称更好。

The only time I use a prefix is with the private backing store for a public property. In these cases, the names are otherwise identical and most of the time the only place I'll ever reference the prefixed name is inside it's associated property. When I can finally use auto-implemented properties with VB.Net I won't even need to do that.

I do this in C# as well, on those instances when I can't just use an auto-implemented property. Better the _ prefix than varying the names only by case.

日暮斜阳 2024-09-11 18:03:02

我们使用 _(下划线)作为变量名称的前缀。它很短而且切中要点...

Private _ID as integer
Public Property ID() As Integer
    Get
        Return _ID
    End Get
    Set(ByVal value As Integer)
        _ID = value
    End Set
End Property

We use _ (underscore) to prefix our variables names. It's short and to the point...

Private _ID as integer
Public Property ID() As Integer
    Get
        Return _ID
    End Get
    Set(ByVal value As Integer)
        _ID = value
    End Set
End Property
毅然前行 2024-09-11 18:03:02

虽然很多 MS 代码似乎使用 m_* 进行私有声明,但我为自己保存了一个字符,只使用 _name 作为私有成员。我的规则:

  • 私有成员前面有一个下划线
  • 公共成员(方法和属性)是 PascalCase。
  • 参数采用驼峰命名法。

由于我使用 C# 工作,因此参数名称与大小写不同的属性名称相同是没有问题的。但这在 VB 中行不通。

Although a lot of the MS code seems to use m_* for private declarations, I save myself a character and just use _name for private members. My rules:

  • Private members are preceeded by an underscore
  • Public members (methods and properties) are PascalCase.
  • Parameters are camelCase.

Since I work in C#, having a parameter name with the same name as a property with different case is no problem. That won't work in VB, though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文