hello guys, how can my program gives automatic code numbers?

for example

the first student has the code number 1,the second student the code number 2

One way to do this is have a static field in the Student class to keep track of what the next ID should be. Then, every time you create a new Student object, use that value and increment it so the next Student gets the next ID, and so on.

Like this:

class Student
{
    private static int nextID = 1;
    
    public int ID { get; private set; }
    
    public Student()
    {
        ID = nextID++;
    }
}

Edited 5 Years Ago by gusano79: typo

well guys, i'll tell you a problem because i want you to tell more things...

the program will ask the personal information for each customer of a video club. It will save them at an array of 3 positions. Then we could do changes from a menu...


the code will take an automatic value each time we got a new client(object)
the number will count the number of the new clients

the program wll end when the user types the number 0 from the menu.


i have done something of that but i need your help with

the code
the number
and with the array

using System;

class VideoClub
{

        int code;
        static int number;
        string name;
        int movies_bor;
        

       



        public void SetName(string name)
        {
            this.name= name;

        }

        public string GetName()
        {

            return name;
        }

        public void SetMovBor(int movies_bor)
        {
            this.movies_bor = movies_bor;
        }

        public double GetMoviesBor()
        {
            return movies_bor;
        }


        public void Borrow()
        {

            Console.WriteLine(" Name of client :{0} ", GetName());
            Console.WriteLine(" number of Movies Borrowed : {0}", GetMovBor());           
                  
        
        }

        public void Return()
        {
            Console.WriteLine(" Name of client: {0}", GetName());         
            Console.WriteLine(" Number of movies want to return: {0}",GetMovBor());            
            

        }

       

        }

        public void Menu()
        {
            int choice;

             do
             {
                Console.WriteLine("--Menu--");  
                Console.WriteLine("1. Borrow");
                Console.WriteLine("2. Return");
                Console.WriteLine("0. exit");
                Console.WriteLine("Your choice is: ");
                choice= (Int32.Parse(Console.ReadLine()));
                Console.WriteLine(" the code of the client: ");

            switch (choice)
            {
                case 1: Borrow(); break;
                case 2: Return(); break;
                case 0: break;
                default: Console.WriteLine("wrong choice"); break;



            }
            
            } while (choice!= 0);

        }
    }
    }

}


    class Program
    {

         static void Main()
        {

            VideoClub[] array= new VideoClub[3];
             VideoClub client1= new VideoClub();
             VideoClub client2= new VideoClub();
             VideoClub client3= new VideoClub();

           
            Console.WriteLine("first client");
            Console.WriteLine("name: ");
            client1.SetName(Console.ReadLine());
                   
            Console.WriteLine("number of movies has borrowed: ");
           client1.SetMoviesB(Int32.Parse(Console.ReadLine()));

            client1.Menu;




            Console.WriteLine("second client");
            Console.WriteLine("name: ");
            client2.SetName(Console.ReadLine());
            Console.WriteLine("number of movies has borrowed: ");
          client2.SetMoviesB(Int32.Parse(Console.ReadLine()));

            client2.Menu();

            Console.WriteLine("third client");
            Console.WriteLine("name: ");
            client3.SetName(Console.ReadLine());
            Console.WriteLine("number of movies has borrwed: ");
            client3.SetMoviesB(Int32.Parse(Console.ReadLine()));

            client3.Menu();
             }
      

        }
    }

Edited 5 Years Ago by Freedom*: n/a

Do you want to refresh the values with each execution of the program or you want to save it for future use?

i want to save at the array te personal information of the clients and then we could do changes on them with the menu....

i'm not sure about what you asked me...

I am asking the same question which you have answered .... now y not you add an integer holding the autonumber ... e.g. count=0 and update with each iteration ?

Edited 5 Years Ago by abelLazm: n/a

ok, just tell me how i will increase the variable number and how i will get the code automatically from the program

This article has been dead for over six months. Start a new discussion instead.