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

Recommended Answers

All 6 Replies

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).

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.

public class sampleclass
    {
        private int count;
        sampleclass(int count)
        {
            this.count = count;
        }
    }

public static void Main()
        {
            sampleclass c = new sampleclass(1);
        }

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

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

"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:

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.