ROY ROY - 9 days ago 6
ASP.NET (C#) Question

How will I access public variable in my class

I am trying to access a global variable defined in the class.I am unable to access the variable inside my override function.What should I do???
below is the code :

public partial class VR: System.Web.UI.Page
{
public SqlConnection SQLCONN;
public string headervalue = "";
protected void Page_Load(object sender, EventArgs e)
{

}

// code...............



public class ITextEvents : PdfPageEventHelper
{

// code....



public override void OnEndPage(iTextSharp.text.pdf.PdfWriter writer, iTextSharp.text.Document document)
{

string value = "";
// code.....................................
headervalue //error unable to access it here

}

protected void btnExport_Click(object sender, EventArgs e)
{
headervalue = "abcc";
CreatePDF();
}


I am trying to access headervalue inside public override void OnEndPage method.I am able to access headervalue into btnExport click method

Answer

A variable can only be declared inside a class.

So, in order to use it, you must do it :

public class ITextEvents : PdfPageEventHelper
{
    public static string headervalue = "";

    public override void OnEndPage(PdfWriter writer, Document document)
    {
       [..your implementation..]
    }
 }

If you want to share it between more than one class, you need to use container like SimpleIOC.

I don't know if it will help you.

Comments