访问全局变量。什么是最好的方法? [英] Access to global variables. What is the best way?

查看:74
本文介绍了访问全局变量。什么是最好的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现一次又一次我需要在一个公共变量中访问

信息。与此同时,我阅读的书籍表明,人们不应该过多使用公共变量 - 这很糟糕

编程实践。有一个简单的方法来处理这个?我希望

在最佳实践中做一些事情。方式,但同时我不想要

来制作一个联邦案例。


这种情况​​一次又一次地出现,因为例如,使用像

Infragistics这样的产品。如果我对网格中的记录执行某些操作,我需要确定

我们在输入事件中正在讨论的记录,然后将该值设为

值在After Bla bla事件中。


因此我没有30个不同的公共变量,什么是更好但是

仍然相对简单的方式这样做?或者说这些公共变量是否真的那么糟糕?

I am finding that time after time I have instances where I need to access
information in a variable that is public. At the same time, the books I
read say that one should not use public variables too much - that it''s bad
programming practice. Is there an easy way to deal with this? I would like
to do things in the "Best Practices" way but at the same time I don''t want
to make a federal case out of it.

This comes up over and over again, for example, when using products like
Infragistics. If I do something to a record in a grid, I need to identify
which record we''re talking about in the enter event, and then make that
value available in the After Bla bla event.

So that I don''t have 30 different public variables, what''s a better but
still relatively simple way of doing this? Or is it really that bad to have
all those public variables?

推荐答案

Woody,

我认为这是一个我们不时陷入困境的陷阱。

它主要与来自使用的编程环境有关

支持全局变量可能是预先OOP。


在OOP世界中。你不需要这个,只要你有你的b / b
类/容器,访问器方法等正常工作。也许你需要

重新考虑你的设计,或者它已经走得太远了?

问候 - OHM


Woody Splawn写道:
Woody,
I think this is a trap we all fall into from time to time.
And its mostly to do with coming from a programming environment which used
to support Global Variables maybe pre OOP.

In the OOP world. You wont need this provided that you have your
classes/containers,accessor methods etc worked out properly. Maybe you need
to re-think your design, or has it gone too far for this ?
Regards - OHM

Woody Splawn wrote:
我发现一次又一次我需要在一个公共变量中访问信息的实例。与此同时,我读过的书说不应该使用公共变量太多 - 这是不好的编程习惯。有没有简单的方法来处理这个问题?我想用最佳实践的方式做事,但与此同时我不想用它来制作联邦案件。

这种情况一次又一次地出现,例如,当使用像Infragistics这样的产品时。如果我对网格中的记录执行某些操作,我需要确定我们在enter事件中正在讨论的记录,然后
然后在After Bla bla事件中使该值可用。 br />
所以我没有30个不同的公共变量,什么是更好的
但仍然是相对简单的方法呢?或者拥有所有这些公共变量真的很糟糕吗?
I am finding that time after time I have instances where I need to
access information in a variable that is public. At the same time,
the books I read say that one should not use public variables too
much - that it''s bad programming practice. Is there an easy way to
deal with this? I would like to do things in the "Best Practices"
way but at the same time I don''t want to make a federal case out of
it.

This comes up over and over again, for example, when using products
like Infragistics. If I do something to a record in a grid, I need to
identify which record we''re talking about in the enter event, and
then make that value available in the After Bla bla event.

So that I don''t have 30 different public variables, what''s a better
but still relatively simple way of doing this? Or is it really that
bad to have all those public variables?




-

最诚挚的问候 - OHM

O_H_M {at} BTInternet {dot} com



--
Best Regards - OHM

O_H_M{at}BTInternet{dot}com




也许你将全局变量与公共变量混合在一起。


全局变量(在VB6中使用global调用)

在VB.NET中public shared变量。


例如:


public Class MyClass1

public shared MyGlobalVariable as string''是全局的变量。

结束班级


您现在可以使用MyClass1.MyGlobalVariable而无需创建

类MyClass1的实例。 />

这是有些人认为的糟糕的设计


正常的公共变量我认为不是很糟糕的设计。


像这样:


public Class MyClass1

public MyVariable as string''是一个公共变量但不是Global

结束班

但是有人说'那'也是坏的,使用属性而不是'


我的意见是:


如果可以的话,避免使用公共共享变量。

但是在某些情况下,公共共享

变量的设计更好! !


公共共享变量是危险的,因为你可以c在代码的任何部分中改变它的价值

,这可能会导致很难找到的错误。


想想一下大学你(或你自己)

将一些共享公众设置为一个值,使你的代码崩溃

在另一个没有任何关系的代码部分你正在做什么。


所以如果变量属于他们应该属于的对象,那就更好了。

to。


我的建议:

只要有规则,请遵守规则,但不要盲目跟随

支付任何费用.... ...

如果你很聪明,请记住,如果必要的话,应该打破任何规则。



" Woody Splawn" < WO *** @ splawns.com> schrieb im Newsbeitrag

新闻:es ************** @ TK2MSFTNGP09.phx.gbl ...

Maybe you mix global variables with public variables.

global variables (in VB6 called with "global")
are in VB.NET "public shared" Variables.

