none
System.Array' does not contain a definition for 'Add' and no extension method 'Add' when i use List<int> in Client Side.

    Question

  • Hello,

                   I'm a beginner in WCF Ria Services, and i have a problem that i outiline below:

    I have a DLL named Contract, inside it i have:

    private List<int> permission;
    
    public class User{
    
     [DataMember]
    
            public List<int> Permission{
    
                get { return this.permission; }
                set { this.permission= value;}
            }
    
    }
            public User() {
    
               this.Permission = new List<int>();
    
            }

    So... When I try to use this List<int> in the Client Side like showed below:

      User user= new User();
    
     user.Permission.Add(1); //ERROR.
    
     ServiceTest service= new ServiceTest ();
     service.CadastraUser(user);

    An Error is presented like showed below:

    Error 1 'System.Array' does not contain a definition for 'Add' and no extension method 'Add' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)

     

    How Can I solve this problem ? And how Can I  fill this List<int> in the Client Side ?

     

    P.S: I Use WCF Ria Services.

     

    Thank you, so much.

    Best Regards.

     

    Thursday, August 18, 2011 3:50 PM

Answers

  • On the client Permissions is not a List<int>, it is an IEnumerable<int> and cannot be directly added or deleted from. If you want to change the collection you have to replace it completely. For example:

     


    List<int> Permission = user.Permission.ToList();
    Permission.Add(1);
    user.Permission = Permission;

     

    EDIT: In case you are wondering why it isn't just a List, if it was a list like it was on the server there would be no way for the Entity to be notified if you changed the contents of the List. You would add something to it but the EntityState would stay unmodified. This way you at least have to change the IEnumerable that the Entity started with to a List before you can start adding and removing things from it making sure that the EntityState changes.

    Friday, August 19, 2011 10:51 AM
  • If you are using WCF RIA Services then you shouldn't have anything in Service References to configure. The Silverlight client for RIA Services is generated using the WCF RIA Services Link in the project properties, not a service reference.

                   List<int> TestPermission;
                   if (user.Permission != null)
                        user.Permission.ToList();
                   else
                       TestPermission = new List<int>();
                   TestPermission.Add(1); user.Permission = TestPermission;

    Friday, August 19, 2011 2:08 PM

All replies

  • Could it be that you are accessing the wrong property?

     

    I noticed the property in the class is Permission but in the error line it is Permissao. It that just a typo in the forum or is that collection an array and you make the typo in code?

     

     

    Thanks,

     

    Brad

    Friday, August 19, 2011 9:57 AM
  • I'm sorry, I typed the name of the property wrong, I've corrected the mistake.

    Best Regards.

    Friday, August 19, 2011 10:26 AM
  • On the client Permissions is not a List<int>, it is an IEnumerable<int> and cannot be directly added or deleted from. If you want to change the collection you have to replace it completely. For example:

     


    List<int> Permission = user.Permission.ToList();
    Permission.Add(1);
    user.Permission = Permission;

     

    EDIT: In case you are wondering why it isn't just a List, if it was a list like it was on the server there would be no way for the Entity to be notified if you changed the contents of the List. You would add something to it but the EntityState would stay unmodified. This way you at least have to change the IEnumerable that the Entity started with to a List before you can start adding and removing things from it making sure that the EntityState changes.

    Friday, August 19, 2011 10:51 AM
  • ColinBlair,

    Thank you so much for your attention, below i put whole code to you see completely my problem:

            [TestMethod]
            [ExpectedException(typeof(FaultException<DetailsError>))]
            public void storeUser_MustThrowExceptionForWrongDate()
            {
                try
                {
                    User user = new User();
                    user.Name = "TestWCFSErvice";
                    user.Email = "testWCF@teste.com";
                    user.Logon = "testeWCF";
                    user.Senha = "WCFData10";
                    user.Phone = "23455678";
                    user.Initial_Date = Convert.ToDateTime("11/12/2011");
                    user.Final_Date = Convert.ToDateTime("01/12/2009");
    
                    // BEFORE.
                    //user.Permission.Add(1);
    
                   // AFTER YOUR HINT.
                   List<int> TestPermission = user.Permission.ToList(); //ERROR:System.ArgumentNullException {"Value cannot be null.\r\nParameter name: source"}
                   TestPermission.Add(1);
                   user.Permission = TestPermission;
    
    
                    ServiceAutentication service = new ServiceAutentication();
                    service .storeUser(user);
                    service .Close();
                }
                catch (FaultException<DetailsError> eX)
                {
                    Assert.AreEqual("Final Date or Initial Date is Wrong", eX.Reason.ToString());
                    throw;
                }
            }

    P.S: I selected my Service in Service References -> Configure Service Reference -> In Collection Type I selected System.Collection.Generic.List

    I still have a problem, showed below:

    System.ArgumentNullException {"Value cannot be null.\r\nParameter name: source"}

    How Can Solve this problem ?

    Best Regards.

    Thanks beforehand.

    Friday, August 19, 2011 1:55 PM
  • If you are using WCF RIA Services then you shouldn't have anything in Service References to configure. The Silverlight client for RIA Services is generated using the WCF RIA Services Link in the project properties, not a service reference.

                   List<int> TestPermission;
                   if (user.Permission != null)
                        user.Permission.ToList();
                   else
                       TestPermission = new List<int>();
                   TestPermission.Add(1); user.Permission = TestPermission;

    Friday, August 19, 2011 2:08 PM
  • ColinBlair,

    Thank you so much for your attention, I've solved the problem...to be honest you solved the problem. But there's one special thing that i'd like to say, I was wrong because I'm not using WCF RIA SERVICES... I use Silverlight-enabled WCF SERVICE, using it is correct that configuration that I've done in the Service Reference Setting, about the Collection Type ?

    Thank you again.

    Friday, August 19, 2011 2:38 PM
  • Yes, the service reference would be correct for a plain WCF service.

    Friday, August 19, 2011 3:06 PM
  • hiii can u tell me which property's name u changed i find the same error in my program also......

    Monday, February 13, 2012 3:44 AM