“银行账户”这个名称在当前上下文中不存在

本文关键字:上下文 不存在 银行账户 | 更新日期: 2023-09-27 18:34:41

所以我是c#的初学者,尝试使用Microsoft.Office.Interop.Excel参考,我遇到了一个问题。这是我的主要形式:

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

返回错误:

"银行账户"这个名称在当前上下文中不存在

我不明白这是怎么发生的,有人可以帮我解决这个问题,也许可以解释一下是什么原因造成的?

多谢!

“银行账户”这个名称在当前上下文中不存在

将银行账户作为类字段:

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

}

使 BankAccounts 成为 Form1 范围内的变量(更多关于范围(:

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