Constructors

Please support our C# advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 3
Reputation: fdelriog is an unknown quantity at this point 
Solved Threads: 0
fdelriog fdelriog is offline Offline
Newbie Poster

Constructors

 
0
  #1
Jun 15th, 2009
Hi,

I don´t understand what are constructors and what are they used for. Can someone explain me in simple words or with an easy example what is a constructor and why are they neccesary?

Thx
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,680
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 727
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Constructors

 
2
  #2
Jun 15th, 2009
When you turn on your computer, it does a bunch of stuff, right? POST, loading the OS, logging you in, starting up automatic programs, etc... Constructors have a similar purpose in that they put your objects into an initial ready state. Achieving that state would be cumbersome at best (because you have to set every data member) and impossible at worst (if you have private data in the class).
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 329
Reputation: Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough Diamonddrake is a jewel in the rough 
Solved Threads: 39
Diamonddrake's Avatar
Diamonddrake Diamonddrake is offline Offline
Posting Whiz

Re: Constructors

 
0
  #3
Jun 15th, 2009
Here is some great text on the matter:

http://en.wikipedia.org/wiki/Constru...puter_science)
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 124
Reputation: thewebhostingdi has a little shameless behaviour in the past 
Solved Threads: 11
thewebhostingdi thewebhostingdi is offline Offline
Junior Poster

Re: Constructors

 
0
  #4
Jun 15th, 2009
Constructors are used to initialize the objects once they are created. Constructors are always public and do not have any return types. you can overload the constructors according to the number and types of arguments specified. Here is the basic example. This sets count variable to integer 1.
  1. public class sampleclass
  2. {
  3. private int count;
  4. sampleclass(int count)
  5. {
  6. this.count = count;
  7. }
  8. }
  9.  
  10. public static void Main()
  11. {
  12. sampleclass c = new sampleclass(1);
  13. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: fdelriog is an unknown quantity at this point 
Solved Threads: 0
fdelriog fdelriog is offline Offline
Newbie Poster

Re: Constructors

 
0
  #5
Jun 16th, 2009
Thx all for the help. I think i understand better now. Still i have some other questions:
i) Why would it be helpful to have more than one constructor in a class?
ii) What is the "this" keyword used for?

Thx
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 3
Reputation: fdelriog is an unknown quantity at this point 
Solved Threads: 0
fdelriog fdelriog is offline Offline
Newbie Poster

Re: Constructors

 
0
  #6
Jun 16th, 2009
Thx all for the help. I think i understand better now. Still i have some other questions:
i) Why would it be helpful to have more than one constructor in a class?
ii) What is the "this" keyword used for?

Thx
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,219
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 574
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast

Re: Constructors

 
0
  #7
Jun 16th, 2009
"this" refers to "this instance" of a class. You can use "this." to refer to properties and methods for the current class to help narrow down the intellisense menus as well as distinguishing between this.Method() and base.Method() if you are overriding a method from a base class. Here is an example of multiple constructors:
  1. private IPCamera()
  2. {
  3. bf = new Blackfin();
  4. }
  5. public IPCamera(string IP)
  6. : this(IP, DefaultBlackfinPort)
  7. {
  8. }
  9. public IPCamera(string IP, int Port)
  10. : this(new IPEndPoint(IPAddress.Parse(IP), Port))
  11. {
  12. }
  13. public IPCamera(EndPoint EndPoint)
  14. : this()
  15. {
  16. if (EndPoint == null)
  17. throw new ArgumentNullException("EndPoint");
  18.  
  19. IPEndPoint ipEndPt = (EndPoint as IPEndPoint);
  20. if (ipEndPt != null)
  21. {
  22. this.CameraIP = ipEndPt.ToString();
  23. this.CameraPort = ipEndPt.Port;
  24. }
  25. else
  26. throw new ArgumentException(string.Format("Unknown EndPoint {0}", EndPoint.GetType().ToString()), "EndPoint");
  27. socket = new Socket(EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
  28. endPoint = EndPoint;
  29. }

This is useful because it gives you more ways to instantiate a class, in this case it defaults a port but all constructors lead to a base constructor where blackfin is initialized. In the next few days I will be adding more constructors where you can pass an already opened socket. This allows you to open a connection and create an instance of the class, or pass an IP and have it create one for you.
Last edited by sknake; Jun 16th, 2009 at 4:19 pm.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC