A property, indexer or dynamic member access may not be passed as an out or ref parameter? [duplicate]
-
02-07-2021 - |
Question
In the code below, I get the error
A property, indexer or dynamic member access may not be passed as an out or ref parameter?
on the m_settings.Length
argument when compiling it. How could I solve this?
public override bool Read(GH_IO.Serialization.GH_IReader reader)
{
if (m_settings != null && m_settings.Length > 0)
{
reader.TryGetInt32("StringCount", ref m_settings.Length);
for (int i = 0; i < m_settings.Length; i++)
{
reader.TryGetString("String", i, ref m_settings[i]);
}
}
return base.Read(reader);
}
La solution
How could I solve this?
By not doing that :) How would you expect it to work anyway? Assuming m_settings
is an array, an array can't change length anyway...
If you really need ref
behaviour, you'll need a local variable:
int length = m_settings.Length;
reader.TryGetInt32("StringCount", ref length);
// Presumably you want to use length here...
// Perhaps m_settings = new string[length]; ?
It's frankly a little odd that:
TryGetInt32
uses aref
parameter instead of anout
parameter, unlike the normalTryXXX
pattern- You're ignoring the return value of
TryGetInt32
, which I'd expect to be a success/failure value. Why would you want to silently ignore failure?
Autres conseils
Use a temporary local variable:
int temp = 0;
if (reader.TryGetInt32("StringCount", ref temp)) {
m_settings.Length = temp;
}
Of course this still leaves open some questions:
- you don't check the return value of
TryGetInt32
-- what if the conversion fails? - is
m.settings.Length
really writeable?
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow