944,005 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Unsolved
  • Views: 603
  • C# RSS
Jun 15th, 2009
0

Constructors

Expand Post »
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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fdelriog is offline Offline
3 posts
since Jun 2009
Jun 15th, 2009
2

Re: Constructors

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).
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jun 15th, 2009
0

Re: Constructors

Here is some great text on the matter:

http://en.wikipedia.org/wiki/Constru...puter_science)
Reputation Points: 442
Solved Threads: 89
Master Poster
Diamonddrake is offline Offline
721 posts
since Mar 2008
Jun 15th, 2009
0

Re: Constructors

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.
C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 3
Solved Threads: 14
Junior Poster
thewebhostingdi is offline Offline
168 posts
since Jun 2009
Jun 16th, 2009
0

Re: Constructors

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fdelriog is offline Offline
3 posts
since Jun 2009
Jun 16th, 2009
0

Re: Constructors

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fdelriog is offline Offline
3 posts
since Jun 2009
Jun 16th, 2009
0

Re: Constructors

"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:
c# Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Reading Large Files - Memory Management
Next Thread in C# Forum Timeline: Problem with pause DOS command





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC