| | |
Constructors
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
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.
•
•
Join Date: Jun 2009
Posts: 124
Reputation:
Solved Threads: 11
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)
public class sampleclass { private int count; sampleclass(int count) { this.count = count; } } public static void Main() { sampleclass c = new sampleclass(1); }
"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:
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.
c# Syntax (Toggle Plain Text)
private IPCamera() { bf = new Blackfin(); } public IPCamera(string IP) : this(IP, DefaultBlackfinPort) { } public IPCamera(string IP, int Port) : this(new IPEndPoint(IPAddress.Parse(IP), Port)) { } public IPCamera(EndPoint EndPoint) : this() { if (EndPoint == null) throw new ArgumentNullException("EndPoint"); IPEndPoint ipEndPt = (EndPoint as IPEndPoint); if (ipEndPt != null) { this.CameraIP = ipEndPt.ToString(); this.CameraPort = ipEndPt.Port; } else throw new ArgumentException(string.Format("Unknown EndPoint {0}", EndPoint.GetType().ToString()), "EndPoint"); socket = new Socket(EndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp); endPoint = EndPoint; }
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.
![]() |
Similar Threads
- Why constructors don't have return types (C++)
- Taking address of constructors?? (C++)
- C++ Tic Tac Toe using classes & operator overloading (C++)
- Confusion with constructors (Java)
- what is the effect of using constructors in the program? (Java)
- How to do constructors (Java)
Other Threads in the C# Forum
- Previous Thread: Open another form
- Next Thread: Problem with pause DOS command
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# check checkbox client color combobox control conversion csharp custom data database datagrid datagridview dataset datetime degrees development draganddrop drawing encryption enum event excel file finalyearproject form format forms function gdi+ getoutlookcontactusinfcsvfile globalization httpwebrequest image index input install installer java label list listbox mandelbrot math mono mouseclick mysql operator panel path photoshop picturebox pixelinversion post programming radians regex remote remoting richtextbox serialization server silverlight sleep socket sql sql-server statistics stream string table text textbox thread time timer timespan update upload usercontrol users validate validation visualstudio webbrowser windows winforms wpf xml






