名为“X”的局部变量已在此范围内定义 [英] A local variable named 'X' is already defined in this scope

查看:80
本文介绍了名为“X”的局部变量已在此范围内定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用系统; 
使用 System.Configuration;
使用 System.Data;
使用 System.Linq;
使用 System.Collections;
使用 System .Collections.Generic;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Xml.Linq;
使用 System.IO;
使用 System.Data.OleDb;
使用 System.ComponentModel;

public partial class _Default:System.Web.UI.Page
{
// class Point {双X,Y; }
DataTable dt = new DataTable();

受保护 void Page_Load(对象发​​件人,EventArgs e)
{

}
protected void btnSubmit_Click( object sender,EventArgs e)
{
string connectionString = ;
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileLocation = Server.MapPath( 〜/ App_Data / + fileName);
FileUpload1.SaveAs(fileLocation);
if (fileExtension == 。xls
{
connectionString = Provider = Microsoft.Jet.OLEDB。 4.0;数据源= + fileLocation + ;扩展属性= \Excel 8.0; HDR =是; IMEX = 2\ ;
}
else if (fileExtension == 。xlsx
{
connectionString = Provider = Microsoft.ACE.OLEDB.12.0; Data Source = + fileLocation + ;扩展属性= \Excel 12.0; HDR =是; IMEX = 2 \;
}
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
ArrayList List = new ArrayList();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null );
string getExcelSheetName = dtExcelSheetName.Rows [ 0 ] [ Table_Name]。ToString();
cmd.CommandText = SELECT * FROM [ + getExcelSheetName + ];
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
if (会话[ dtInSession]!= null
{
dt =(DataTable)Session [ dtInSession];
}
for int i = 0 ; i < dtExcelRecords.Rows.Count; i ++)
{
int [] X;
int [] Y;
// 如果你想得到字符串
DataRow thisRow =(DataRow )dtExcelRecords.Rows [I];
int [] X = Convert.ToInt32(thisRow [ X]);
int [] Y =转换.ToInt32(thisRow [ Y]);
// double Length = Convert.ToDouble(Y);
// double X1 = Convert.ToDouble(X);
// double Y1 = Convert.ToDouble(Y);

DataRow dr = dt.NewRow();

}
// polygonArea();
}
}
// 计算
private Double polygonArea( int [] X, int [] Y)
{
Double area = 0 0 ;
int j = X.Length - 1 ;
for int i = 0 ; i < X.Length; i ++)
{
area = area +(X [j] + X [i] * (Y [j] - Y [i]));
j = i;
}
area = area / 2 ;
if (area < 0
area = area * -1;
返回区域;
}

}

解决方案

  int  [] X; 
int [] Y;
// 如果你想得到字符串
DataRow thisRow =(DataRow )dtExcelRecords.Rows [I];
int [] X = Convert.ToInt32(thisRow [ X]);
int [] Y =转换.ToInt32(thisRow [ Y]);



您好,初始化 int [] X int [] Y 两次导致错误:

引用:

一个名为'X'的局部变量已在此范围内定义



删除:

 < span class =code-keyword> int  [] X; 
int [] Y;





希望它有所帮助! :)


  int  [] X; 
int [] Y;
// 如果你想得到字符串
DataRow thisRow =(DataRow )dtExcelRecords.Rows [I];
int [] X = Convert.ToInt32(thisRow [ X]);
int [] Y =转换.ToInt32(thisRow [ Y]);
// double Length = Convert.ToDouble(Y);
// double X1 = Convert.ToDouble(X);
// double Y1 = Convert.ToDouble(Y);

DataRow dr = dt.NewRow();



首先你要声明X和Y两次数组,这是非法的;在你将使用它们时声明它们。其次,您尝试使用单个元素值初始化数组,这也是非法的。第三,声明变量 dr 的最后一行没有任何用处。



您的代码应该类似于:

  //  如果你想得到字符串 
DataRow thisRow =(DataRow)dtExcelRecords.Rows [i];
int X = Convert.ToInt32(thisRow [ X]);
int Y = Convert.ToInt32(thisRow [ Y]);



