Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何处理可变范围问题_C#_Asp.net - Fatal编程技术网

C# 如何处理可变范围问题

C# 如何处理可变范围问题,c#,asp.net,C#,Asp.net,变量“doc”在if语句之外的方法中的任何其他地方都不可访问,因此if doc==null失败,因为“doc”的范围仅在定义它的if语句中……我如何处理此问题?添加public只会导致更多错误 protected void Page_Load(object sender, EventArgs e) { try { string id, type, UniqueColID; string FilePath =

变量“doc”在if语句之外的方法中的任何其他地方都不可访问,因此if doc==null失败,因为“doc”的范围仅在定义它的if语句中……我如何处理此问题?添加public只会导致更多错误

protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string id, type, UniqueColID;
            string FilePath = Server.MapPath("~").ToString();
            type = Request.QueryString["type"];

            if (type.Equals("template"))
            {
                MergeDocument template = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");
                template.DrawToWeb();
            }
            else
            {
                id = Request.QueryString["id"];
                UniqueColID = DBFunctions.DBFunctions.testExist(id);
                if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
                {
                    MergeDocument doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
                }
                else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
                {
                    MergeDocument doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
                }
                DBFunctions.DBFunctions.FlagDriverPrintOnly = false;
                    if (doc == null)
                    doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");

                doc.DrawToWeb();
            }
        }
        catch(Exception err)
        {
            MessageBox("Creating PDF file was not successful. " + err.ToString());
        }
我已尝试在更高级别上声明它,但仍然出现相同的错误:

**Use of unassigned local variable 'doc' -->>>at: if (doc==nul)**   
执行MergeDocument后=空;在更高的级别,我得到一个新错误:

System.NullReferenceException: Object reference not set to an instance of an object. at GeneratePDF.Page_Load(Object sender, EventArgs e)

此错误指向
“if(type.Equals(“template”)”

解决方案相当简单。我想你是c#的新手吧。只需将
doc
变量移动到更大的范围:

MergeDocument doc = null;
if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
{
    doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
}
else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
{
    doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
}

解决方案相当简单。我想你是c#的新手吧。只需将
doc
变量移动到更大的范围:

MergeDocument doc = null;
if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
{
    doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
}
else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
{
    doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
}

在输入if之前进行申报

声明如下:

 MergeDocument doc;
然后将其分配到任何需要的地方

doc = PDF ...

在输入if之前进行申报

声明如下:

 MergeDocument doc;
然后将其分配到任何需要的地方

doc = PDF ...
简单的方法。试试这个:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        string id, type, UniqueColID;
        string FilePath = Server.MapPath("~").ToString();
        type = Request.QueryString["type"];

        if (type.Equals("template"))
        {
            MergeDocument template = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");
            template.DrawToWeb();
        }
        else
        {
            id = Request.QueryString["id"];
            UniqueColID = DBFunctions.DBFunctions.testExist(id);
            MergeDocument doc;
            if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
            {
               doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
            }
            else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
            {
                doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
            }
            DBFunctions.DBFunctions.FlagDriverPrintOnly = false;
                if (doc == null)
                doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");

            doc.DrawToWeb();
        }
    }
    catch(Exception err)
    {
        MessageBox("Creating PDF file was not successful. " + err.ToString());
    }
简单的方法。试试这个:

protected void Page_Load(object sender, EventArgs e)
{
    try
    {
        string id, type, UniqueColID;
        string FilePath = Server.MapPath("~").ToString();
        type = Request.QueryString["type"];

        if (type.Equals("template"))
        {
            MergeDocument template = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");
            template.DrawToWeb();
        }
        else
        {
            id = Request.QueryString["id"];
            UniqueColID = DBFunctions.DBFunctions.testExist(id);
            MergeDocument doc;
            if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
            {
               doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
            }
            else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
            {
                doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
            }
            DBFunctions.DBFunctions.FlagDriverPrintOnly = false;
                if (doc == null)
                doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");

            doc.DrawToWeb();
        }
    }
    catch(Exception err)
    {
        MessageBox("Creating PDF file was not successful. " + err.ToString());
    }

在实际使用变量之前定义
doc
变量:

MergeDocument doc = null;
if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false) 
{ 
    doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID); 
} 
else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true) 
{ 
    doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID); 
} 
DBFunctions.DBFunctions.FlagDriverPrintOnly = false; 
if (doc == null)

在实际使用变量之前定义
doc
变量:

MergeDocument doc = null;
if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false) 
{ 
    doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID); 
} 
else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true) 
{ 
    doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID); 
} 
DBFunctions.DBFunctions.FlagDriverPrintOnly = false; 
if (doc == null)

doc
变量的此声明移动到函数顶部

protected void Page_Load(object sender, EventArgs e) 
    { 
        MergeDocument doc // Move the declaration here..
        try 
        { 
            string id, type, UniqueColID; 
            string FilePath = Server.MapPath("~").ToString(); 
            type = Request.QueryString["type"]; 

            if (type.Equals("template")) 
            { 
                MergeDocument template = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf"); 
                template.DrawToWeb(); 
            } 
            else 
            { 
                id = Request.QueryString["id"]; 
                UniqueColID = DBFunctions.DBFunctions.testExist(id); 
                if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false) 
                { 
                    doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID); 
                } 
                else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true) 
                { 
                    doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID); 
                } 
                DBFunctions.DBFunctions.FlagDriverPrintOnly = false; 
                    if (doc == null) 
                    doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf"); 

                doc.DrawToWeb(); 
            } 
        } 
        catch(Exception err) 
        { 
            MessageBox("Creating PDF file was not successful. " + err.ToString()); 
        } 