for example:

public Class MyClass1
public shared MyGlobalVariable as string ''is a global Variable.
End Class

You now can use MyClass1.MyGlobalVariable without making a instance of the
Class MyClass1.

This is what some people think "a bad design"

Normal Public Variables aren''t a bad design i think.

Like this:

public Class MyClass1
public MyVariable as string ''is a public Variable but not Global
End Class
But there are people saying "that''s also Bad, use Properties instead"

My Opinion is:

Avoid public shared Variables if you can.
But there are some situations where the design is better with public shared
variables !!

Public shared Variables are dangerous because you can change the value of it
in any Part of the Code and that can be lead to bugs that are hard to find.

Think about the situation that a College of you (or Youself)
sets some of those Shared Publics to a Value that makes your Code crashing
in an other Code part that hasn''t anything to do with what you are doing.

So it''s better if the Variables belong to the Object that they should belong
to.

My Advice:
Follow the Rule as long as it makes sence, but do not follow it blind and
for any cost.......
If you are smart, keep in mind the any rule should be broken if it''s
necessary.


"Woody Splawn" <wo***@splawns.com> schrieb im Newsbeitrag
news:es**************@TK2MSFTNGP09.phx.gbl...
我发现时间经过一段时间我需要访问公共变量中的信息。与此同时,我读过的书说不应该过多地使用公共变量 - 这是编程实践的不好。有一个简单的方法来处理这个?我希望b $ b喜欢在最佳实践中做一些事情。方式,但同时我不想要用它来制造联邦案件。

这种情况一次又一次地出现,例如,当使用像 Infragistics。如果我对网格中的记录执行某些操作,我需要确定
我们在enter事件中正在讨论哪条记录,然后在After Bla bla事件中使该值可用。

所以我没有30个不同的公共变量,哪个更好但是
仍然是相对简单的方法呢?或者,
是否包含所有这些公共变量真的很糟糕?

I am finding that time after time I have instances where I need to access
information in a variable that is public. At the same time, the books I
read say that one should not use public variables too much - that it''s bad
programming practice. Is there an easy way to deal with this? I would like to do things in the "Best Practices" way but at the same time I don''t want
to make a federal case out of it.

This comes up over and over again, for example, when using products like
Infragistics. If I do something to a record in a grid, I need to identify
which record we''re talking about in the enter event, and then make that
value available in the After Bla bla event.

So that I don''t have 30 different public variables, what''s a better but
still relatively simple way of doing this? Or is it really that bad to have all those public variables?



Infragistics。如果我对网格中的记录执行某些操作,我需要确定我们在输入事件中正在讨论哪条记录,然后在After Bla bla事件中使该值可用。


对于这个例子,你不需要PUBLIC变量。


网格是一个表格。 (表单只是一种类)


因此在表格中声明您的变量是私有的


类MyForm

继承Windows.Forms.Form


私有selectedRow为行

private bla as string

private blub as integer


结束班

如果你需要和表格类之外的那个

创建属性来访问这些变量。 />

" Woody Splawn" < WO *** @ splawns.com> schrieb im Newsbeitrag

新闻:es ************** @ TK2MSFTNGP09.phx.gbl ...我发现一次又一次我有需要的实例访问公共变量中的信息。与此同时,我读过的书说不应该过多地使用公共变量 - 这是编程实践的不好。有一个简单的方法来处理这个?我希望b $ b喜欢在最佳实践中做一些事情。方式,但同时我不想要用它来制造联邦案件。

这种情况一次又一次地出现,例如,当使用像 Infragistics。如果我对网格中的记录执行某些操作,我需要确定
我们在enter事件中正在讨论哪条记录,然后在After Bla bla事件中使该值可用。

所以我没有30个不同的公共变量,哪个更好但是
仍然是相对简单的方法呢?或者,对于
,所有这些公共变量真的很糟糕吗?

Infragistics. If I do something to a record in a grid, I need to identify
which record we''re talking about in the enter event, and then make that
value available in the After Bla bla event.
For this example you don''t need PUBLIC Variables.

The Grid is in a Form. (A Form is simply a kind of Class)

So declare your Variables as private in the Form

Class MyForm
inherits Windows.Forms.Form

private selectedRow as row
private bla as string
private blub as integer

end class
If you need and of this outside of the Form Class
the create Properties to access these Variables.

"Woody Splawn" <wo***@splawns.com> schrieb im Newsbeitrag
news:es**************@TK2MSFTNGP09.phx.gbl... I am finding that time after time I have instances where I need to access
information in a variable that is public. At the same time, the books I
read say that one should not use public variables too much - that it''s bad
programming practice. Is there an easy way to deal with this? I would like to do things in the "Best Practices" way but at the same time I don''t want
to make a federal case out of it.

This comes up over and over again, for example, when using products like
Infragistics. If I do something to a record in a grid, I need to identify
which record we''re talking about in the enter event, and then make that
value available in the After Bla bla event.

So that I don''t have 30 different public variables, what''s a better but
still relatively simple way of doing this? Or is it really that bad to have all those public variables?



这篇关于访问全局变量。什么是最好的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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