但是,当返回值不是有效整数时,仍然没有规定这种情况,这些值也不存在任何用途,因为它们是 for 循环的本地,并且一旦循环结束就会超出范围并丢失。我建议你回到你的参考手册,并至少阅读范围和数组。


  protected   void  btnSubmit_Click( object  sender,EventArgs e)
{
string connectionString = ;
if (FileUpload1.HasFile)
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string fileLocation = Server.MapPath( 〜/ App_Data / + fileName);
FileUpload1.SaveAs(fileLocation);
if (fileExtension == 。xls
{
connectionString = Provider = Microsoft.Jet.OLEDB。 4.0;数据源= + fileLocation + ;扩展属性= \Excel 8.0; HDR =是; IMEX = 2\ ;
}
else if (fileExtension == 。xlsx
{
connectionString = Provider = Microsoft.ACE.OLEDB.12.0; Data Source = + fileLocation + ;扩展属性= \Excel 12.0; HDR =是; IMEX = 2 \;
}
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
ArrayList List = new ArrayList();
cmd.CommandType = System.Data.CommandType.Text;
cmd.Connection = con;
OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
DataTable dtExcelRecords = new DataTable();
con.Open();
DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null );
string getExcelSheetName = dtExcelSheetName.Rows [ 0 ] [ Table_Name]。ToString();
cmd.CommandText = SELECT * FROM [ + getExcelSheetName + ];
dAdapter.SelectCommand = cmd;
dAdapter.Fill(dtExcelRecords);
if (会话[ dtInSession]!= null
{
dt =(DataTable)Session [ dtInSession];
}
for int i = 0 ; i < dtExcelRecords.Rows.Count; i ++)
{

// 如果你想得到字符串
DataRow thisRow =(DataRow)dtExcelRecords.Rows [i];
int X = Convert.ToInt32(thisRow [ X]);
int Y = Convert.ToInt32(thisRow [ Y]);

}
// polygonArea();
}
}
// 计算
private Double polygonArea( int X, int Y)
{
Double area = 0 . 0 ;
int j = X.Length - 1 ;
for int i = 0 ; i < X.Length; i ++)
{
area = area +(X [j] + X [i] * (Y [j] - Y [i]));
j = i;
}
area = area / 2 ;
if (area < 0
area = area * -1;
返回区域;
}

}



错误://

' int'不包含'Length'的定义,并且没有扩展方法'Length'可以找到接受类型'int'的第一个参数(你是否缺少using指令或汇编引用?)


using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Collections;
using System .Collections.Generic ;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Data.OleDb;
using System.ComponentModel;
 
public partial class _Default : System.Web.UI.Page
{
    //class Point  { double X, Y; }
    DataTable dt = new DataTable();
   
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string connectionString = "";
        if (FileUpload1.HasFile)
        {
            string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
            string fileLocation = Server.MapPath("~/App_Data/" + fileName);
            FileUpload1.SaveAs(fileLocation);
            if (fileExtension == ".xls")
            {
                connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
            }
            else if (fileExtension == ".xlsx")
            {
                connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
            }
            OleDbConnection con = new OleDbConnection(connectionString);
            OleDbCommand cmd = new OleDbCommand();
            ArrayList List = new ArrayList();
            cmd.CommandType = System.Data.CommandType.Text;
            cmd.Connection = con;
            OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
            DataTable dtExcelRecords = new DataTable();
            con.Open();
            DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
            cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
            dAdapter.SelectCommand = cmd;
            dAdapter.Fill(dtExcelRecords);
            if (Session["dtInSession"] != null)
            {
                dt = (DataTable)Session["dtInSession"];
            }
            for (int i = 0; i < dtExcelRecords.Rows.Count; i++)
            {
                int [] X;
                int [] Y;
                //if you want to get the string
                DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
                int[] X = Convert.ToInt32(thisRow["X"]);
              int[]   Y = Convert .ToInt32(thisRow["Y"]);
                //double Length = Convert.ToDouble(Y);
              //double X1 = Convert.ToDouble(X);
              //double Y1 = Convert.ToDouble(Y); 
 
                DataRow dr = dt.NewRow();
                
            }
 //polygonArea();
        }
    }
    //calculation
    private Double polygonArea(int[] X, int[] Y)
    {
        Double area = 0.0;
        int j = X.Length - 1;
        for (int i = 0; i < X.Length; i++)
        {
            area = area + (X[j] + X[i] * (Y[j] - Y[i]));
            j = i;
        }
        area = area / 2;
        if (area < 0)
            area = area * -1;
        return area;
    }
 
}

