我想访问在类中定义的全局变量。我无法访问我的覆盖函数内的变量。我该怎么办?下面 是代码:我将如何访问我的类中的公共变量
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();
}
我试图访问headervalue公共覆盖空洞内的OnEndPage method.I我能够访问headervalue到btnExport点击方法
您能否只粘贴相关代码? –
'headervalue'被声明**其中**?你不能在类之外声明它,因为你的粘贴代码显示 –
看起来你试图通过将它放在类之外来定义*“全局变量”*。你不能在C#中做到这一点,它是无效的语法。对此的解决方案将取决于您认为“全局变量”是什么? – Liam