Vb6 类型与随机数生成器不匹配 [英] Vb6 type mismatch with random number generator

查看:23
本文介绍了Vb6 类型与随机数生成器不匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我正在创建的游戏生成一个 1 到 20 之间的随机数,但由于某种原因,此代码已停止工作并出现类型不匹配错误,我尝试了游戏中的其他生成器,但我得到了同样的错误.

I wanted to generate a random number between 1 and 20 for a game that I am creating and for some reason this code has stopped working and gives me a type mismatch error, I have tried other generators in the game but I get the same error.

Public Counter1 As Integer ' Counters for each reel
Public ArrayImgMid1 As Integer ' Store number to iterate over image arrays
Public ReelPic1 As Variant ' Array of images for each reel
Public Reel1Spin As Long ' Spins for each reel
Public ImgBeth, ImgWallow, ImgDan, ImgChris, ImgCatBug As StdPicture ' Images for reels

Private Sub CmdSpin_Click()

'Enable all timers to imitate spinning
TimerReel1.Enabled = True
TimerReel1.Interval = 100

' Disable spin button
CmdSpin.Enabled = False

' Set all counters to 0
Counter1 = 0

' Generate random number for the first reel to spin
Reel1Num = (CLng(Rnd * 20) + 1) ' This is highlighted when I press debug

推荐答案

这样写:

Dim Reel1Num As Long

Reel1Num = (CLng(Rnd * 20) + 1)

不要在 VB6 中使用 Integer - 它代表一个 16 位有符号整数,它的性能略低于 Long ,后者实际上是一个 32 位有符号整数.这样您也不会遇到 16 位数据类型的限制.

Don't use Integer in VB6 - it represents a 16-bit signed integer and it has slightly less performance than Long which is really a 32-bit signed integer. This way you also won't run into limits with the 16-bit data type.

您的代码不起作用的原因是 Int() 函数不会将值的数据类型转换为 Integer 类型 - 它会舍入指定的value 为整数值但保留其数据类型.

The reason your code doesn't work is because the Int() function doesn't convert the data type of a value to type Integer - it rounds the specified value to an integer value but keeps its data type.

要将值转换为特定数据类型,请使用 CInt()CLng() 等函数.但正如我所说,除非您特别需要,否则避免使用 Integer - Long 在大多数情况下更好.

To convert a value to a particular data type, use the CInt(), CLng(), etc. functions. But as I said, avoid using Integer unless you specifically need it - Long is better in most cases.

在您发布代码后,我看不到 Reel1Num 变量的定义 - 它在哪里定义?是 Reel1Spin 吗?如果是这种情况,请确保启用 Tools->Options 中的 Require variable declaration 选项 - 默认情况下它是关闭的.如果你没有戴上它,这是一种很容易射中自己脚的方法.

After you posted your code, I can't see a definition of the Reel1Num variable - where is it defined? Is it Reel1Spin? If that's the case, make sure you enable the Require variable declaration option in Tools->Options - it's off by default. If you don't have it on, it's a very easy way to shoot yourself in the foot.

与您的错误无关,但您的大多数图像对象定义不正确 - 在 VB6 中,必须为每个变量指定变量的类型,而不是每行一次.所以在这一行:

Unrelated to your error, but most of your image objects are defined incorrectly - in VB6, the type of a variable must be specified for every variable, not once per line. So on this line:

Public ImgBeth、ImgWallow、ImgDan、ImgChris、ImgCatBug As StdPicture

您只真正创建了 1 个 StdPicture 对象,所有其他对象都将是变体.正确的做法是:

you only really create 1 StdPicture object, all others will be variants. The right way to do it is:

Public ImgBeth As StdPicture, ImgWallow As StdPicture, ImgDan As StdPicture, _
    ImgChris As StdPicture, ImgCatBug As StdPicture

除此之外,我看不出你的代码有什么问题——我的电脑上没有安装 VB6.请记住,VB6 有一种处理类型不匹配的时髦方式,在某些情况下,突出显示的行可能不是导致实际错误的行.这曾经让我发疯.

Aside from these, I can't see anything wrong with your code - I don't have VB6 installed on my computer. Keep in mind, that VB6 had a funky way of dealing with type mismatch and in some cases the highlighted line may not be the one that causes the actual error. This used to drive me crazy.

这篇关于Vb6 类型与随机数生成器不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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