在代码中间声明变量。 [英] Declaring variables in the middle of your code.

查看:71
本文介绍了在代码中间声明变量。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到这几天的趋势是在代码中间而不是顶部声明变量。这有什么好处?

看起来它很难重用变量。


以下是我见过的所有例子远程创建OleDbCommand

对象:

Dim cmd为新的OleDbCommand(Select * FROM Table1,cnn)


我必须弄清楚它和它一样:


Dim cmd为新的OleDbCommand

cmd.CommandText =" SELECT * FROM Table1"

cmd.Connection = cnn


我知道它需要3行,但至少我知道如何在以后重用它。如何在第一个例子中重用变量

?我知道你可以这样做

这个:


''第一次使用

Dim cmd作为新的OleDbCommand(" Select * FROM表1,cnn)


''第二次使用

cmd.CommandText =" SELECT * FROM Table2"

cmd。连接= cnn2


但这看起来不一致,而且我必须弄清楚声明中的哪些属性是什么(命令文本和连接)。


我知道我很挑剔,但这让我烦恼!我老了。或者

也许只是老了!


Chuck。

I''ve noticed that the trend these days is to declare variables in the
middle of code instead of at the top. What is the advantage of this?
It seems like it makes it hard to reuse variables.

Here is how all the examples I''ve seen so far create an OleDbCommand
Object:

Dim cmd as new OleDbCommand("Select * FROM Table1",cnn)

I had to figure out that it was the same as this:

Dim cmd as new OleDbCommand
cmd.CommandText = "SELECT * FROM Table1"
cmd.Connection = cnn

I know it takes 3 lines but at least I know how to reuse it later. How
to you reuse the variable in the first example? I know you could do
this:

''first use
Dim cmd as new OleDbCommand("Select * FROM Table1",cnn)

''second use
cmd.CommandText = "SELECT * FROM Table2"
cmd.Connection = cnn2

But that seems inconsistent, plus I have to figure out which
properties were which in the declaration (CommandText and Connection).

I know I''m being picky but this is bugging me! I''m old school. Or
maybe just old!

Chuck.

推荐答案

它没有无论你在

程序中声明你的局部变量。编译器/运行时将为每个过程设置一个变量声明块

,这发生在任何代码实际运行之前(因为在
..NET中,堆栈是经过精心管理的,并且总是为任何给定的会员固定

电话)。这增加了一定程度的安全性,是有助于防止缓冲区溢出的事情之一。


但是,从您的示例来看,您似乎更感兴趣与

构造函数。构造函数是构建类实例的特殊过程

(当您调用New时)。它们在VB4 / 5/6中不可用,但是它们是b $ b ..NET。除其他外,它们有助于保持代码更紧凑。在某些

的情况下,它们会阻止类实例处于不完整或无效的
状态,因为必须将对象初始化为正确的值,因为它只需要$

已创建。每个班级都可以定义尽可能多的结构程序,每个类别都有不同的参数。如果类提供没有参数的构造函数(大多数情况下是
),则只能使用旧样式

instanciation。有时(但不总是),使用构造函数可能比调用单个属性更快一点了执行速度。这是

不保证。

除了没有无参数(默认)构造函数的情况之外,

是否使用可选构造函数而不是指定一次一个属性,

基本上是一个选择问题。

我肯定有些人会称之为老派:-)


-Rob Teixeira [MVP]


" CR" < CR *** @ hotmail.com>在消息中写道

新闻:1a ************************** @ posting.google.c om ...
It doesn''t matter where you declare your local variables inside your
procedures. The compiler/runtime will set a variable declaration block for
each procedure, and this happens before any code actually runs (because in
..NET, the stack is carefully managed, and always fixed for any given member
call). This adds a level of security and is one of the things that helps
prevent buffer overruns.

However, from your example, it seems you are more interested with
Constructors. Constructors are special procedures that build class instances
(when you call New). They weren''t available as such in VB4/5/6, but are
..NET. They help keep the code more compact, among other things. In certain
cases, they prevent class instances from being in an incomplete or invalid
state because the object must be initialized to correct values as soon as it
is created. Every class can define as many constructure procedures as
necessary, each with different parameters. You can only use the old style
instanciation if the class provides a constructor with no parameters (which
most do). Sometimes (but not always), using the constructor can be a tad bit
faster in execution speed than calling the individual properties. This is
not guaranteed.
Other than the cases where no Parameterless (Default) Constructor exists,
whether you use optional constructors vs. specifying one property at a time,
is basically a matter of choice.
I''m sure some people will call it old-school though :-)

-Rob Teixeira [MVP]

"CR" <cr***@hotmail.com> wrote in message
news:1a**************************@posting.google.c om...
我注意到这些天的趋势是在代码中间而不是在顶部声明变量。这有什么好处?
这似乎很难重用变量。

以下是我到目前为止所见的所有示例创建OleDbCommand
对象:

