全局变量 [英] global variable

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

问题描述

在阅读之后,c#似乎没有使用全局变量。很多

也对他们提出建议。所以我想知道如何解决这个问题。

我试图计算,保留结果,做另一个

计算然后将结果添加到我的保留值等我的价值观:

我支持然后铺设马匹。所以我先回来,如果它赢了,那么返回

£20。然后我用一个抵消的赌注给马打了一个给我一个
的总回报1英镑。好吧,所以我需要保留这个价值,我把它放在文本框a中。

。现在说同一匹马我再打赌
。同样下注20英镑,如果它赢了,除了我必须加上我的1英镑给

£21。好到目前为止,但是我必须保留这笔赌注的20英镑结果,因为它使用
来计算抵消赌注。不是21英镑。在妈妈的作品中我将这个数字放入文本框b中。我写了一个课程来完成所有

的计算。这些计算是在点击一个按钮时完成的,所以一旦点击这些变量就会失去焦点,从而将值放在

文本框中。但这并不觉得非常专业。另外我必须使用

另一个文本框来保留第一个支持的赔率,所以我可以检查

的赔率是否小。希望你得到这个主义。任何人都可以

建议是否有更好的方法。

问候罗伯特

Hi, after reading up, c# doesnt appear to use gloal variables. Alot of
post advise against them also. So Im wondering how to get around this.
Im trying to do a calculation, retain the result, do another
calculation then add the result to my retained value etc. my values:
Im backing then laying horses. So I back first, say with a return of
£20 if it wins. Then I lay the horse with an offset bet giving me an
overall return of £1. Ok, so I need to retain this value, at the
momment I put it in textbox ''a''. Now say on the same horse I bet
again. Same bet £20 if it wins, except Ive got to add my £1 giving
£21. Ok so far, but I must retain the £20 outcome for this bet as it
is used to calculate the offset bet. Not the £21. At the momment Im
putting this figure into textbox ''b''. Ive written a class to do all
the calcs. These calcs are done when a button is clicked, so once
clicked the variables go out of focus, whence putting the values in a
text box. But this doesnt feel very proffesional. Also I have to use
another text box to retain the first odds backed a, so I can check
that the lay odds are smaller. Hope you get the jist. Can anybody
advise if there is a better way to do this.
Regards Robert

推荐答案

2007年6月2日星期六09:45:07 -0700, Ro ***** ***@yahoo.co.uk

< Ro ******** @ yahoo.co.ukwrote:
On Sat, 02 Jun 2007 09:45:07 -0700, Ro********@yahoo.co.uk
<Ro********@yahoo.co.ukwrote:

在阅读之后,c#似乎没有使用全局变量。很多

也对他们提出建议。所以我想知道如何解决这个问题。
Hi, after reading up, c# doesnt appear to use gloal variables. Alot of
post advise against them also. So Im wondering how to get around this.



通常的方式是将变量放入类中。事实上,这只是

唯一的方式。

The usual "way" is to put variables into classes. In fact, that''s the
only way.


我试图进行计算,保留结果,做另一个

计算然后将结果添加到我的保留值等。
Im trying to do a calculation, retain the result, do another
calculation then add the result to my retained value etc.



嗯,你必须有一个执行计算的类。把中间值放在那个班级里可能会有意义。


我不完全理解你给出的例子,而不是那些下注的人

马匹。但是听起来好像你保存的数据肯定是*不是*通常放在全球

变量中的数据。你似乎有一个每匹马的状态(你打赌的是什么,

马怎么做等等)所以这些值应该是每匹马类。你

甚至可以命名这个班级马。如果你喜欢。 :)


Pete

Well, you must have a class that performs the calculations. It may make
sense to put the intermediate values in that class.

I don''t fully understand the example you give, not being someone who bets
on horses. But it sounds to me as though the data that you''re keeping
definitely is *not* of the sort that one would normally put in a global
variable. You seem to have a per-horse state (what you''ve bet, how the
horse did, etc.) and so those values should be in a per-horse class. You
could even name the class "Horse" if you like. :)

Pete


您好,感谢您的快速回复。我知道你的意思,我确实尝试了但是

我发现在点击事件之间我失去了价值观,我猜是因为他们失去了焦点而无法获得
。这听起来是否正确。

问候罗伯特

Hi, thanks for a quick reply. I know what you meen, I did try that but
I found that between the click events I lost the values, Im guessing
because they go out of focus. Does this sound right.
Regards Robert


2007年6月2日星期六10:02:35 -0700, Ro ******** @ yahoo.co.uk

< ; Ro ******** @ yahoo.co.ukwrote:
On Sat, 02 Jun 2007 10:02:35 -0700, Ro********@yahoo.co.uk
<Ro********@yahoo.co.ukwrote:

您好,感谢您的快速回复。我知道你的意思,我确实尝试了但是

我发现在点击事件之间我失去了价值观,我猜是因为他们失去了焦点而无法获得
。这听起来不错吗?
Hi, thanks for a quick reply. I know what you meen, I did try that but
I found that between the click events I lost the values, Im guessing
because they go out of focus. Does this sound right.



声音对吗?我甚至不知道你在说什么。

点击和文本框是向用户和用户显示数据的方式

提供输入。他们不是你存储数据的地方。失去焦点

不应该导致显示数据(例如在文本框中)改变

或丢失。这只是意味着焦点转移到其他地方。


也许如果你能更清楚地了解*代码*你有麻烦,那就是b $ b麻烦有助于。发布一个简洁但完整的例子来说明你正在尝试的东西,以及对它的工作原理的解释,会有所帮助。 br />

Pete

Sound right? I don''t even have any idea what you''re talking about.
Clicks and textboxes are ways to display data to the user and for the user
to provide input. They aren''t places you store data. And losing focus
shouldn''t result in displayed data (in a textbox, for example) to change
or be lost. It just means the focus goes somewhere else.

Perhaps if you could be more explicit about the *code* you''re having
trouble with, that would help. Posting a concise-but-complete example of
what you''re trying, along with an explanation of what about it doesn''t
work, would help.

Pete


这篇关于全局变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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