示例#1
0
        public Contact Update(int id, Contact contact)
        {
            if (id <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(id), "Id must be > 0.");
            }
            if (contact == null || !contact.Validate())
            {
                throw new ArgumentNullException(nameof(contact));
            }

            //contact.Validate();

            var index = GetIndex(id);

            if (index < 0)
            {
                throw new Exception("Contact does not exist.");
            }

            //Game names must be unique
            var existingIndex = GetIndex(contact.Name);

            if (existingIndex >= 0 && existingIndex != index)
            {
                throw new Exception("The contact's name needs to be unique.");
            }

            contact.Id = id;
            var existing = _items[index];

            Clone(existing, contact);

            return(contact);
        }
示例#2
0
        public Contact Add(Contact contact)
        {
            //Validate
            if (contact == null || !contact.Validate())
            {
                throw new ArgumentNullException(nameof(contact));
            }

            //Game names must be unique
            var existing = FindByName(contact.Name);

            if (existing != null)
            {
                throw new Exception("Contact must be unique.");
            }



            for (var index = 0; index < _items.Length; ++index)
            {
                if (_items[index] == null)
                {
                    contact.Id    = ++_nextId;
                    _items[index] = Clone(contact);
                    break;
                }
                ;
            }
            ;

            return(contact);
        }
示例#3
0
        public Contact Add(Contact contact)
        {
            if (contact == null)
            {
                throw new ArgumentNullException(nameof(contact));
            }



            if (!contact.Validate())
            {
                throw new Exception("Contact is invalid.");
            }

            // names must be unique
            var existing = GetIndex(contact.Name);

            if (existing >= 0)
            {
                throw new Exception("Contact must be unique.");
            }


            contact.Id = ++_nextId;
            _items.Add(Clone(contact));

            return(contact);
        }