问题:关于声明变量的VB.net编码标准

时间:2021-06-15 21:20:16

I've seen some variable declare in VB.net in several way like:

我在VB.net中看到过一些变量声明,如:

print("dim _Foo as string");

and print("dim m_Foo as string"); and print("dim foo as string");

并打印(“dim m_Foo as string”);并打印(“dim foo as string”);

I will like to know what's the standard for VB.net coding.

我想知道VB.net编码的标准是什么。

4 个解决方案

#1


2  

It all depends on the scope. In the case of:

这一切都取决于范围。如果是:

Private Dim m_Foo As String

That implies that m_Foo is a member of a class. This also implies the same thing:

这意味着m_Foo是一个类的成员。这也意味着同样的事情:

Private Dim _Foo As String

It's a matter of preference.

这是一个偏好问题。

On the other hand, something like this:

另一方面,这样的事情:

Dim Foo As String

might refer to a variable local to a given method. Some might even prefix it with a "l_":

可能引用给定方法的局部变量。有些人甚至可能用“l_”作为前缀:

Dim l_Foo As String

Declaring like these examples helps in determining scope when scanning code. Putting it all together, here's a sample class showing a well-known naming convention (but not the only one):

像这些示例一样声明有助于确定扫描代码时的范围。总而言之,这是一个示例类,显示了一个众所周知的命名约定(但不是唯一的):

Public Class Bar
    Private m_firstName As String

    Public Sub New(ByVal firstName As String)
        m_firstName = firstName
    End Sub

    Public Function SayGreeting() As String
        Dim l_Greeting As String
        l_Greeting = String.Format("{0}, {1}!", "Hello", m_firstName)
        Return l_Greeting
    End Function
End Class

#2


1  

Using "m_Name" for member variables is standard in VB. I almost never meet someone who doesn't do it that way.

对成员变量使用“m_Name”是VB中的标准。我几乎从未遇到过那种不这样做的人。

Many, but not all, use "s_Name" for Shared variables. This makes it clear how the variable is being used.

许多(但不是全部)对共享变量使用“s_Name”。这清楚地表明了变量的使用方式。

For local variables I use "name". Why? Well a parameter is essentially a local variable and it uses that pattern (as per .NET guidelines), so why not make all local variables look that way?

对于局部变量,我使用“名称”。为什么?好吧,参数本质上是一个局部变量,它使用该模式(根据.NET指南),那么为什么不让所有局部变量看起来那样呢?

#3


1  

I would disagree, I have seen many methds for prefixing class variables including "m", "m_" and _.

我不同意,我已经看到许多用于为类变量添加前缀的方法,包括“m”,“m_”和_。

I have a completely different method that has served me well. I create a structure called backingStore which contains my class variables then I have one Private Variable called Local as new backingStore.

我有一个完全不同的方法,对我很好。我创建了一个名为backingStore的结构,它包含我的类变量,然后我有一个名为Local的私有变量作为新的backingStore。

This allows me to have the same name for the property and the class variable and when referencing it, it is perfectly clear which one I am talking about. ex.

这允许我为属性和类变量使用相同的名称,并且在引用它时,我非常清楚我正在谈论哪一个。恩。

Local.FirstName = "Fred"

Local.FirstName =“Fred”

See complete sample here

请在此处查看完整示例

Design Lunacy: Pain in the m_#$*^

设计精灵:痛苦的m _#$ * ^

#4


0  

It really depends on the naming conventions as agreed in your working environment. There are definitely guidelines available : Naming Conventions for .NET / C# Projects - Which is basically what we follow.

它实际上取决于您工作环境中商定的命名约定。肯定有可用的指南:.NET / C#项目的命名约定 - 这基本上就是我们遵循的。

For example:

_member //Class member - Camel Case
intLocalVariable //note use of Hungarian notation - Camel Case
pMethodParameter //Camel Case
MyProperty //Pascal Case

#1


2  

It all depends on the scope. In the case of:

这一切都取决于范围。如果是:

Private Dim m_Foo As String

That implies that m_Foo is a member of a class. This also implies the same thing:

这意味着m_Foo是一个类的成员。这也意味着同样的事情:

Private Dim _Foo As String

It's a matter of preference.

这是一个偏好问题。

On the other hand, something like this:

另一方面,这样的事情:

Dim Foo As String

might refer to a variable local to a given method. Some might even prefix it with a "l_":

可能引用给定方法的局部变量。有些人甚至可能用“l_”作为前缀:

Dim l_Foo As String

Declaring like these examples helps in determining scope when scanning code. Putting it all together, here's a sample class showing a well-known naming convention (but not the only one):

像这些示例一样声明有助于确定扫描代码时的范围。总而言之,这是一个示例类,显示了一个众所周知的命名约定(但不是唯一的):

Public Class Bar
    Private m_firstName As String

    Public Sub New(ByVal firstName As String)
        m_firstName = firstName
    End Sub

    Public Function SayGreeting() As String
        Dim l_Greeting As String
        l_Greeting = String.Format("{0}, {1}!", "Hello", m_firstName)
        Return l_Greeting
    End Function
End Class

#2


1  

Using "m_Name" for member variables is standard in VB. I almost never meet someone who doesn't do it that way.

对成员变量使用“m_Name”是VB中的标准。我几乎从未遇到过那种不这样做的人。

Many, but not all, use "s_Name" for Shared variables. This makes it clear how the variable is being used.

许多(但不是全部)对共享变量使用“s_Name”。这清楚地表明了变量的使用方式。

For local variables I use "name". Why? Well a parameter is essentially a local variable and it uses that pattern (as per .NET guidelines), so why not make all local variables look that way?

对于局部变量,我使用“名称”。为什么?好吧,参数本质上是一个局部变量,它使用该模式(根据.NET指南),那么为什么不让所有局部变量看起来那样呢?

#3


1  

I would disagree, I have seen many methds for prefixing class variables including "m", "m_" and _.

我不同意,我已经看到许多用于为类变量添加前缀的方法,包括“m”,“m_”和_。

I have a completely different method that has served me well. I create a structure called backingStore which contains my class variables then I have one Private Variable called Local as new backingStore.

我有一个完全不同的方法,对我很好。我创建了一个名为backingStore的结构,它包含我的类变量,然后我有一个名为Local的私有变量作为新的backingStore。

This allows me to have the same name for the property and the class variable and when referencing it, it is perfectly clear which one I am talking about. ex.

这允许我为属性和类变量使用相同的名称,并且在引用它时,我非常清楚我正在谈论哪一个。恩。

Local.FirstName = "Fred"

Local.FirstName =“Fred”

See complete sample here

请在此处查看完整示例

Design Lunacy: Pain in the m_#$*^

设计精灵:痛苦的m _#$ * ^

#4


0  

It really depends on the naming conventions as agreed in your working environment. There are definitely guidelines available : Naming Conventions for .NET / C# Projects - Which is basically what we follow.

它实际上取决于您工作环境中商定的命名约定。肯定有可用的指南:.NET / C#项目的命名约定 - 这基本上就是我们遵循的。

For example:

_member //Class member - Camel Case
intLocalVariable //note use of Hungarian notation - Camel Case
pMethodParameter //Camel Case
MyProperty //Pascal Case