Search code examples
c#propertiesmemberindexergrasshopper

A property, indexer or dynamic member access may not be passed as an out or ref parameter?


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.Lengthargument 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);
                }

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 a ref parameter instead of an out parameter, unlike the normal TryXXX 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?