2016-02-15 52 views
-5

所以我是一個初學者,用c#試驗了Microsoft.Office.Interop.Excel參考,並且遇到了一個問題。這是我的主要形式有:名稱'BankAccounts'在當前上下文中不存在

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public void Form1_Load(object sender, EventArgs e) 
    { 
     var BankAccounts = new List<Account> 
     { 
      new Account 
      { 
       ID = 345, 
       Balance = 541.27 
      }, 
      new Account 
      { 
       ID = 123, 
       Balance = -127.44 
      } 
     }; 
    } 

    public void button1_Click(object sender, EventArgs e) 
    { 
     ThisAddIn.DisplayInExcel(BankAccounts, (Account, cell) => 
     // This multiline lambda expression sets custom processing rules 
     // for the bankAccounts. 
     { 
      cell.Value = Account.ID; 
      cell.Offset[0, 1].Value = Account.Balance; 
      if (Account.Balance < 0) 
      { 
       cell.Interior.Color = 255; 
       cell.Offset[0, 1].Interior.Color = 255; 
      } 
     }); 

    } 
} 

返回錯誤:

The name 'BankAccounts' does not exist in the current context

我不明白這是怎麼發生的,可能有人請幫助我解決這個問題,或許解釋了什麼引起的呢?

非常感謝!

+9

'BankAccounts'只有範圍內的存在'Form1_Load'的方法# –

+0

啊謝謝你,我看到我要去哪裏錯了。我怎樣才能讓'BankAccounts'可以從不同的課程中使用? – Acid

+0

要從另一個類使用它,您必須添加一個getter函數或將該變量設爲public。 – mike

回答

0

請BankAccounts可變Form1的範圍內(更上scopes):

public partial class Form1 : Form 
{ 
    // when placed here, any method in Form1 can access it 
    List<Account> BankAccounts; 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    public void Form1_Load(object sender, EventArgs e) 
    { 
     BankAccounts = new List<Account> 
     { 
      new Account 
      { 
       ID = 345, 
       Balance = 541.27 
      }, 
      new Account 
      { 
       ID = 123, 
       Balance = -127.44 
      } 
     }; 
    } 

    public void button1_Click(object sender, EventArgs e) 
    { 
     ThisAddIn.DisplayInExcel(BankAccounts, (Account, cell) => 
     // This multiline lambda expression sets custom processing rules 
     // for the bankAccounts. 
     { 
      cell.Value = Account.ID; 
      cell.Offset[0, 1].Value = Account.Balance; 
      if (Account.Balance < 0) 
      { 
       cell.Interior.Color = 255; 
       cell.Offset[0, 1].Interior.Color = 255; 
      } 
     }); 

    } 
} 
+0

上下文關鍵字'var'可能只出現在局部變量聲明中 – Sayse

+0

我試過使用這個確切的代碼,但它似乎返回相同的錯誤。 – Acid

+0

'var'只能在方法範圍內聲明變量時使用。將其更改爲'List BankAccount;'。 –

1

作爲BankAccounts一類字段:

public partial class Form1 : Form 
{ 




private List<Account> BankAccounts; 

public Form1() 
{ 
    InitializeComponent(); 
} 

public void Form1_Load(object sender, EventArgs e) 
{ 
    BankAccounts = new List<Account> 
    { 
     new Account 
     { 
      ID = 345, 
      Balance = 541.27 
     }, 
     new Account 
     { 
      ID = 123, 
      Balance = -127.44 
     } 
    }; 
} 

public void button1_Click(object sender, EventArgs e) 
{ 
    ThisAddIn.DisplayInExcel(BankAccounts, (Account, cell) => 
    // This multiline lambda expression sets custom processing rules 
    // for the bankAccounts. 
    { 
     cell.Value = Account.ID; 
     cell.Offset[0, 1].Value = Account.Balance; 
     if (Account.Balance < 0) 
     { 
      cell.Interior.Color = 255; 
      cell.Offset[0, 1].Interior.Color = 255; 
     } 
    }); 

} 

}

相關問題