Link to home
Create AccountLog in
Avatar of rwheeler23
rwheeler23Flag for United States of America

asked on

C# non-ref returning property

I am currently updating a 15 year C# project. There are several sections containing code like this.

   Array.Resize(ref eConnect.GLAccountType, 1);
   eConnect.GLAccountType[0] = GLAccount;

eConnect is reference that contains many methods. On the Array.Resize line I get this message on all occurrences.

"A non ref-returning property or  indexer may not be used as an out or ref value"

I have the original project and I get the same messages in there. When I run the code it appears to work if I comment out these lines. I have added the new reference for eConnect with this project.


Avatar of rwheeler23
rwheeler23
Flag of United States of America image

ASKER

I have also ran into this statement when I tried to run the code:
"Could not find any resources appropriate for the specified culture or the neutral culture"
I copied the cs and designer files from the old project and changed the name space in both. I then added this existing code to this project. Is there something else I could be doing?
Avatar of AndyAinscow

The code looks OK at first glance but something isn't correct.  Can you post how eConnect.GLAccountType  is declared/defined.


I cannot get to the eConnect code but I can show you this:

This is in something called eConnectionTpe.decomplied
  private GLAccountType[] gLAccountTypeField;

        [XmlElement("GLAccountType", Form = XmlSchemaForm.Unqualified)]
        public GLAccountType[] GLAccountType
        {
            get
            {
                return gLAccountTypeField;
            }
            set
            {
                gLAccountTypeField = value;
            }
        }

eConnect is from a third party ?

            dummy dd = new dummy();
            int iSizeBefore = dd.iArray.Length;             Array.Resize<int>(ref dd.iArray, 2);             int iSizeAfter = dd.iArray.Length;                         iSizeBefore = dd.GLAccountType.Length;             //Array.Resize<int>(ref dd.GLAccountType, 9);
            dd.ResizeArray(9);             iSizeAfter = dd.GLAccountType.Length;             int x = 9;

Open in new window


    public class dummy
    {         public int[] iArray = new int[7];         public int[] GLAccountType         {             get             {                 return iArray;             }             set             {                 iArray = value;             }         }         public void ResizeArray(int iSize)
        {             Array.Resize<int>(ref iArray, iSize);         }     }

Open in new window


Resizing works perfectly when accessing the array directly.  I get the same error as you when attempting to use the property.  Does the third party code have a function to resize the array (or can you just use the variable in the other class directly rather than via the property?

eConnect is an API for Microsoft Dynamics GP. It is a collection of nodes for writing to the GP databases. I have not used it like this. I do the serialization right in the code and do not bother with creating a temporary xml file. I will test out your suggestion and if does not work I will revert to the way I have always done this.
I gave up with this old approach and converted to what I have always used. This code was from 15 years ago.
ASKER CERTIFIED SOLUTION
Avatar of AndyAinscow
AndyAinscow
Flag of Switzerland image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer