Bulvak
Bulvak

Reputation: 1804

How to deal with variable scope issues

the variable "doc" is not accessible anywhere else in the method outside of the if statement so that if doc==null fails since the scope of "doc" is only within those if statements that it is defined in....How do I deal with this issue? adding public just leads to more errors..

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

I have tried declaring it at higher level but I still get same error:

**Use of unassigned local variable 'doc' -->>>at: if (doc==nul)**   

After doing MergeDocument=null; at higher level I get a new error:

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

this error points to "if (type.Equals("template"))"

Upvotes: 0

Views: 169

Answers (8)

Security Hound
Security Hound

Reputation: 2551

You need to define MergeDocument either outside of the try statement or within the else statement in order to use it outside of the if...else if statement.

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

Upvotes: -1

John Leidegren
John Leidegren

Reputation: 60987

Refactor the code.

Extract the creation of the object to a new method. This way the flow of your program is more clear.

Uninitialized or variables that just retain their default value are bad. The compiler wont be able to catch any misuse of an unassigned variable (because it's default value is now null).

Basically:

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

doc.DrawToWeb();

This way we can also tell that if GetMergeDocument returned null we would handle that accordingly.

Upvotes: 1

SHAKIR SHABBIR
SHAKIR SHABBIR

Reputation: 1295

Easy way. Try this:

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

Upvotes: 1

Branko Dimitrijevic
Branko Dimitrijevic

Reputation: 52107

Separate the doc declaration and assignment, and put the declaration in appropriate scope:

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...

Upvotes: 1

Misam
Misam

Reputation: 4389

Move this declaration of doc variable to the top of the function

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

Upvotes: 0

Neil Knight
Neil Knight

Reputation: 48547

Define the doc variable just before you actually want to use it:

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)

Upvotes: 1

Nick
Nick

Reputation: 9154

Declare it prior to entering the if.

The declaration is this line:

 MergeDocument doc;

Then assign it wherever you need

doc = PDF ...

Upvotes: 0

Jim Counts
Jim Counts

Reputation: 12795

The solution is fairly simple. I'll assume you're new to c#. Just move the doc variable to a wider scope:

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

Upvotes: 0

Related Questions