我编写的代码只是我整个代码的一部分,因为我只想测试是否可以运行part子程序。当我跑步时,会有一条消息说:参考参数不匹配。我不熟悉使用调用来调用子过程。有人请帮帮我!
Dim Age As Integer
Dim Weight, Heights, BMI, BMR As Single
Dim MenBMR, WomenBMR As Single
Private Sub cmdBMI_Click()
If Age > 20 Then
Call AdultBMI(BMI)
End
End Sub
Private Sub AdultBMI(BMI As Single, Weights As Single, Heights As Single)
Age = Val(txtAge.Text)
Weight = Val(txtWeight.Text)
Heights = Val(txtHeight.Text)
BMI = Weight / ((Heights / 100) ^ 2)
If BMI < 18.5 Then
txtBMIValue.Text = BMI
txtBMIStatus.Text = "Underweight"
MsgBox ("You are underweight!")
End
End Sub
答案 0 :(得分:4)
BMI
是一种变体。
在下面的这一行
Dim Weight, Heights, BMI, BMR As Single
Weight
,Heights
和BMI
都是变体。在VB6中,您必须在使用逗号时显式提供每个变量的类型。试试这个。
Dim Weight As Single, Heights As Single, BMI As Single, BMR As Single
答案 1 :(得分:2)
好的,这段代码存在很多问题,我会尽可能多地推出。
您已经为子例程AdultBMI制作了BMI,Weights和Heights参数,但您在子例程中将它们用作局部变量。参数用于从子例程外部引入值。请参阅下文,了解我将如何更改您的代码。
当您调用AdultBMI时,您只给出了一个参数而不是例程中的三个参数,这就是您收到错误的原因。
你的End Sub之前有一个End语句。结束将立即终止您的程序。您需要使用&#39;结束如果
理想情况下,您的AdultBMI子例程将是一个带有两个参数Weight和Height的函数,然后返回BMI:
Private Sub Command1_Click()
Dim BMI As Double
BMI = AdultBMI(Val(txtHeight.Text), Val(txtWeight.Text))
If BMI < 18.5 Then
MsgBox "Underweight"
End If
End Sub
Private Function AdultBMI(ByVal Height As Double, ByVal Weight As Double) As Double
AdultBMI = Weight / ((Height / 100) ^ 2)
End Function
我认为您需要更多地了解变量和范围,因为随着您的编程进展,它将为您提供良好的服务。
答案 2 :(得分:1)
你有一个名为的功能
AdultBMI()
带有三个参数,如函数定义所示。
您只使用一个参数调用此函数。剩下的两个在哪里?
您的定义是
Private Sub AdultBMI(BMI As Single, Weights As Single, Heights As Single)
Age = Val(txtAge.Text)
Weight = Val(txtWeight.Text)
Heights = Val(txtHeight.Text)
BMI = Weight / ((Heights / 100) ^ 2)
If BMI < 18.5 Then
txtBMIValue.Text = BMI
txtBMIStatus.Text = "Underweight"
MsgBox ("You are underweight!")
End
End Sub
这里你期望三个参数为Private Sub AdultBMI(BMI As Single, Weights As Single, Heights As Single)
但是在你的函数调用中,你只传递了一个参数AdultBMI(BMI)
它应该像AdultBMI(BMI,Weight,Height)
。并且无论如何你使用了global variables
并且你在函数调用中传递了global variables
,这是不必要的。它们在整个程序中都可用。您甚至没有初始化变量,但是您正在条件下检查它们。您必须先通过某些textbox
或inputbox
等来初始化它们。
此外,您有一个variant
数据要传递给期望single
数据的函数。
您需要将每个变量分别声明为dim a as single, b as single
而不是dim a,b as single
。这将使a
成为变体
无论如何,如果您希望代码停止显示参数不匹配错误,请尝试使用此代码
Dim Age As Integer
Dim Weight As Single, Heights As Single, BMI As Single, BMR As Single
Dim MenBMR As Single, WomenBMR As Single
Private Sub cmdBMI_Click()
If Age > 20 Then
Call AdultBMI()
End Sub
Private Sub AdultBMI()
Age = Val(txtAge.Text)
Weight = Val(txtWeight.Text)
Heights = Val(txtHeight.Text)
BMI = Weight / ((Heights / 100) ^ 2)
If BMI < 18.5 Then
txtBMIValue.Text = BMI
txtBMIStatus.Text = "Underweight"
MsgBox ("You are underweight!")
End Sub