将cmd视为新的OleDbCommand(Select * FROM Table1,cnn)

我必须弄清楚它与此相同:

Dim cmd as new OleDbCommand
cmd.CommandText =" SELECT * FROM Table1"
cmd.Connection = cnn

我知道它需要3行但是在至少我知道如何在以后重复使用它。如何在第一个例子中重用变量?我知道你可以做到这个:
首次使用
Dim cmd作为新的OleDbCommand(Select * FROM Table1,cnn)

>''第二次使用
cmd.CommandText =" SELECT * FROM Table2"
cmd.Connection = cnn2

但这似乎不一致,另外我必须弄明白哪个<声明中的属性是什么(CommandText和Connection)。

我知道我很挑剔,但这让我烦恼!我老了。或者也许只是老了!

查克。
I''ve noticed that the trend these days is to declare variables in the
middle of code instead of at the top. What is the advantage of this?
It seems like it makes it hard to reuse variables.

Here is how all the examples I''ve seen so far create an OleDbCommand
Object:

Dim cmd as new OleDbCommand("Select * FROM Table1",cnn)

I had to figure out that it was the same as this:

Dim cmd as new OleDbCommand
cmd.CommandText = "SELECT * FROM Table1"
cmd.Connection = cnn

I know it takes 3 lines but at least I know how to reuse it later. How
to you reuse the variable in the first example? I know you could do
this:

''first use
Dim cmd as new OleDbCommand("Select * FROM Table1",cnn)

''second use
cmd.CommandText = "SELECT * FROM Table2"
cmd.Connection = cnn2

But that seems inconsistent, plus I have to figure out which
properties were which in the declaration (CommandText and Connection).

I know I''m being picky but this is bugging me! I''m old school. Or
maybe just old!

Chuck.



CR,
我'已经注意到,这些天的趋势是在代码中间而不是在顶部声明变量。这有什么好处?
我发现变量的声明是由使用它的代码声明的,

使得更容易看到声明是什么。另外,如您的示例所示,它允许您在声明变量时初始化变量。


有时您可以将变量实际范围限定为特定语句,

示例:


''VS.NET 2003语法

For Each index As Integer = 0 to 9

''做一些索引

下一页


''index''变量仅在For语句中有效。

看起来它很难重用变量。
重用变量并不总是一个好主意!


如果你在函数的顶部设置一个变量,那么在底部使用它
$ b $你的函数的b,然后决定重用它在你的

函数的中间。希望你马上找到问题,而不是6个月

以后,当出现问题时......

但这似乎不一致,加上我必须找出哪个
属性是在声明(CommandText和Connection)中的哪些属性。
我发现你的例子是一个很好的例子,说明为什么不重用变量,而不是在哪里重新使用它是个好主意。


我会实际上定义了cmdTable1,cmdTable2变量,这些变量实际上有效地消除了声明中哪些属性的问题。作为

你知道什么是变量,你不需要担心什么时候

是这个变量这个&什么时候。此外,我会考虑有两个

例程,SelectTable1& SelectTable2创建&执行了

命令...


注意当我想重用我通常做的变量(名称)时:

昏暗cmd为OleDbCommand
''首先使用
cmd =新OleDbCommand(" Select * FROM Table1",cnn)
''第二次使用
cmd =新OleDbCommand(" SELECT * FROM Table2",cnn2)


注意我正在重新初始化变量本身,以避免重用

对象,这可能会导致问题。


我希望你注意的另一个趋势是使用更小的更专用的

功能,而不是更大的更通用的功能。


Martin Fowler的书重构 - 改进现有代码的设计

由Addision Wesley http://www.refactoring.com ,在代码中提供了许多气味和/ b
以及如何纠正它们。我没有看到他明确建议的地方

通过使用声明,但它看起来确实是一个

隐含的做法。此外,他还通过更大的更通用的功能/对象实践来推广更小的专用函数/对象




希望这有助于

Jay


" CR" < CR *** @ hotmail.com>在消息中写道

新闻:1a ************************** @ posting.google.c om ...我注意到现在的趋势是在代码中间而不是在顶部声明变量。这有什么好处?
这似乎很难重用变量。

以下是我到目前为止所见的所有示例创建OleDbCommand
对象:

将cmd视为新的OleDbCommand(Select * FROM Table1,cnn)

我必须弄清楚它与此相同:

Dim cmd as new OleDbCommand
cmd.CommandText =" SELECT * FROM Table1"
cmd.Connection = cnn

我知道它需要3行但是在至少我知道如何在以后重复使用它。如何在第一个例子中重用变量?我知道你可以做到这个:
首次使用
Dim cmd作为新的OleDbCommand(Select * FROM Table1,cnn)

>''第二次使用
cmd.CommandText =" SELECT * FROM Table2"
cmd.Connection = cnn2

但这似乎不一致,另外我必须弄明白哪个<声明中的属性是什么(CommandText和Connection)。

我知道我很挑剔,但这让我烦恼!我老了。或者
也许只是老了!

Chuck。
I''ve noticed that the trend these days is to declare variables in the
middle of code instead of at the top. What is the advantage of this? I find having declaration of the variable is by the code that uses it,
making it easier to see what the declaration is. Also as your example shows
it allows you to initialize the variable when you declare it.

Sometimes you can actually scope the variable to the specific statement, for
example:

'' VS.NET 2003 syntax
For Each index As Integer = 0 to 9
'' do something with index
Next

The ''index'' variable is only valid within the For statement.
It seems like it makes it hard to reuse variables. Reusing variables is not always a good idea!

If you set a variable at the top of your function, then use it at the bottom
of your function, then later decide to "reuse" it in the middle of your
function. Hopefully you find the problem right away, rather then 6 months
later, when a problem occurs...
But that seems inconsistent, plus I have to figure out which
properties were which in the declaration (CommandText and Connection). I find your example a good example of why NOT to reuse a variable, not where
reusing it is a good idea.

I would actually define cmdTable1, cmdTable2 variables which effectively
eliminates your problem of "which properties were in the declaration"). As
you know what is in which variable, and you don''t need to worry about when
is this variable this & when is it that. Further I would consider having two
routines, SelectTable1 & SelectTable2 that created & executed the
commands...

Note when I do want to reuse the variable (name) I normally do:
Dim cmd as OleDbCommand ''first use cmd = New OleDbCommand("Select * FROM Table1",cnn)
''second use cmd = New OleDbCommand("SELECT * FROM Table2", cnn2)

Notice I am reinitializing the variable itself as to avoid reusing the
object, which can cause problems.

Another trend I hope you are noticing is to have smaller more dedicated
functions, rather then larger more general functions.

Martin Fowler''s book "Refactoring - Improving the Design of Existing Code"
by Addision Wesley http://www.refactoring.com, offers a number of ''smells''
in code and how to correct them. I don''t see where he explicitly suggests to
put the declaration by the use, however it does appear that it is an
implicit practice. Also he promotes the smaller dedicated functions/object
over the larger more general function/object practice of coding.

Hope this helps
Jay

"CR" <cr***@hotmail.com> wrote in message
news:1a**************************@posting.google.c om... I''ve noticed that the trend these days is to declare variables in the
middle of code instead of at the top. What is the advantage of this?
It seems like it makes it hard to reuse variables.

Here is how all the examples I''ve seen so far create an OleDbCommand
Object:

Dim cmd as new OleDbCommand("Select * FROM Table1",cnn)

I had to figure out that it was the same as this:

Dim cmd as new OleDbCommand
cmd.CommandText = "SELECT * FROM Table1"
cmd.Connection = cnn

I know it takes 3 lines but at least I know how to reuse it later. How
to you reuse the variable in the first example? I know you could do
this:

''first use
Dim cmd as new OleDbCommand("Select * FROM Table1",cnn)

''second use
cmd.CommandText = "SELECT * FROM Table2"
cmd.Connection = cnn2

But that seems inconsistent, plus I have to figure out which
properties were which in the declaration (CommandText and Connection).

I know I''m being picky but this is bugging me! I''m old school. Or
maybe just old!

Chuck.



你好CR,


一个旧式的答案,
Hi CR,

A old style answer,
我注意到这几天的趋势是在代码中间而不是在顶部声明变量。这有什么好处?
似乎很难重用变量。


从今天起计算机中的100字节是什么。

好​​处是你的变量总是新的,你不需要

测试你是否可以在那一刻使用它们。
''首先使用
Dim cmd作为新的OleDbCommand(Select * FROM Table1,cnn)

''第二次使用
cmd.CommandText =" SELECT * FROM Table2"
cmd.Connection = cnn2
I''ve noticed that the trend these days is to declare variables in the
middle of code instead of at the top. What is the advantage of this?
It seems like it makes it hard to reuse variables.
What is 100bytes in a computer from today.
The benefit is that your variables are always new and you do not have to
test if you can use them at that moment.
''first use
Dim cmd as new OleDbCommand("Select * FROM Table1",cnn)

''second use
cmd.CommandText = "SELECT * FROM Table2"
cmd.Connection = cnn2



通过这种方式你可以多次使用你的命令。

例如,当你在数据库中构建一个新表时


我认为老派可能还年轻,可以改变。否则它就不会再让你好了。


使用VB.net你可以根据需要做有效的事情

as;它经常运行,是一次性程序,是等等。


我希望这能给出一个想法吗?


Cor


In this way you can use your command a lot of times.
By instance when you are building a new table in the database

I think old school however probably young enough to change. Otherwise it
would not botter you anymore.

With VB.net you can do things as effective as you want, depending on things
as; is it often running, is it a one time program, is it etc etc.

I hope this gives an idea?

Cor


这篇关于在代码中间声明变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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