为什么我需要这个Visual Basic代码的函数?

时间:2015-10-01 16:37:44

标签: vb.net visual-studio function visual-studio-2012

我正在尝试找出您通过订购甜甜圈和咖啡的应用程序的功能。没有输入数量的框。使用组合框中分组的单选按钮选择甜甜圈的类型,就像咖啡的类型一样。用户订购不输入任何信息。该应用程序根据所选的单选按钮计算。还有3%的销售税。

甜甜圈如下: 上釉是0.65美元,糖0.65美元,巧克力是0.85美元,填充是1.00美元。

咖啡费用: 常规是1.00美元,卡布奇诺是2.50美元。

如何为每个人编写一个函数?我想我会写一个计算甜甜圈,咖啡和销售税的功能。如果每个案例只有一个可能的选择,我不确定我应该包括什么。我以为我可以写一堆常量,因为没有任何改变,并且做了一个If语句,但是赋值是要求函数。

这就是我到目前为止的甜甜圈。

Private Function GetDonutPrice(ByVal strDonut As String) As Double
     Dim dblDonutPrice As Double
     'Calculates and returns the price of donuts 
     If strDonut = "G" Or strDonut = "S" Then
        dblDonutPrice = 0.65 
     ElseIf strDonut = "C" Then
        dblDonutPrice = 0.85
     Else
        strDonut = "F"
        dblDonutPrice = 1.0 
     End If

     Return dblDonutPrice 
 End Function

然后对于btnCalc_Click我编码了这个:

Private Sub btnCalc_Click(sender As Object, e As EventArgs) 
     'Displays donut and cappucino subtotal, taxes and price 
     Dim strDonutPrice As String
     Dim dblDonutPrice As Double
     If radGlazed.Checked Then
        strDonutPrice = "G" 
     ElseIf radSugar.Checked Then
        strDonutPrice = "S" 
     ElseIf radChoc.Checked Then
        strDonutPrice = "C"
     ElseIf radFilled.Checked strDonutPrice = "F"
     End If
    ' get the donut price ' 
    dblDonutPrice = GetDonutPrice(strDonutPrice) 
 End Sub 

 And I get an error in dblDonutPrice = GetDonutPrice(strDonutPrice) 

2 个答案:

答案 0 :(得分:2)

听起来你需要一个接收甜甜圈的类型和数量的功能,并进行数学运算并返回成本。咖啡也一样。所以你总共有三个功能。咖啡,甜甜圈,以及主要用于输入订单和数量的咖啡,调用其他功能,添加,然后执行销售税

这样的事可能。

Private Sub cmdAddItUp_Click(sender As System.Object, e As System.EventArgs) Handles cmdAddItUp.Click
    Dim iQuantity1 as integer
    Dim dDoughnutCost as double

    'Get the data to send to doughnuts
    iQuantity1 = val(TextBox1.Text)
    iQuantity2 = val(TextBox2.Text)
    etc.

    'Get the doughnuts cost
    dDoughnutCost = Doughtnuts(iQuantity1, iQuantity2, iQuantity3)

    'Do the same for the coffee
    dCoffeeCost = Coffees(iQuantity1, iQuantity2, iQuantity3)

    'Add it up
    dSubTotal = dDoughnutCost  + dCoffeeCost 

    'Calculate tax
    dTotal = Tax(dSubTotal)

    'Now you have the total, do something with it. Display it maybe!

End Sub

Private Function Doughtnuts(iQuantity1 As Integer, iQuantity2 As Integer, iQuantity3 As Integer) As Double
    'Do the math
End Function

Private Function Coffees(iQuantity1 As Integer, iQuantity2 As Integer, iQuantity3 As Integer) As Double
    'Do the math
End Function

Private Function Tax(dSubtotal As Double) As Double
    'Calculate tax
End Function

希望有助于您入门。祝你上学顺利。

答案 1 :(得分:1)

我对你的UI做了一些假设。我没有将这些项目作为单选按钮放在ComboBox中,而是将所有的甜甜圈和咖啡放入相应的GroupBox。这个答案的概念仍然适用,但你必须注意这不是直接插入和去。

以下是我布置UI的方式:

UI Layout

