: this ......... what does it mean??
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: : this ......... what does it mean??

Hybrid View

  1. #1
    Join Date
    Jun 2010
    Posts
    2

    : this ......... what does it mean??

    Hello guys,

    I was reading a book about csharp called
    "Pro C# 2010 and .NET 4 Platform"

    I came across a piece of code that i dont understand. the book doesnt explain it and i cant continue knowing there is a piece of info that i didnt understand.

    I would really appreciate if someone could help me understand the code on the line number 19



    1> public enum PointColor
    2> { LightBlue, BloodRed, Gold }
    3> class Point
    4> {
    5> public int X { get; set; }
    6> public int Y { get; set; }
    7> public PointColor Color{ get; set; }
    8> public Point(int xVal, int yVal)
    9> {
    10> X = xVal;
    11> Y = yVal;
    12> Color = PointColor.Gold;
    13> }
    14> public Point(PointColor ptColor)
    15> {
    16> Color = ptColor;
    17> }
    18> public Point()
    19> : this(PointColor.BloodRed){ } <<<<<<<<<<<<<< I need to understand this line
    20> public void DisplayStats()
    21> {
    22> Console.WriteLine("[{0}, {1}]", X, Y);
    23> Console.WriteLine("Point is {0}", Color);

    24> }
    25> }

    thanks guys.

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: : this ......... what does it mean??

    It means that the constructor on line 14 is invoked with that parameter

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: : this ......... what does it mean??

    That is called constructor chaining.

    Assume you have two constructors, one which takes and argument and one which does not. Your simple constructor (the one that does not take an argument) can just call the complex constructor and provide a default value. This allows you to keep all of your constructor logic in one place instead of duplicating code.

    Code:
    class MyClass
    {
        private Rectangle _rect;
    
        public MyClass() : this( new Rectangle(0, 0, 10 10 ) ) { }  // calls MyClass(Rectangle rect)
    
        public MyClass( Rectangle rect )
        {
            _rect = rect;
        }
    }
    Instead of...


    Code:
    class MyClass
    {
        private Rectangle _rect;
    
        public MyClass()
        {
            _rect = new Rectangle(0, 0, 10 10 );
        }
    
        public MyClass( Rectangle rect )
        {
            _rect = rect;
        }
    }
    I know that may seem trivial, but a meaningful constructor will do more than that and chaining them keeps your code clean and maintainable.

  4. #4
    Join Date
    Jan 2010
    Posts
    1,099

    Re: : this ......... what does it mean??

    Similarly, if your class derives from another class, you can call the base class' constructor using the base keyword, in exactly the same fashion.
    Code:
    public ADerivedClass() 
        : base( /* ...whatever... */ ) 
    { 
       // Additional stuff, if required... 
    }

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: : this ......... what does it mean??

    and, adding to that

    paramterless base class constructors are called implicitly, so no need to worry about those.

Tags for this Thread

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center