名称' BankAccounts'在当前上下文中不存在

时间:2016-02-15 11:07:17

标签: c# c#-4.0

所以我是一名初学者,用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;
            }
        });

    }
}

返回错误:

  

名称&#39; BankAccounts&#39;在当前上下文中不存在

我无法理解这是怎么回事,有人可以帮助我解决这个问题并解释是什么导致了这个问题吗?

非常感谢!

2 个答案:

答案 0 :(得分: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;
        }
    });

}

}

答案 1 :(得分: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;
            }
        });

    }
}