在声明之前不能使用局部变量'Location'。 [英] Cannot use local variable 'Location' before it is declared.
本文介绍了在声明之前不能使用局部变量'Location'。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好
我在以下代码中收到错误
在声明之前不能使用局部变量'Location'。本地变量声明隐藏字段a.form1.location。
in the br />
<预郎= CS> LOCATIONNAME =(文本框)(Location.FindControl(<跨度类= 代码串> 跨度> <跨度类= 代码串>地点跨度>));
LocationID =(TextBox)(Location.FindControl( LocationID));
这些行
这是我的代码
public partial class FrmMasterAsset:System.Web.UI.Page
{
// LQMasterAssetDataContext DB;
clsGeneralLib mobjGenlib;
public static string mSearchBy =;
TextBox ManufactureName;
TextBox ManufactureID;
TextBox UOMName; TextBox UOMID;
TextBox类别名称; TextBox CategoryID;
TextBox LocationName; TextBox LocationID;
TextBox DepartmentName; TextBox DepartmentID;
protected void Page_Load(object sender,EventArgs e)
{
// DB = new LQMasterAssetDataContext();
mobjGenlib = new clsGeneralLib();
//对于用户控制定义
ManufactureName =(TextBox)(Manufacturer.FindControl(Account));
ManufactureID =(TextBox)(Manufacturer.FindControl(AccountID));
// UOMName =(TextBox)(UOM.FindControl(UOMType));
// UOMID =(TextBox)(UOM.FindControl(UOMTypeID));
CategoryID =(TextBox)(AssetCategory.FindControl(AssetCategoryID));
CategoryName =(TextBox)(AssetCategory.FindControl(AssetCategory));
LocationName =(TextBox)(Location.FindControl(Location));
LocationID =(TextBox)(Location.FindControl(LocationID));
..........
.........
依此类推
可以提前帮助我解决这个问题。
解决方案
嗯,是的。你会的。
该代码没有声明任何名为Location的变量 - 所以你不能使用它,因为系统不知道它是什么类型,也看不到它无论如何都要获得任何价值!
可能,你正试图在当前页面找到一个控件,应该使用:
LocationName =(TextBox)(FindControl( Location));
LocationID =(TextBox)(FindControl( LocationID));哪会检查当前页面。
您好我找到了我的上述问题的解决方案;
在.aspx页面的项目中,我将注册名称 Location 更改为 AssetLocation ...它运行正常。
注意 - 在具有与其字段类似的局部变量的类的方法中,局部变量在方法中访问时隐藏字段。使用局部变量比使用字段更有效。
Hi
I'm Getting the error in the following code as
"
Cannot use local variable 'Location' before it is declared.The Declaration of Local Variable hides the field a.form1.location.
"
in the
LocationName = (TextBox)(Location.FindControl("Location"));
LocationID = (TextBox)(Location.FindControl("LocationID"));
these lines
Here is my code
public partial class FrmMasterAsset : System.Web.UI.Page { //LQMasterAssetDataContext DB; clsGeneralLib mobjGenlib; public static string mSearchBy = ""; TextBox ManufactureName; TextBox ManufactureID; TextBox UOMName; TextBox UOMID; TextBox CategoryName; TextBox CategoryID; TextBox LocationName; TextBox LocationID; TextBox DepartmentName; TextBox DepartmentID; protected void Page_Load(object sender, EventArgs e) { //DB = new LQMasterAssetDataContext(); mobjGenlib = new clsGeneralLib(); //For User Control Definition ManufactureName = (TextBox)(Manufacturer.FindControl("Account")); ManufactureID = (TextBox)(Manufacturer.FindControl("AccountID")); //UOMName = (TextBox)(UOM.FindControl("UOMType")); //UOMID = (TextBox)(UOM.FindControl("UOMTypeID")); CategoryID = (TextBox)(AssetCategory.FindControl("AssetCategoryID")); CategoryName = (TextBox)(AssetCategory.FindControl("AssetCategory")); LocationName = (TextBox)(Location.FindControl("Location")); LocationID = (TextBox)(Location.FindControl("LocationID")); .......... ......... and so on
can any one help me to resolve this thanks in advance.
解决方案
Well, yes. You will.
That code doesn't declare any variable called "Location" - so you can't use it, because the system doesn't know what type it is, and can't see it getting any value anyway!
Possibly, you are trying to find a control on the current page, and should be using:
LocationName = (TextBox)(FindControl("Location")); LocationID = (TextBox)(FindControl("LocationID"));Which would check the current page.
Hi i Have found the solution for my Above issue; In my project in the .aspx page i changed the name Location in the Register as AssetLocation ...it works fine. Note- " Within a method of a class that has a local variable named similar to its field, the local variable hides the field while accessing it within the method. It is more efficient to use a local variable than a field."
这篇关于在声明之前不能使用局部变量'Location'。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文