[英]Error “Use of unassigned local variable”?
The error is in "search, adapter.GetData(search, null, null, TextBox1.Text, null);" 错误出现在“搜索,adapter.GetData(搜索,null,null,TextBox1.Text,null);”中 which is a parameter, but already initialize the variable, in the other parameters does not mark error.
这是一个参数,但是已经初始化了变量,在其他参数中没有标记错误。 why?
为什么?
string search;
adapter.GetData(search, null, null, TextBox1.Text, null);
this.gridview.DataBind();
this.gridview.DataSource = adapter.GetData(search, null, null, TextBox1.Text,null);
Initialize a variable means assign it an initial value 初始化变量意味着为其分配初始值
string search; // not initialized, You define it's type and it's name.
string search = "something"; //initialized
You can even set null
value to search like string search =null;
您甚至可以设置
null
值来进行搜索,例如string search =null;
then compiler will not warn you because you are intentionally doing it. 则编译器不会警告您,因为您是故意这样做的。
if you need to search and bind returned search result to gridview 如果您需要搜索并将返回的搜索结果绑定到gridview
search = "set some value here";
//set data source
this.gridview.DataSource =adapter.GetData(search, null, null, TextBox1.Text, null);
//now bind the gridview
this.gridview.DataBind();
As it is right now, search is not a parameter, it is a local variable. 目前,搜索不是参数,而是局部变量。 That local variable does not have a value assigned to it, therefore the error.
该局部变量没有赋值,因此出错。 If you just want to test it, do something like
如果您只想测试,请执行以下操作
string search = "Any value you want to test";
string search;
Is a declaration while 是一个声明,而
string search = "foo";
is an assignment. 是一项任务。
Null is not a variable, and TextBox.Text1 has a value. Null不是变量,并且TextBox.Text1具有值。 You are getting that error because while the search variable is declared, it does not have a value assigned to it.
之所以会出现此错误,是因为在声明搜索变量时,它没有赋值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.