Results 1 to 3 of 3

Thread: Constructors

  1. #1

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Constructors

    Can someone please give me an overview of the use of constructors in .net.

    I am currently trying to improve my oop and am a bit baffled by this subject.

    Parksie

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Constructors

    A constructor is just a method that initialises an instance. In C# a constructor has the same name as the class itself, e.g.
    Code:
    public class SomeClass
    {
        // This is a constructor.
        public SomeClass()
        {
            // Initialise the properties of the new instance here.
        }
    }
    Whenever you create an instance of a class using the "new" keyword you are implicitly calling a constructor, e.g.
    Code:
    SomeClass instance = new SomeClass();
    will call the constructor above. When you create an instance using the "new" keyword you can pass arguments if and only if there is a constructor with the same signature, e.g. you could only do this:
    Code:
    SomeClass instance = new SomeClass(someString, someInteger);
    if the SomeClass class has a constructor that takes those arguments, i.e.
    Code:
    public class SomeClass
    {
        // This is a constructor.
        public SomeClass(string arg1, int arg2)
        {
            // Initialise the properties of the new instance here.
        }
    }
    Constructors can be overloaded, which allows you to create instances using different combinations of arguments, e.g. this
    Code:
    public class SomeClass
    {
        // This is a constructor.
        public SomeClass()
        {
            // Initialise the properties of the new instance here.
        }
    
        // This is another constructor.
        public SomeClass(string arg1, int arg2)
        {
            // Initialise the properties of the new instance here.
        }
    }
    would allow you to do this
    Code:
    SomeClass instance1 = new SomeClass();
    SomeClass instance2 = new SomeClass(someString, someInteger);
    Note that constructors are used to initialise the new instance, i.e. set its properties. If there are no properties to set then you can leave the constructor body empty.

    Further reading: http://msdn2.microsoft.com/en-us/library/ace5hbzh.aspx
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Fanatic Member venerable bede's Avatar
    Join Date
    Sep 2002
    Location
    The mystic land of Geordies
    Posts
    1,018

    Resolved Re: Constructors

    Thanks, that was concise and just what I was looking for.

    Parksie

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width