变量错误,需要帮助 [英] variable error, need help please

查看:73
本文介绍了变量错误,需要帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么我收到三个变量的错误:registration_fee,lodge_fee和days。我是c#的新手,并且认为我在switch语句中分配了值。这是所有三个变量的错误。 使用未分配的本地变量registration_fee等



任何帮助将不胜感激。



谢谢!



I don't understand why I am getting an error for the three variables: registration_fee , lodge_fee, and days. I am new to c# and thought I assigned the value in my switch statements. This is the error for all three variables. "Use of unassigned local variable registration_fee" etc.

any help would be greatly appreciated.

thanks!

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

namespace PC_12_Pg_265
{
    public partial class workshop_selector : Form
    {
        public workshop_selector()
        {
            InitializeComponent();
        }

        private void exit_button_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void total_button_Click(object sender, EventArgs e)
       {
            string workshop, location;
            int registration_fee , lodge_fee, days, total;

            if (workshop_listBox.SelectedIndex != -1)
            {
                workshop = workshop_listBox.SelectedIndex.ToString();

                switch (workshop)
                {
                    case "Handling Stress":
                        {
                            registration_fee = 1000;
                            days = 3;
                            break;
                        }
                    case "Time Management":
                        {
                            registration_fee = 800;
                            days = 3;
                            break;
                        }
                    case "Supervision Skills":
                        {
                            registration_fee = 1500;
                            days = 3;
                            break;
                        }
                    case "Negotiation":
                        {
                            registration_fee = 1300;
                            days = 5;
                            break;
                        }
                    case "How to Interview":
                        {
                            registration_fee = 500;
                            days = 1;
                            break;
                        }
                }

            }

            else
            {
                MessageBox.Show("Please select a workshop");
            }

            if (location_listBox.SelectedIndex != -1)
            {
                location = location_listBox.SelectedIndex.ToString();

                switch (location)
                {
                    case "Austin":
                        lodge_fee = 150;
                        break;
                    case "Chicago":
                        lodge_fee = 225;
                        break;
                    case "Dallas":
                        lodge_fee = 175;
                        break;
                    case "Orlando":
                        lodge_fee = 300;
                        break;
                    case "Phoenix":
                        lodge_fee = 175;
                        break;
                    case "Raleigh":
                        lodge_fee = 150;
                        break;
                }

            }

            else
            {
                MessageBox.Show("Please select a location");
            }


            total = registration_fee + (lodge_fee * days);

            MessageBox.Show("Your total is $" + total);
    
        }
     }
 }

推荐答案

+总计);

}
}
}
" + total); } } }


不,你没有在交换机中分配变量您的案例只包括您列出的字符串。想象一下 workshop location 的值是无处。在这种情况下,不会分配任何内容。这些情况通过以下方式解决:1)在声明时立即为所有变量分配一些值,例如 int registration_fee = 0; 2)在默认的情况下分配一些值。请记住,您需要在使用它们时处理这些默认值。



请参阅: http:// msdn。 microsoft.com/en-us/library/06tc147t%28v=vs.110%29.aspx [ ^ ]。



但是,让我告诉你使用你的开关将值与字符串进行比较是如此糟糕......实际上,这样的代码甚至是不可接受的。首先,使用那些立即常量(Austin,Chicago......)的想法已经杀死了代码的任何维护。我想,你在代码中的其他地方有一些值。想象一下如果拼错任何字符串或需要修改任何字符串会发生什么;编译器不会检测到问题。有时,您需要使用枚举类型,在您的情况下,您更有可能必须在某些数据文件中包含所有这些选项,这些文件也可以是资源。在最糟糕的情况下,只需将这些常量声明为显式常量



至于这样的整体想法long switch 语句:请参阅我的文章:动态方法调度程序 [ ^ ]。



-SA
No, you did not assign your variables in your switch statements. Your cases only include those strings you listed. Imagine the value of workshop or location is "nowhere". In this case, nothing will be assigned. Such cases are resolved in the following ways: 1) assign some value to all variables immediately at they declaration, such as int registration_fee = 0; 2) assign some vales in default case. Remember that you will need to deal with those default values at the point where you use them.

Please see: http://msdn.microsoft.com/en-us/library/06tc147t%28v=vs.110%29.aspx[^].

However, let me tell you that using your switch to compare the value with strings is so bad… actually, such code is even unacceptable. First of all, the idea to use those immediate constants ("Austin", "Chicago"…) already kills any maintenance of your code. I guess, you have the some values somewhere else in your code. Imagine what happens if you misspell any of those strings or need to modify any of the strings; the compiler won't detect the problem. Sometimes, you need to use enumeration types, in your case, it's more likely that you have to have all those options in some data files, which can also be resources. In the very worst case, just declare those constants as explicit constant.

As to the whole idea of such long switch statements: please see my article: Dynamic Method Dispatcher[^].

—SA


这篇关于变量错误,需要帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