DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C# (http://www.daniweb.com/forums/forum61.html)
-   -   Simple Question?? (http://www.daniweb.com/forums/thread168585.html)

gouki2005 Jan 15th, 2009 1:52 pm
Simple Question??
 
namespace Empleados
{
    class Empleados
    {
        private int edad;

        static private int numeroemp = 0;




        public Empleados(int edad)
        {

            this.edad = edad;
            Empleados.numeroemp  ++;

 
       
        }

        public void mostrarInfo()
        {
            Console.WriteLine("el empleado tiene {0} años",edad );
            Console.WriteLine("Numero de empleados creados {0} ", Empleados.obtenernumeEmpledos() );
        }

        static void Main(string[] args)
        {
           
              int edad = 46;

    Empleados emp1 = new Empleados (edad);
    emp1.mostrarInfo();
    Empleados emp2 = new Empleados(45);
    emp2.mostrarInfo();


       
         
        }

        public static int obtenernumeEmpledos()
        {

            return numeroemp;
        }
    }
}

this mi question if I run this program the results are

el empleado 1 tiene 46 años
numero de empleados creados 1
el empleado 2 tiene 45 años
numero de empleados creados 2

its ok but if i change
this code

  public Empleados(int edad)
        {

            this.edad = edad;
            Empleados.numeroemp  ++;

 
       
        }
for this

  public Empleados(int edad)
        {

            this.edad = edad;
            Empleados.numeroemp  = + 1;

 
       
        }

the results are

el empleado 1 tiene 46 años
numero de empleados creados 1
el empleado 2 tiene 45 años
numero de empleados creados 1

why??

is no the same using ++ than + 1???
or i am wrong??

Antenka Jan 15th, 2009 2:38 pm
Re: Simple Question??
 
in your case it doesn't matter, which way you use to add 1. I suppose that you was talking about "+=" operator?
Let's see:
1.
Empleados.numeroemp++;
increasing numeroemp by 1.
2.
Empleados.numeroemp += 1;
which is the short version of this:
Empleados.numeroemp = Empleados.numeroemp + 1;
3.
Empleados.numeroemp  = + 1;
here you assign numeroemp by +1.

rapture Jan 15th, 2009 3:37 pm
Re: Simple Question??
 
I was going to add that the two outputs are the same . ..

LizR Jan 15th, 2009 4:23 pm
Re: Simple Question??
 
The difference is the

= +1
is the same as =1
where as

+= 1

will add one and store it.

Typos FTW

BlackSun Jan 15th, 2009 10:08 pm
Re: Simple Question??
 
use this Empleados.numeroemp += 1;


All times are GMT -4. The time now is 3:23 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC