Bassam Badr
Bassam Badr

Reputation: 667

update a number automatically

Hi i am using C# 2010 Windows Forms application,and for my project i want to know how can i make a variable increment automatically when i add a new member also i excepted when i remove any member the number of reset members will decremented by 1 i hope that i make my problem clear for you and thanks for any help......sorry for my bad english

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AddressBookInC
{
    class Addresses
    {
        public int AddressID { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String AddressEmail { get; set; }
        public String PhoneNumber { get; set; }
    }
}

Upvotes: 0

Views: 116

Answers (6)

the_lotus
the_lotus

Reputation: 12748

You can do this with a static variable that increment each time a new class is generated.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AddressBookInC
{
    class Addresses
    {
        private static int _incrementId = 0;

        public Addresses()
        {
            AddressID = _incrementId++;
        }

        public int AddressID { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String AddressEmail { get; set; }
        public String PhoneNumber { get; set; }
    }
}

Sometime it's better to just let the database do it by itself. Have ID 0 for new items until they are inserted in the database.

Upvotes: 0

ElmoVanKielmo
ElmoVanKielmo

Reputation: 11315

Ok, now I know what you want.

You should use List<Address> and keep all members in the list.

I assume that AddressID is 1 based.

When adding new member, use number of elements in the list as new AddressID, then add new member to the list.

After removing member or members, sort the list by AddressID and for each member in the list, his new AddressID is his position in the list incremented by one.

Upvotes: 1

S Nash
S Nash

Reputation: 2499

So you have a class of addresses and you want to count them automatically.

Here is how to do it:

Add a public property named "Count" to your Addresses class.

Add an Event called: NumChanged to the class.

and every time user of class creates a new address or removes an address you should raise that event and set count to count+1 or count-1

Upvotes: 0

Charles Bretana
Charles Bretana

Reputation: 146557

Base your Addresses collection on one of the built-in collection classes (like List) and use it's intrinsic Count property.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AddressBookInC
{
    public class Addresses: List<Address> { }
    public class Address
    {
        public int AddressID { get; set; }
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String AddressEmail { get; set; }
        public String PhoneNumber { get; set; }
    }
}

-- client code somewhere

 var myAddresses = new Addresses();
 myAddresses.Add(new Address{AddressId = 1, FirstName= "Bob", etc. });
 myAddresses.Add(new Address{AddressId = 2, FirstName= "Dave", etc. });
 myAddresses.Add(new Address{AddressId = 3, FirstName= "Al", etc. });

 int count = myAddresses.Count();

Upvotes: 0

Nikola Davidovic
Nikola Davidovic

Reputation: 8656

Add Events, with names for example AddressBookItemAdded and AddressBookItemRemoved to your addressbook and fire them whenever you add/remove any contact. In the eventHandlers of those events just increment/decrement the number of contacts.

check this for example:http://msdn.microsoft.com/en-us/library/aa645739%28v=vs.71%29.aspx

Upvotes: 0

Dimitar Dimitrov
Dimitar Dimitrov

Reputation: 15158

Well why don't you place them in a List<Address>, something like:

List<Address> addresses = new List<Address>();

addresses.Add(new Address());
addresses.Add(new Address());

Console.WriteLine(addresses.Count); // now gives you Count of 2

addresses.RemoveAt(0);

Console.WriteLine(addresses.Count); // now gives you Count of 1

And here is your Address class:

class Address
{
    public int AddressID { get; set; }
    public String FirstName { get; set; }
    public String LastName { get; set; }
    public String AddressEmail { get; set; }
    public String PhoneNumber { get; set; }
}

Upvotes: 1

Related Questions