用'long'变量类型溢出? [英] Overflow with 'long' variable type?

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

问题描述

大家好,我对VB比较陌生,但我知道当你试图让变量太高或太低时,会发生溢出,例如你已经准备好了。 300,当你准备好用作一个字节时。

这里发生了什么 - 我正在尝试制作一个''解码''色号的功能(我的意思是,类型的对象.BackColor可以是RGB()函数返回的单独的R,G和B值,每个值从0到255.

但这就是问题所在。为此,我使用此代码:

展开 | 选择 | Wrap | 行号

解决方案

问题在于VB正在将内部转换为它认为可以逃脱的最小格式。在这方面似乎做出了很糟糕的选择。在这种情况下,很可能是转换为Integer。


在即时窗口中试试这个例子...


? 255! * 255! * 255!


然后尝试这条修改过的行......

展开 | 选择 | Wrap < span class =codeDivider> | 行号


啊,我明白了。谢谢,这使它工作正常!

出于兴趣,你说那种''强制''VB保持它长数据类型。你可以强迫它保持其他数据类型吗?

只是想知道将来知道它是否有用。



啊,我明白了。谢谢,这使它工作正常!

出于兴趣,你说那种''强制''VB保持它长数据类型。你可以强迫它保持其他数据类型吗?

只是想知道将来是否有用。



当然。


实际上并没有强迫VB对计算做任何特别的事情,只是指定了255(在本例中)是Long值。我们可以使用255#来使其成为双精度十进制值。 (这样效率会降低,速度也会变慢,但除非你进行数百万次计算,否则差异太小而无法注意到。)


只是当设置计算,VB考虑所涉及的所有数据类型(或者可能只是最小 - 我忘了)。因此,正如您所见,确保您的数据类型正确可能会影响其工作方式。你可以通过声明类似的方式达到相同的效果:
Public Const TwoFiftyFive As Long = 255

然后在计算中使用该常量。


Hi all, I''m relatively new to VB but I know that an overflow occurs when you try to make a variable be too high or low for what you''ve prepared it for, e.g. 300 when you''ve prepared it for use as a byte.
Here''s what happening - I am trying to make a function which ''decodes'' a color number (I mean, the kind of values object.BackColor can be, and which the RGB() function gives back) into its separate R, G and B values each from 0 to 255.
But here''s the problem. To do that, I use this code:

Expand|Select|Wrap|Line Numbers

解决方案

The problem is that VB is converting things internally to the smallest format that it thinks it can get away with. And it seems to make very poor choices in this regard. Most likely it is converting to Integer in this case.

Try this example in the immediate window...

? 255! * 255! * 255!

Then try this modified line...

Expand|Select|Wrap|Line Numbers


Ah, I see. Thank you, that makes it work fine!
Out of interest though, you said that sort of ''forces'' VB to keep it a Long data type. Can you force it into keeping it as other data types?
Just wondering if it might be useful to know in the future.


Ah, I see. Thank you, that makes it work fine!
Out of interest though, you said that sort of ''forces'' VB to keep it a Long data type. Can you force it into keeping it as other data types?
Just wondering if it might be useful to know in the future.

Sure.

It''s not actually forcing VB to do anything in particular about the calculation, just specifying that the 255 (in this case) is a Long value. We could have put 255# to make it a double-precision decimal value. (This would be less efficient and make things slower, but unless you were doing the calculation millions of times the difference would be too small to notice.)

It''s just that when setting up a calculation, VB takes into account all of the data types involved (or maybe just the "smallest" - I forget). So ensuring your data is the correct type can affect the way it works, as you''ve seen. You could have achieved the same effect by, say, declaring something like:
Public Const TwoFiftyFive As Long = 255
and then using that constant in your calculation.


这篇关于用'long'变量类型溢出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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