2 jwjanqiu jwjanqiu 于 2014.12.13 10:29 提问

为什么会有use of unassigned local variable 'result'
    internal void check(string name,ref string result)
    {
        object pass;
        SqlConnection con = new SqlConnection("Data Source=SAMSUNG;Initial Catalog=AL_Nayesh_Kids_Castle;Persist Security Info=True;User ID=sa;Password=259695");
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from register", con);
        SqlDataReader dr = cmd.ExecuteReader();
        while (dr.Read())
        {
            if (name == dr[0].ToString())
            {
                pass = dr[1];
                result = Convert.ToString(pass);
                break;
            }
        }
        dr.Close();
        con.Close();
    }
            static void Main(string[] args)
    { string result, password;
        register obj = new register();
        obj.student();
        Console.WriteLine("name:");
        string name = Console.ReadLine();
        Console.WriteLine("password");
        password = Console.ReadLine();
        obj.check(name,ref result);
        if(password==result)
            Console.WriteLine("welcome");
    }

2个回答

devmiao
devmiao   Ds   Rxr 2014.12.13 11:07

string result ="" , password = "";

devmiao
devmiao   Ds   Rxr 2014.12.13 11:08

养成好习惯,定义变量给初值。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!