1

我正在尝试在 Visual Basic 中编写一个简单的程序,它通过一个类传递一个变量,计算它,然后返回它。我的表格“Pizza”有 2 组,每组有 3 个单选按钮。单击总计按钮时,比萨大小价格将添加到比萨饼皮价格中。我通过故障排除发现它只添加了“createPizza.Crust”+“createPizza.Size”。我错过了什么。感谢您的任何帮助。

Form "Pizza"
Public Class Pizza

Private Sub btnTotal_Click(sender As Object, e As EventArgs) Handles btnTotal.Click
    If rThin.Checked = True Then
        createPizza.Crust = 1
    ElseIf rPan.Checked = True Then
        createPizza.Crust = 2
    ElseIf rStuff.Checked = True Then
        createPizza.Crust = 3
    End If

    If rSm.Checked = True Then
        createPizza.Size = 1
    ElseIf rMm.Checked = True Then
        createPizza.Size = 2
    ElseIf rLg.Checked = True Then
        createPizza.Size = 3
    End If

    lblTotal.Text = "Total: " + createPizza.PizTotal.ToString()

  End Sub
End Class

“创建披萨”类

Public Class createPizza

Public Shared Size As Double
Public Shared Crust As Double

Public Shared Property pizzaCrust As Double
    Get
        Return Crust
    End Get
    Set(ByVal crustpr As Double)
        If Crust = 1 Then
            crustpr = 1
        ElseIf Crust = 2 Then
            crustpr = 1.5
        ElseIf Crust = 3 Then
            crustpr = 3
        End If
        Crust = crustpr
    End Set
End Property

Public Shared Property pizzaSize As Double
    Get
        Return Size
    End Get
    Set(ByVal sizepr As Double)
        If Size = 1 Then
            sizepr = 10.0
        ElseIf Size = 2 Then
            sizepr = 12.5
        ElseIf Size = 3 Then
            sizepr = 15
        End If
        Size = sizepr
    End Set
End Property

Public Shared Function PizTotal() As Decimal
    Return pizzaCrust + pizzaSize
End Function

End Class
4

2 回答 2

0

我已经更新了课程以适应 VBA。

createPizza

Private Size As Double
Private Crust As Double


Public Function PizTotal() As Double
    PizTotal = pizzaCrust + pizzaSize
End Function


Public Property Get pizzaCrust() As Double
    pizzaCrust = Crust
End Property

Public Property Let pizzaCrust(ByVal vNewValue As Double)
    Select Case vNewValue
    Case 1
        crustpr = 1
    Case 2
        crustpr = 1.5
    Case 3
        crustpr = 3
    End Select

    Crust = crustpr
End Property

Public Property Get pizzaSize() As Double
    pizzaSize = Size
End Property

Public Property Let pizzaSize(ByVal vNewValue As Double)
    Select Case vNewValue
    Case 1
        sizepr = 10#
    Case 2
        sizepr = 12.5
    Case 3
        sizepr = 15
    End Select

    Size = sizepr
End Property

模块 示例代码

Sub sample()
    Dim a As New createPizza
    a.pizzaCrust = 2
    a.pizzaSize = 3

    MsgBox a.PizTotal
End Sub
于 2013-05-03T02:55:01.467 回答
0

您的PizTotal函数仅返回Size + Crust,因为您的pizzaSizepizzaCrust属性分别返回 和 的SizeCrust。我可以看到您必须在属性设置中计算成本的代码,但您从不调用设置器pizzaSizepizzaCrust因此永远不会计算成本。

你的pizzaSizepizzaCrust属性没有多大意义。例如,crust 属性设置将价格作为输入,但如果Crust是 1、2 或 3,它会重置价格,然后将价格存储在Crust变量中。如果你写了以下逻辑:

Dim p as New createPizza
p.Crust = 2
p.pizzaCrust = 12345678
Dim price1 As Double = p.pizzaCrust
p.pizzaCrust = 12345678
Dim price2 As Double = p.pizzaCrust

price1是 1.5 和price212345678,因为 PizzaCrust 既使用了 的值Crust又修改了Crust。您将Crust变量用于地壳类型和地壳价格,而不是区分它们。您应该将外壳类型存储在一个变量中,将 cust 成本存储在另一个变量中(或者最好只编写一个函数,根据外壳类型 1、2 或 3 返回 curst 成本)。

于 2013-05-03T03:17:17.060 回答