Simple Question??

Please support our C# advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2008
Posts: 27
Reputation: gouki2005 is an unknown quantity at this point 
Solved Threads: 0
gouki2005 gouki2005 is offline Offline
Light Poster

Simple Question??

 
0
  #1
Jan 15th, 2009
  1. namespace Empleados
  2. {
  3. class Empleados
  4. {
  5. private int edad;
  6.  
  7. static private int numeroemp = 0;
  8.  
  9.  
  10.  
  11.  
  12. public Empleados(int edad)
  13. {
  14.  
  15. this.edad = edad;
  16. Empleados.numeroemp ++;
  17.  
  18.  
  19.  
  20. }
  21.  
  22. public void mostrarInfo()
  23. {
  24. Console.WriteLine("el empleado tiene {0} años",edad );
  25. Console.WriteLine("Numero de empleados creados {0} ", Empleados.obtenernumeEmpledos() );
  26. }
  27.  
  28. static void Main(string[] args)
  29. {
  30.  
  31. int edad = 46;
  32.  
  33. Empleados emp1 = new Empleados (edad);
  34. emp1.mostrarInfo();
  35. Empleados emp2 = new Empleados(45);
  36. emp2.mostrarInfo();
  37.  
  38.  
  39.  
  40.  
  41. }
  42.  
  43. public static int obtenernumeEmpledos()
  44. {
  45.  
  46. return numeroemp;
  47. }
  48. }
  49. }

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

  1. public Empleados(int edad)
  2. {
  3.  
  4. this.edad = edad;
  5. Empleados.numeroemp ++;
  6.  
  7.  
  8.  
  9. }
for this

  1. public Empleados(int edad)
  2. {
  3.  
  4. this.edad = edad;
  5. Empleados.numeroemp = + 1;
  6.  
  7.  
  8.  
  9. }

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??
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 249
Reputation: Antenka has a spectacular aura about Antenka has a spectacular aura about Antenka has a spectacular aura about 
Solved Threads: 65
Antenka's Avatar
Antenka Antenka is offline Offline
Posting Whiz in Training

Re: Simple Question??

 
0
  #2
Jan 15th, 2009
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.
  1. Empleados.numeroemp++;
increasing numeroemp by 1.
2.
  1. Empleados.numeroemp += 1;
which is the short version of this:
  1. Empleados.numeroemp = Empleados.numeroemp + 1;
3.
  1. Empleados.numeroemp = + 1;
here you assign numeroemp by +1.
So what if you can see the darkest side of me?
No one would ever change this animal I have become
Help me believe it's not the real me
Somebody help me tame this animal
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 276
Reputation: rapture has a spectacular aura about rapture has a spectacular aura about 
Solved Threads: 37
rapture rapture is offline Offline
Posting Whiz in Training

Re: Simple Question??

 
0
  #3
Jan 15th, 2009
I was going to add that the two outputs are the same . ..
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 1,735
Reputation: LizR has a spectacular aura about LizR has a spectacular aura about 
Solved Threads: 186
LizR LizR is offline Offline
Posting Virtuoso

Re: Simple Question??

 
0
  #4
Jan 15th, 2009
The difference is the

= +1
is the same as =1
where as

+= 1

will add one and store it.

Typos FTW
Did I just hear "You gotta help us, Doc. We've tried nothin' and we're all out of ideas" ? Is this you? Dont let this be you! I will put in as much effort as you seem to.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 46
Reputation: BlackSun is an unknown quantity at this point 
Solved Threads: 4
BlackSun BlackSun is offline Offline
Light Poster

Re: Simple Question??

 
0
  #5
Jan 15th, 2009
use this Empleados.numeroemp += 1;
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C# Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC