问题描述
我注意到这几天的趋势是在代码中间而不是顶部声明变量。这有什么好处?
看起来它很难重用变量。
以下是我见过的所有例子远程创建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.
推荐答案
通过这种方式你可以多次使用你的命令。
例如,当你在数据库中构建一个新表时
我认为老派可能还年轻,可以改变。否则它就不会再让你好了。
使用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
这篇关于在代码中间声明变量。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!