我遇到了障碍。我只想了解如何为 SelectedIndex 项目分配声明的成本?我觉得我错过了一些明显的东西。
我已经声明了 taco 类型的成本,但我不确定如何将它们分配给ddlTacoType
Visual Basic 中的 SelectedIndex 项。使用 switch 语句是个好主意吗?理想情况下,当用户选择“Chipotle Chicken”时,输出应显示成本为 8.99。
任何帮助将非常感激。
干杯,
理查德
' Project: Street Tacos Order Form
' Author: Richard Lew
' Date: November 3, 2019
' Purpose: The web application allows a customer to fill out a street taco order form.
Public Class About
Inherits Page
Private _decChicken As Decimal = 8.99
Private _decPork As Decimal = 9.99
Private _strFish As Decimal = 12.99
Private _strBeef As Decimal = 13.99
Private _strExtra As Decimal = 0.99
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
End Sub
Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
' The btnSubmit click event will calculate the cost of the street tacos
' based on the type of tacos selected.
' Declare and initialize variables
Dim decTacoCost As Decimal
Dim decFinalCost As Decimal
Dim decTotalCost As Decimal
Dim decChickenCost As Decimal = 8.99D
Dim decPorkCost As Decimal = 9.99D
Dim decFishCost As Decimal = 12.99D
Dim decBeefCost As Decimal = 13.99D
Dim decExtraCost As Decimal = 0.99
Dim strName As String
Dim strAddress As String
Dim strPhone As String
Dim decOrderCost As Decimal = 0D
Dim strMessage As String
' Trim additional spaces that are entered by the user
strName = txtName.Text.Trim
strAddress = txtAddress.Text.Trim
strPhone = txtPhone.Text.Trim
' Clear the Order Message
lblOrder.Text = ""
' Ensure a Taco Selection is Selected
If ddlTacoType.SelectedIndex < 0 Then
lblTacoTypeError.Visible = True
Else
lblTacoTypeError.Visible = False
End If
' Ensure a Topping is Selected
If Not (chkRedChili.Checked Or chkGreenChili.Checked Or chkBeans.Checked Or chkCream.Checked Or chkLime.Checked Or chkNoToppings.Checked) Then
lblToppingsError.Visible = True
Else
lblToppingsError.Visible = False
End If
' Calculate the cost of the Taco(s) selected by the user
Select Case decTotalCost
Case 0
decTacoCost = decChickenCost
Case 1
decTacoCost = decPorkCost
Case 2
decTacoCost = decFishCost
Case 3
decTacoCost = decBeefCost
Case 4
decTacoCost = decExtraCost
End Select
End Sub
End Class
当用户在ddlTacoType
下拉列表中选择 taco 类型时,程序应将项目与其成本相关联,并在程序结束时计算所选项目的总成本。