使用未分配的局部变量的 [英] Use of unassigned local variable

查看:149
本文介绍了使用未分配的局部变量的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题,写一个转换器,从十六进制斌,DEC斌等。这里是我的code,当我调试它,我已经有了一个错误的使用未分配的局部变量Dec_Int10的 ,你能帮助我吗?如何解决这个问题?

 保护无效Button_Click(对象发件人,事件参数五)
{
      如果(Page.IsValid)
      {
           INT Dec_Int10;
           如果(!(string.IsNullOrEmpty(TextBox1.Text)))
           {
                 Dec_Int10 = Convert.ToInt32(TextBox1.Text,10)));
           }
           如果(!(string.IsNullOrEmpty(TextBox2.Text)))
           {
                 Dec_Int10 = Convert.ToInt32(TextBox2.Text,16)));
           }
           如果(!(string.IsNullOrEmpty(TextBox3.Text)))
           {
                 Dec_Int10 = Convert.ToInt32(TextBox3.Text,8)));
           }
           如果(!(string.IsNullOrEmpty(TextBox4.Text)))
           {
                 Dec_Int10 = Convert.ToInt32(TextBox4.Text,2)));
           }
           串Dec_Str10 = Convert.ToString(Dec_Int10,10);
           字符串Hex_Str16 = Convert.ToString(Dec_Int10,16);
           字符串Oct_Str8 = Convert.ToString(Dec_Int10,8);
           串Bin_Str2 = Convert.ToString(Dec_Int10,2);
           TextBox1.Text = Dec_Str10;
           TextBox2.Text = Hex_Str16;
           TextBox3.Text = Oct_Str8;
           TextBox4.Text = Bin_Str2;
       }
}
 

解决方案

您得到这个错误,因为没有分配给变量和自止分配给它没有默认值都在里面,如果块,编译器认为是有机会变量将永远不会被分配

如果你只是把它初始化为0,你将不再有错误。

这是由编译器提供的检查,以阻止你做容易犯错误的一个。

I've got problem with writing a converter from hex to bin, dec to bin etc. Here's my code, when I debug it I've got an error "Use of unassigned local variable Dec_Int10", could you help me? How can I fix this error?

protected void Button_Click (object sender, Event Args e)
{
      if (Page.IsValid)
      {
           int Dec_Int10;
           if(!(string.IsNullOrEmpty(TextBox1.Text)))
           {
                 Dec_Int10 = Convert.ToInt32(TextBox1.Text, 10)));
           }
           if(!(string.IsNullOrEmpty(TextBox2.Text)))
           {
                 Dec_Int10 = Convert.ToInt32(TextBox2.Text, 16)));
           }
           if(!(string.IsNullOrEmpty(TextBox3.Text)))
           {
                 Dec_Int10 = Convert.ToInt32(TextBox3.Text, 8)));
           }
           if(!(string.IsNullOrEmpty(TextBox4.Text)))
           {
                 Dec_Int10 = Convert.ToInt32(TextBox4.Text, 2)));
           }
           string Dec_Str10 = Convert.ToString(Dec_Int10, 10);
           string Hex_Str16 = Convert.ToString(Dec_Int10, 16);
           string Oct_Str8 = Convert.ToString(Dec_Int10, 8);
           string Bin_Str2 = Convert.ToString(Dec_Int10, 2);
           TextBox1.Text = Dec_Str10;
           TextBox2.Text = Hex_Str16;
           TextBox3.Text = Oct_Str8;
           TextBox4.Text = Bin_Str2;
       }
}

解决方案

You get that error because there is no default value assigned to the variable and since the only assignments to it are inside if blocks, the compiler thinks there is a chance the variable will never be assigned.

If you just initialize it to 0 you'll no longer have the error.

This is one of the checks provided by the compiler to keep you from making easy-to-make mistakes.

这篇关于使用未分配的局部变量的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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