解决方案

int [] X;
int [] Y;
//if you want to get the string
DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
int[] X = Convert.ToInt32(thisRow["X"]);
int[] Y = Convert .ToInt32(thisRow["Y"]);


Hi, you have initialize int [] X and int [] Y twice which results to an error:

Quote:

A local variable named 'X' is already defined in this scope


Delete this:

int [] X;
int [] Y;



Hope it helps! :)


int [] X;
int [] Y;
//if you want to get the string
DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
int[] X = Convert.ToInt32(thisRow["X"]);
int[]   Y = Convert .ToInt32(thisRow["Y"]);
//double Length = Convert.ToDouble(Y);
//double X1 = Convert.ToDouble(X);
//double Y1 = Convert.ToDouble(Y); 

DataRow dr = dt.NewRow();


Firstly you are declaring the arrays X and Y twice, which is illegal; declare them at the point you will use them. Secondly you are trying to initialize an array with a single element value, which is again illegal. And thirdly, the last line declaring the variable dr serves no purpose whatsoever.

Your code should be something like:

//if you want to get the string
DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
int X = Convert.ToInt32(thisRow["X"]);
int Y = Convert.ToInt32(thisRow["Y"]);


However, there is still no provision for the situation when the returned value is not a valid integer, nor are these values of any use since they are local to the for loop and will go out of scope and be lost as soon as the loop ends. I would suggest going back to your reference manuals and reading up on scope and arrays at the minimum.


protected void btnSubmit_Click(object sender, EventArgs e)
{
    string connectionString = "";
    if (FileUpload1.HasFile)
    {
        string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
        string fileExtension = Path.GetExtension(FileUpload1.PostedFile.FileName);
        string fileLocation = Server.MapPath("~/App_Data/" + fileName);
        FileUpload1.SaveAs(fileLocation);
        if (fileExtension == ".xls")
        {
            connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=2\"";
        }
        else if (fileExtension == ".xlsx")
        {
            connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + fileLocation + ";Extended Properties=\"Excel 12.0;HDR=Yes;IMEX=2\"";
        }
        OleDbConnection con = new OleDbConnection(connectionString);
        OleDbCommand cmd = new OleDbCommand();
        ArrayList List = new ArrayList();
        cmd.CommandType = System.Data.CommandType.Text;
        cmd.Connection = con;
        OleDbDataAdapter dAdapter = new OleDbDataAdapter(cmd);
        DataTable dtExcelRecords = new DataTable();
        con.Open();
        DataTable dtExcelSheetName = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        string getExcelSheetName = dtExcelSheetName.Rows[0]["Table_Name"].ToString();
        cmd.CommandText = "SELECT * FROM [" + getExcelSheetName + "]";
        dAdapter.SelectCommand = cmd;
        dAdapter.Fill(dtExcelRecords);
        if (Session["dtInSession"] != null)
        {
            dt = (DataTable)Session["dtInSession"];
        }
        for (int i = 0; i < dtExcelRecords.Rows.Count; i++)
        {
           
            //if you want to get the string
          DataRow thisRow = (DataRow)dtExcelRecords.Rows[i];
          int  X = Convert.ToInt32  (thisRow["X"]);
          int  Y = Convert.ToInt32 (thisRow["Y"]);
         
        }
//polygonArea();
    }
}
//calculation
private Double polygonArea(int X, int Y)
{
    Double area = 0.0;
    int j = X.Length - 1;
    for (int i = 0; i < X.Length; i++)
    {
        area = area + (X[j] + X[i] * (Y[j] - Y[i]));
        j = i;
    }
    area = area / 2;
    if (area < 0)
        area = area * -1;
    return area;
}

}


Errors ://

'int' does not contain a definition for 'Length' and no extension method 'Length' accepting a first argument of type 'int' could be found (are you missing a using directive or an assembly reference?)


这篇关于名为“X”的局部变量已在此范围内定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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