-5

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

ROY
  • 33
  • 12
  • 4
    Can you please only paste the relevant code? – Mark C. Jun 13 '16 at 12:21
  • 2
    `headervalue` is declared **where**? You cannot declare it outside a class, as your pasted code shows – Camilo Terevinto Jun 13 '16 at 12:24
  • It appears your trying to define *"A global variable"* by putting it outside of the class. You can't do this in C#, it is invalid syntax. The solution to this will depend on whatever you think a "global variable" is? – Liam Jun 13 '16 at 12:28
  • sorry will post the code .It is defined above page load method. – ROY Jun 13 '16 at 12:30
  • o_O...above the page load method... You can't have methods outside of a class either...?! I think you should [read this](http://csharp.net-tutorials.com/classes/introduction/) – Liam Jun 13 '16 at 12:32
  • Why have you put back all the irrlevant code??! Please read [How do I ask a good question?](http://stackoverflow.com/help/how-to-ask). Your question is not clear and cutting and pasting your entire program into it does not help – Liam Jun 13 '16 at 12:46
  • Avoid global variables at all costs. It is rare that you should ever require public variables in any realistic, well-designed application. – ManoDestra Jun 13 '16 at 13:23
  • I have edited irrelevant code so you can get a better idea – ROY Jun 14 '16 at 05:05
  • Updated code..Removed Irrelevant code – ROY Nov 25 '16 at 05:37

1 Answers1

1

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.

Guillaume P.
  • 163
  • 12
  • I am trying to access headervalue into two methods one is OnEndPage and on btnExport click . I will assign some value into btnExport click an will access this value into public override method().I can access headervalue into btnExport click method – ROY Jun 14 '16 at 05:03
  • So just set `headervalue` as I did in my answer. Easy solution not the best one. – Guillaume P. Jun 14 '16 at 07:37
  • But how will I assign specific value to headervalue from btnExport method – ROY Jun 14 '16 at 08:14
  • Set it like you write it in your main post : `protected void btnExport_Click(object sender, EventArgs e) { headervalue = "abcc"; }` – Guillaume P. Jun 14 '16 at 08:20
  • but I cant access headervalue into OnEndPage method.I have to define headervalue into btnExport method and than try to access inside OnEndPage method.The flow of the code is whan I click on btnExport a value is assigned to headervalue and this value is than accessed by OnEndPage – ROY Jun 14 '16 at 08:33
  • I want to access headervalue at two position inside btnExport method which is present inside VR class and I also want to access it inside iTextEvents class – ROY Jun 14 '16 at 08:45
  • there is no error .I am just unable to access headervalue inside OnEndPage method.I assign some value to headervalue inside btnExport method and than trying to access it inside OnEndPage method – ROY Jun 14 '16 at 09:02
  • Did you set it static ? I need to create a poc in order to help you. because I don't know the library you are using. – Guillaume P. Jun 14 '16 at 09:05
  • I have not set it as static – ROY Jun 14 '16 at 09:08
  • Can you try it please and tell me. – Guillaume P. Jun 14 '16 at 09:09
  • what information do you want from me?? Should I try to set it as static?? – ROY Jun 14 '16 at 09:20
  • Yes set it static as I did in this answer ! And try again to run your program. And tell me what happened ! – Guillaume P. Jun 14 '16 at 09:23
  • Yes It worked. Thanks man... setting the string as static worked for me.Thanks for helping me out. – ROY Jun 14 '16 at 09:31
  • No problem. As I said earlier, it is not the best way to solve it. It is the easiest one. – Guillaume P. Jun 14 '16 at 09:41