我已将RadioButton放置在FlowLayoutPanel内的GroupBox内。我还将标签放入TableLayoutPanel以提供总金额。

最重要的部分是我如何为列出的项目命名RadioButton控件。在代码隐藏文件中,我使用RadioButton控件的名称来检查正在修改哪个Item。我可以在运行时填充RadioButton控件以确保所有内容排成一行,但这样做更多,如果您在GetType(MyEnum).GetNames()事件中Me.Load使用RadioButtons并不困难。使用正确的名称,文本以及向单选按钮添加适当的事件处理程序来创建新Public Class DoughnutsAndCoffee Private SelectedItems As New List(Of Items) Public Event ItemsChanged() Public Sub UpdateTotal() Handles Me.ItemsChanged Dim subTotal As Decimal = 0D For Each item As Items In SelectedItems subTotal += Prices.GetPrice(item) Next Dim TaxTotal As Decimal = subTotal * Prices.Tax_Rate Dim total As Decimal = subTotal + TaxTotal Me.SubTotal_Label.Invoke(Sub() Me.SubTotal_Label.Text = subTotal.ToString("C2")) Me.Tax_Label.Invoke(Sub() Me.Tax_Label.Text = TaxTotal.ToString("C2")) Me.Total_Label.Invoke(Sub() Me.Total_Label.Text = total.ToString("C2")) End Sub Private Sub RadioButton_CheckedChanged(sender As Object, e As EventArgs) Handles Cappuccino_RadioButton.CheckedChanged, Chocolate_RadioButton.CheckedChanged, Filled_RadioButton.CheckedChanged, Glazed_RadioButton.CheckedChanged, Regular_RadioButton.CheckedChanged, Sugar_RadioButton.CheckedChanged Dim senderRB As RadioButton = DirectCast(sender, RadioButton) Dim selectedItem As String = (From item As String In GetType(Items).GetEnumNames Where senderRB.Name.Contains(item) Select item).FirstOrDefault If selectedItem <> String.Empty Then Dim Item As Items = DirectCast([Enum].Parse(GetType(Items), selectedItem), Items) If senderRB.Checked Then Me.SelectedItems.Add(Item) RaiseEvent ItemsChanged() Else If Me.SelectedItems.Contains(Item) Then Me.SelectedItems.Remove(Item) RaiseEvent ItemsChanged() End If End If End If End Sub Private Sub DoughnutsAndCoffee_Shown(sender As Object, e As EventArgs) Handles Me.Shown RaiseEvent ItemsChanged() End Sub End Class Public Structure Prices Public Const Tax_Rate As Decimal = 0.03D Public Const Glazed As Decimal = 0.65D Public Const Sugar As Decimal = 0.65D Public Const Chocolate As Decimal = 0.85D Public Const Filled As Decimal = 1D Public Const Regular As Decimal = 1D Public Const Cappuccino As Decimal = 2.5D Public Shared Function GetPrice(item As Items) As Decimal Dim itemStr As String = [Enum].GetName(GetType(Items), item) Return GetType(Prices).GetField(itemStr).GetValue(Nothing) End Function End Structure Public Enum Items Glazed Sugar Chocolate Filled Regular Cappuccino End Enum 的表单。

如果您想了解其工作原理,请在项目中添加一个新表单,并将其命名为DoughnutsAndCoffee。这是设计器文件和背后的代码:

代码背后:

