gpt4 book ai didi

c# - 无法分配从方法返回的结构的属性

转载 作者:行者123 更新时间:2023-11-30 12:18:21 25 4
gpt4 key购买 nike

我从德语翻译得到以下错误:

错误 BC30068:表达式是一个值,不能作为赋值的目标。

我正在尝试执行以下操作:

sheet.Cells(row, col).Value = newVal ' this is VB

我将 Cells 声明为:

public Cell Cells(int x, int y) // this is C#
{
return new Cell(this, x, y);
}

public struct Cell
{
Worksheet ws;
int x;
int y;

public Cell(Worksheet ws, int x, int y)
{
this.ws = ws;
this.x = x;
this.y = y;
}

public object Value
{
get { return ws.GetCellValue(x, y); }
set { ws.SetCellValue(x, y, value); }
}
}

我该如何解决这个问题?我不希望 Cell 成为一个类,而是成为一个结构,否则任何访问都会在堆上创建一个新对象。当然,我可以缓存所有创建的 Cell 对象,但这可能会引入错误并浪费大量内存和新的间接层。

有更好的方法吗?

最佳答案

把它变成一个类有什么问题?

另外,为什么不直接使用索引器:

public object this[int column, int row] {
get { return this.GetCellValue(column, row); }
set { this.SetCellValue(column, row, value); }
}

这样,你就可以像那样使用它了:

sheet[column, row] = newValue;

关于c# - 无法分配从方法返回的结构的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2333749/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com