繁体   English   中英

方法必须具有返回类型,调试时不显示任何内容

[英]Method must have a return type, Not showing anything when Debugging

我构建此代码,尝试显示具有3个选项的列表框,然后combox具有可供您选择的选项的附加内容,如果您想要糖或奶油,则还具有一个复选框,然后尝试在菜单中显示总成本文本框,然后在按钮上显示或订购消息框。

当我尝试调试表单时,我的问题是我的form1_load(必须具有返回类型)。 这是我的全部代码。 任何帮助将不胜感激。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
sing System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{

    public Form1()
    {
        InitializeComponent();
    }
    public class Order
    {
        public string[] menuSelection = new
               string[] { "Coffee", "Tea", "Pastry" };

        public decimal[] menuSelectionPrice = new
                decimal[] { 2.75m, 1.25m, 2.00m };

        private string selection;
        private bool cream;
        private string specialRequest;
        private decimal selectionPrice;
        private decimal specialPrice;

        public Order()
        {
            selection = " ";
            cream = false;
            specialRequest = "";
            selectionPrice = 0;
            drinkPrice = 0;
        }
        // property for selection
        public string Selection
        {
            get
            {
                return selection;
            }
            set
            {
                selection = value;
                SetSelectionPrice();
            }
        }
        // Property for special request 
        public string SpecialRequest
        {
            get
            {
                return specialRequest;
            }
            set
            {
                specialRequest = value;
                SetSpecialPrice();
            }
        }
        public bool Cream
        {
            set
            {
                cream = value;
            }
        }
        // read only property for  selection price
        public decimal SelectionPrice
        {
            get
            {
                return selectionPrice;
            }
        }
        // after the selection is set, store the selection price
        public void SetSelectionPrice()
        {
            for (int i = 0 < menuSelection.Length; i++)
            {
                if (menuSelection[i] == selection)
                {
                    selectionPrice = menuSelectionPrice[i];
                }
            }
        }
        // Return the Cream Selection.
        public string GetCreamSelection()
        {
            string creamOrNot;
            if (cream)
            {
                creamOrNot = "Cream";
            }
            else
            {
                creamOrNot = "No Cream";
            }
               return creamOrNot;
        }
        public decimal SpecialPrice
        {
            get
            {
                return specialPrice;
            }
        }
        public void SetSpecialPrice()
        {
            switch (specialRequest)
            {
                case "Cinnamon":
                    specialPrice = 0.50m;
                    break;
                case "Cocoa":
                    specialPrice = 1.00m;
                    break;
                case "Whipped Cream":
                    specialPrice = 0.74m;
                    break;
                case "Lemon" :
                case "Cherry" :
                case "Chocolate" :
                case "Pineapple" :
                case "Peach" :
                case "Strawberry" :
                case "Cheese" :
                case "Watermellon" :
                case "Apple" :
                case "Orange" :
                    specialPrice = 2.00m;
                    break;

            }
        }
        public decimal DetermineTotalCost() 
        {
            return selectionPrice + specialPrice;
        }
        public override string ToString()
        {
            return "Total Due: " + DetermineTotalCost().ToString("C");
        }
    }
     public void Form1_Load(object sender,System.EventArgs e)
     {
         newOrder = new Order();
         for (int i = 0; i < newOrder.menuSelection.Length; i++)
         {
             this.lstBxChoice.Items.Add(newOrder.menuSelection[i]);
         }
     }

    private void lstBxChoice_SelectedIndexChanged(object sender, EventArgs e)
    {
        newOrder.Selection = this.lstBxChoice.Text;
    }

    private void cmbSpecial_SelectedIndexChanged(object sender, EventArgs e)
    {
        newOrder.SpecialRequest =this.cmboSpecial.Text;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        MessageBox.Show(newOrder.Selection + "\n" +  newOrder.SpecialRequest + "\n" + newOrder.GetCreamSelection());
    }

    private void checkBox1_CheckedChanged(object sender, EventArgs e)
    {
        if (this.checkBox1.Checked)
            newOrder.cream = true;
        else
            newOrder.cream = false;
    }





}

}

您的GetCreamSelection应该返回字符串

最初,您应该全局创建Order实例,

 newOrder = new Order();

其次,

  public string GetCreamSelection()
    {
        string creamOrNot;
        if (cream)
        {
            creamOrNot = "Cream";
        }
        else
        {
            creamOrNot = "No Cream";
        }
        return creamOrNot;
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM