gpt4 book ai didi

vb.net - 通过属性分配给结构变量

转载 作者:行者123 更新时间:2023-12-01 13:00:38 24 4
gpt4 key购买 nike

我有以下内容

Public Structure Foo
dim i as integer
End Structure

Public Class Bar

Public Property MyFoo as Foo
Get
return Foo
End Get
Set(ByVal value as Foo)
foo = value
End Set

dim foo as Foo
End Class

Public Class Other

Public Sub SomeFunc()
dim B as New Bar()
B.MyFoo = new Foo()
B.MyFoo.i = 14 'Expression is a value and therefore cannot be the target of an assignment ???
End Sub
End Class

我的问题是,为什么我不能通过 Bar 类中的属性分配给 i?我做错了什么?

最佳答案

找到答案here ,它说了以下内容:

' Assume this code runs inside Form1.
Dim exitButton As New System.Windows.Forms.Button()
exitButton.Text = "Exit this form"
exitButton.Location.X = 140
' The preceding line is an ERROR because of no storage for Location.

The last statement of the preceding example fails because it creates only a temporary allocation for the Point structure returned by the Location property. A structure is a value type, and the temporary structure is not retained after the statement runs. The problem is resolved by declaring and using a variable for Location, which creates a more permanent allocation for the Point structure. The following example shows code that can replace the last statement of the preceding example.

这是因为结构只是一个临时变量。因此,解决方案是创建一个您需要的新结构,为其分配所有内部变量,然后将该结构分配给类的结构属性。

关于vb.net - 通过属性分配给结构变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6200247/

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