<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class DoughnutsAndCoffee
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.Glazed_RadioButton = New System.Windows.Forms.RadioButton()
        Me.Sugar_RadioButton = New System.Windows.Forms.RadioButton()
        Me.Chocolate_RadioButton = New System.Windows.Forms.RadioButton()
        Me.Filled_RadioButton = New System.Windows.Forms.RadioButton()
        Me.Regular_RadioButton = New System.Windows.Forms.RadioButton()
        Me.Cappuccino_RadioButton = New System.Windows.Forms.RadioButton()
        Me.TableLayoutPanel1 = New System.Windows.Forms.TableLayoutPanel()
        Me.Total_Label = New System.Windows.Forms.Label()
        Me.Label5 = New System.Windows.Forms.Label()
        Me.Tax_Label = New System.Windows.Forms.Label()
        Me.Label3 = New System.Windows.Forms.Label()
        Me.SubTotal_Label = New System.Windows.Forms.Label()
        Me.Label1 = New System.Windows.Forms.Label()
        Me.FlowLayoutPanel2 = New System.Windows.Forms.FlowLayoutPanel()
        Me.FlowLayoutPanel1 = New System.Windows.Forms.FlowLayoutPanel()
        Me.Coffee_GroupBox = New System.Windows.Forms.GroupBox()
        Me.Doughnuts_GroupBox = New System.Windows.Forms.GroupBox()
        Me.TableLayoutPanel1.SuspendLayout()
        Me.FlowLayoutPanel2.SuspendLayout()
        Me.FlowLayoutPanel1.SuspendLayout()
        Me.Coffee_GroupBox.SuspendLayout()
        Me.Doughnuts_GroupBox.SuspendLayout()
        Me.SuspendLayout()
        '
        'Glazed_RadioButton
        '
        Me.Glazed_RadioButton.AutoSize = True
        Me.Glazed_RadioButton.Location = New System.Drawing.Point(3, 3)
        Me.Glazed_RadioButton.Name = "Glazed_RadioButton"
        Me.Glazed_RadioButton.Size = New System.Drawing.Size(58, 17)
        Me.Glazed_RadioButton.TabIndex = 9
        Me.Glazed_RadioButton.Text = "Glazed"
        Me.Glazed_RadioButton.UseVisualStyleBackColor = True
        '
        'Sugar_RadioButton
        '
        Me.Sugar_RadioButton.AutoSize = True
        Me.Sugar_RadioButton.Location = New System.Drawing.Point(3, 26)
        Me.Sugar_RadioButton.Name = "Sugar_RadioButton"
        Me.Sugar_RadioButton.Size = New System.Drawing.Size(53, 17)
        Me.Sugar_RadioButton.TabIndex = 10
        Me.Sugar_RadioButton.Text = "Sugar"
        Me.Sugar_RadioButton.UseVisualStyleBackColor = True
        '
        'Chocolate_RadioButton
        '
        Me.Chocolate_RadioButton.AutoSize = True
        Me.Chocolate_RadioButton.Location = New System.Drawing.Point(3, 49)
        Me.Chocolate_RadioButton.Name = "Chocolate_RadioButton"
        Me.Chocolate_RadioButton.Size = New System.Drawing.Size(73, 17)
        Me.Chocolate_RadioButton.TabIndex = 11
        Me.Chocolate_RadioButton.Text = "Chocolate"
        Me.Chocolate_RadioButton.UseVisualStyleBackColor = True
        '
        'Filled_RadioButton
        '
        Me.Filled_RadioButton.AutoSize = True
        Me.Filled_RadioButton.Location = New System.Drawing.Point(3, 72)
        Me.Filled_RadioButton.Name = "Filled_RadioButton"
        Me.Filled_RadioButton.Size = New System.Drawing.Size(49, 17)
        Me.Filled_RadioButton.TabIndex = 12
        Me.Filled_RadioButton.Text = "Filled"
        Me.Filled_RadioButton.UseVisualStyleBackColor = True
        '
        'Regular_RadioButton
        '
        Me.Regular_RadioButton.AutoSize = True
        Me.Regular_RadioButton.Location = New System.Drawing.Point(3, 3)
        Me.Regular_RadioButton.Name = "Regular_RadioButton"
        Me.Regular_RadioButton.Size = New System.Drawing.Size(62, 17)
        Me.Regular_RadioButton.TabIndex = 13
        Me.Regular_RadioButton.Text = "Regular"
        Me.Regular_RadioButton.UseVisualStyleBackColor = True
        '
        'Cappuccino_RadioButton
        '
        Me.Cappuccino_RadioButton.AutoSize = True
        Me.Cappuccino_RadioButton.Location = New System.Drawing.Point(3, 26)
        Me.Cappuccino_RadioButton.Name = "Cappuccino_RadioButton"
        Me.Cappuccino_RadioButton.Size = New System.Drawing.Size(82, 17)
        Me.Cappuccino_RadioButton.TabIndex = 14
        Me.Cappuccino_RadioButton.Text = "Cappuccino"
        Me.Cappuccino_RadioButton.UseVisualStyleBackColor = True
        '
        'TableLayoutPanel1
        '
        Me.TableLayoutPanel1.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
        Me.TableLayoutPanel1.AutoSize = True
        Me.TableLayoutPanel1.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink
        Me.TableLayoutPanel1.ColumnCount = 2
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
        Me.TableLayoutPanel1.ColumnStyles.Add(New System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50.0!))
        Me.TableLayoutPanel1.Controls.Add(Me.Total_Label, 1, 2)
        Me.TableLayoutPanel1.Controls.Add(Me.Label5, 0, 2)
        Me.TableLayoutPanel1.Controls.Add(Me.Tax_Label, 1, 1)
        Me.TableLayoutPanel1.Controls.Add(Me.Label3, 0, 1)
        Me.TableLayoutPanel1.Controls.Add(Me.SubTotal_Label, 1, 0)
        Me.TableLayoutPanel1.Controls.Add(Me.Label1, 0, 0)
        Me.TableLayoutPanel1.Location = New System.Drawing.Point(67, 133)
        Me.TableLayoutPanel1.Name = "TableLayoutPanel1"
        Me.TableLayoutPanel1.RowCount = 3
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333!))
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333!))
        Me.TableLayoutPanel1.RowStyles.Add(New System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 33.33333!))
        Me.TableLayoutPanel1.Size = New System.Drawing.Size(140, 57)
        Me.TableLayoutPanel1.TabIndex = 9
        '
        'Total_Label
        '
        Me.Total_Label.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.Total_Label.AutoSize = True
        Me.Total_Label.Location = New System.Drawing.Point(80, 41)
        Me.Total_Label.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
        Me.Total_Label.Name = "Total_Label"
        Me.Total_Label.Size = New System.Drawing.Size(39, 13)
        Me.Total_Label.TabIndex = 5
        Me.Total_Label.Text = "Label6"
        '
        'Label5
        '
        Me.Label5.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.Label5.AutoSize = True
        Me.Label5.Location = New System.Drawing.Point(10, 41)
        Me.Label5.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
        Me.Label5.Name = "Label5"
        Me.Label5.Size = New System.Drawing.Size(31, 13)
        Me.Label5.TabIndex = 4
        Me.Label5.Text = "Total"
        '
        'Tax_Label
        '
        Me.Tax_Label.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.Tax_Label.AutoSize = True
        Me.Tax_Label.Location = New System.Drawing.Point(80, 22)
        Me.Tax_Label.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
        Me.Tax_Label.Name = "Tax_Label"
        Me.Tax_Label.Size = New System.Drawing.Size(39, 13)
        Me.Tax_Label.TabIndex = 3
        Me.Tax_Label.Text = "Label4"
        '
        'Label3
        '
        Me.Label3.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.Label3.AutoSize = True
        Me.Label3.Location = New System.Drawing.Point(10, 22)
        Me.Label3.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
        Me.Label3.Name = "Label3"
        Me.Label3.Size = New System.Drawing.Size(25, 13)
        Me.Label3.TabIndex = 2
        Me.Label3.Text = "Tax"
        '
        'SubTotal_Label
        '
        Me.SubTotal_Label.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.SubTotal_Label.AutoSize = True
        Me.SubTotal_Label.Location = New System.Drawing.Point(80, 3)
        Me.SubTotal_Label.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
        Me.SubTotal_Label.Name = "SubTotal_Label"
        Me.SubTotal_Label.Size = New System.Drawing.Size(39, 13)
        Me.SubTotal_Label.TabIndex = 1
        Me.SubTotal_Label.Text = "Label2"
        '
        'Label1
        '
        Me.Label1.Anchor = System.Windows.Forms.AnchorStyles.Left
        Me.Label1.AutoSize = True
        Me.Label1.Location = New System.Drawing.Point(10, 3)
        Me.Label1.Margin = New System.Windows.Forms.Padding(10, 3, 10, 3)
        Me.Label1.Name = "Label1"
        Me.Label1.Size = New System.Drawing.Size(50, 13)
        Me.Label1.TabIndex = 0
        Me.Label1.Text = "SubTotal"%0

设计师档案:

shouldShowRequestPermissionRationale()