0

变量“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=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"))"

4

8 回答 8

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());
    }
于 2012-06-19T13:29:09.480 回答
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)
于 2012-06-19T13:29:30.570 回答
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...
于 2012-06-19T13:30:19.260 回答
1

重构代码。

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

未初始化或仅保留其默认值的变量是不好的。编译器将无法捕获对未分配变量的任何滥用(因为它的默认值现在为空)。

基本上:

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

doc.DrawToWeb();

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

于 2012-06-19T13:33:36.027 回答
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);
}
于 2012-06-19T13:28:11.600 回答
0

在输入 if 之前声明它。

声明是这一行:

 MergeDocument doc;

然后将其分配到您需要的任何地方

doc = PDF ...
于 2012-06-19T13:28:39.243 回答
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()); 
        } 
于 2012-06-19T13:30:08.103 回答
-1

您需要在语句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());
        }
于 2012-06-19T13:33:42.567 回答