如何处理变量范围问题

时间:2012-06-19 13:25:03

标签: c# asp.net

变量" doc"在if语句之外的方法中的任何其他地方都不可访问,因此如果doc == null失败,因为" doc"只在那些定义的if语句中......如何处理这个问题?添加公共只会导致更多错误..

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 = null后;在更高级别我得到一个新错误:

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

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

8 个答案:

答案 0 :(得分:1)

简单的方法。试试这个:

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());
    }

答案 1 :(得分:1)

在您真正想要使用它之前定义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)

答案 2 :(得分:1)

分隔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);
}

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

答案 3 :(得分:1)

重构代码。

将对象的创建提取到新方法。通过这种方式,您的程序流程更加清晰。

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

基本上:

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

doc.DrawToWeb();

这样我们也可以告诉我,如果GetMergeDocument返回null,我们会相应地处理它。

答案 4 :(得分:0)

解决方案非常简单。我假设你是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);
}

答案 5 :(得分:0)

在输入if。

之前声明它

声明就是这一行:

 MergeDocument doc;

然后在任何需要的地方分配

doc = PDF ...

答案 6 :(得分:0)

将此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()); 
        } 

答案 7 :(得分:0)

您需要在MergeDocument语句之外或try语句中定义else,以便在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());
        }