.NET のリストアイテムのプ​ロパティにアクセスす​るとエラーとなるのは​なぜですか?

リストアイテムのプロパティに次のように割り当てようとすると、
>> list.Item(0).prop = 1
次のエラーが表示されます:
The name 'Item' is not an accessible property for an instance of class
'System.Collections.Generic.List<ClassLibrary1*Example1>'.

 Risposta accettata

MathWorks Support Team
MathWorks Support Team circa 6 ore fa
Modificato: MathWorks Support Team circa 3 ore fa

0 voti

このエラーは、リストが構造体である場合に発生します。構造体は値(つまり参照ではない)であり、リスト項目は読み取り専用であるため、この方法でItemプロパティを変更することはできません。値をコピーし、コピーを変更して、リストに戻す必要があります。
例:
x = list.Item(0);
x.prop = 1;
list.Item(0,x); % assigns list.Item(0) the value of
リスト アイテムの詳細については、こちらを参照してください。

Più risposte (0)

Prodotti

Release

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!