doc
变量的此声明移动到函数顶部

protected void Page_Load(object sender, EventArgs e) 
    { 
        MergeDocument doc // Move the declaration here..
        try 
        { 
            string id, type, UniqueColID; 
            string FilePath = Server.MapPath("~").ToString(); 
            type = Request.QueryString["type"]; 

            if (type.Equals("template")) 
            { 
                MergeDocument template = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf"); 
                template.DrawToWeb(); 
            } 
            else 
            { 
                id = Request.QueryString["id"]; 
                UniqueColID = DBFunctions.DBFunctions.testExist(id); 
                if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false) 
                { 
                    doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID); 
                } 
                else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true) 
                { 
                    doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID); 
                } 
                DBFunctions.DBFunctions.FlagDriverPrintOnly = false; 
                    if (doc == null) 
                    doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf"); 

                doc.DrawToWeb(); 
            } 
        } 
        catch(Exception err) 
        { 
            MessageBox("Creating PDF file was not successful. " + err.ToString()); 
        } 

单据
声明和转让分开,并将声明放在适当的范围内:

MergeDocument doc = null;

if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
{
    doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
}
else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
{
    doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
}

// Use the `doc` below as appropriate...

单据
声明和转让分开,并将声明放在适当的范围内:

MergeDocument doc = null;

if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
{
    doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
}
else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
{
    doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
}

// Use the `doc` below as appropriate...

重构代码。

将对象的创建提取到新方法。这样,程序的流程就更清晰了

未初始化或仅保留默认值的变量是错误的。编译器将无法捕获未赋值变量的任何误用(因为它的默认值现在为null)

基本上:

var doc = GetMergeDocument(id, type, FilePath, UniqueColID)
if (doc == null)
    doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");

doc.DrawToWeb();

通过这种方式,我们还可以判断如果
GetMergeDocument
返回null,我们将相应地处理它。

重构代码。

将对象的创建提取到新方法。这样,程序的流程就更清晰了

未初始化或仅保留默认值的变量是错误的。编译器将无法捕获未赋值变量的任何误用(因为它的默认值现在为null)

基本上:

var doc = GetMergeDocument(id, type, FilePath, UniqueColID)
if (doc == null)
    doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");

doc.DrawToWeb();

通过这种方式,我们还可以判断如果
GetMergeDocument
返回null,我们将相应地处理它。

您需要在
try
语句外部或
else
语句内部定义
MergeDocument
,以便在
if…else if
语句外部使用它

protected void Page_Load(object sender, EventArgs e)
    {
                    MergeDocument doc = null;
        try
        {
            string id, type, UniqueColID;
            string FilePath = Server.MapPath("~").ToString();
            type = Request.QueryString["type"];

            if (type.Equals("template"))
            {
                MergeDocument template = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");
                template.DrawToWeb();
            }
            else
            {
                id = Request.QueryString["id"];
                UniqueColID = DBFunctions.DBFunctions.testExist(id);
                if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
                {
                    doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
                }
                else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
                {
                    doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
                }
                DBFunctions.DBFunctions.FlagDriverPrintOnly = false;
                    if (doc == null)
                    doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");

                doc.DrawToWeb();
            }
        }
        catch(Exception err)
        {
            MessageBox("Creating PDF file was not successful. " + err.ToString());
        }

您需要在
try
语句外部或
else
语句内部定义
MergeDocument
,以便在
if…else if
语句外部使用它

protected void Page_Load(object sender, EventArgs e)
    {
                    MergeDocument doc = null;
        try
        {
            string id, type, UniqueColID;
            string FilePath = Server.MapPath("~").ToString();
            type = Request.QueryString["type"];

            if (type.Equals("template"))
            {
                MergeDocument template = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");
                template.DrawToWeb();
            }
            else
            {
                id = Request.QueryString["id"];
                UniqueColID = DBFunctions.DBFunctions.testExist(id);
                if (DBFunctions.DBFunctions.FlagDriverPrintOnly == false)
                {
                    doc = PDF.LongFormManipulation.generatePDF(id, type, FilePath, UniqueColID);
                }
                else if (DBFunctions.DBFunctions.FlagDriverPrintOnly == true)
                {
                    doc = PDF.LongFormManipulation.generatePDFDriverOnly(id, type, FilePath, UniqueColID);
                }
                DBFunctions.DBFunctions.FlagDriverPrintOnly = false;
                    if (doc == null)
                    doc = new MergeDocument(FilePath + @"\Template\MVCTemplate.pdf");

                doc.DrawToWeb();
            }
        }
        catch(Exception err)
        {
            MessageBox("Creating PDF file was not successful. " + err.ToString());
        }

更高级别的申报单?例如,在顶部的“else”语句中,在id=Request.QueryString[“id”]之前@纳达尔-如果你尝试了其他东西,你应该提供代码。在更高级别上声明文档?例如,在顶部的“else”语句中,在id=Request.QueryString[“id”]之前@Nadal-如果你尝试了其他方法,你应该提供代码。太多了,变量不应该离开“else”块。太多了,变量不应该离开“else”块。除了你的解决方案,为了简化事情,我从generatePDFDriverOnly获取代码,并将其与FlagDriverPrintOnly标记一起发布到generatePDF的代码中,这让我绕过了任何其他错误,并简化了代码。除了您的解决方案,为了简化事情,我从generatePDFDriverOnly获取了代码,并将其与FlagDriverPrintOnly标志一起发布在generatePDF的代码中,这让我绕过了任何其他错误,并简化了代码。如果您要对一个完美有效的问题进行否决表决,我将非常感谢您留下评论如果您要对一个完美有效的问题进行否决表决,我将非常感谢你留下评论吗