所以我读过如果你从一个类型结构中获取一个类型的结构,你得到一个副本,因为结构不是可变的。这样说,如果你想做出改变,你必须更换整个 列表中的结构。现在我使用以下代码经历了非常奇怪的行为
if pressed then
let ret = rectangles.Item(ret_index)
ret.Offset(loc.X - offs.Width - ret.X, loc.Y - offs.Height - ret.Y)
rectangles.Item(ret_index) <- ret
for i = 0 to couples.Count - 1 do
match couples.Item(i) with
| (idx_s, idx_e, rectf, string) when idx_s = ret_index ||
idx_e = ret_index ->
let mutable r = rectf
let x = (rectangles.Item(idx_e).X + rectangles.Item(idx_s).X) / 2
let y = (rectangles.Item(idx_e).Y + rectangles.Item(idx_s).Y) / 2
r.X <- single x
r.Y <- single y
couples.Item(i) <- (idx_s, idx_e, r, string)
定义一个可变的Rectangle是不好的做法? (我读过可变结构是“邪恶的”) 现在我的真正问题是:为什么上面的代码可以工作,但以下代码没有?
if pressed then
let ret = rectangles.Item(ret_index)
ret.Offset(loc.X - offs.Width - ret.X, loc.Y - offs.Height - ret.Y)
rectangles.Item(ret_index) <- ret
for i = 0 to couples.Count - 1 do
match couples.Item(i) with
| (idx_s, idx_e, rectf, string) when idx_s = ret_index ||
idx_e = ret_index ->
let r = rectf
let x = (rectangles.Item(idx_e).X + rectangles.Item(idx_s).X) / 2
let y = (rectangles.Item(idx_e).Y + rectangles.Item(idx_s).Y) / 2
r.Offset(single x - r.X, single y - r.Y)
couples.Item(i) <- (idx_s, idx_e, r, string)