Dennis van der Stelt

The most votes generally drown out the best votes

Community

News

  • Meet me at PDC08

Email Notifications

I read...

I Use...

Tags

Recent Posts

Archives

June 2006 - Posts

Copy constructors

Download source in attachment at the bottom.

A while ago I heard the term “copy constructor” for the first time from a C++ developer. A copy constructor is a constructor which takes a (single) parameter of an existing object of the same type as the constructor’s class. It’s used to create an deep copy of the passed object, instead of creating a shallow copy. With a shallow copy, you’d create a reference to the values of the original object, as show in the following code:

Customer cust1 = new Customer();

Customer cust2;

cust2 = cust1;

cust1.FirstName = "Dennis";

cust1.LastName = "van der Stelt";

Console.WriteLine(cust2.FullName);

// Outputs : Dennis van der Stelt

As you can see, we tell cust2 to be like cust1, which is what it exactly does. This is basic behavior everyone should be aware of. Even when you pass a reference type (like our Customer class) to a method by value, you’ll still only get the reference to the object.

But let’s have a look at our Customer class. A few details are worth mentioning. First the default constructor, without any parameters. I’ve included it because when we’re done, we still want to be able to create a customer object without passing anything. The second detail we’ll discuss is the BirthDate property. It immediately sets a private field with the current age of the customer. Probably better would be to calculate ones age when we retrieve this value, but for the sake of the demo we want it done this way. Last detail to look at is the FirstName property. When we request the first name, we’ll get it capitalized.

public class Customer

{

  private string _firstName;

  private string _lastName;

  private DateTime _birthDate;

  private int _age;

 

  public Customer() { }

 

  public string FirstName

  {

    get

    {

      string value = _firstName.Substring(0, 1).ToUpper() + _firstName.Substring(1, _firstName.Length - 1).ToLower();

      value.Trim();

      return value;

    }

    set { _firstName = value; }

  }

 

  public string LastName

  {

    get { return _lastName; }

    set { _lastName = value; }

  }

 

  public DateTime BirthDate

  {

    get { return _birthDate; }

    set

    {

      _birthDate = value;

      _age = (DateTime.Now.Year - _birthDate.Year) - (_birthDate.AddYears(DateTime.Now.Year - _birthDate.Year) > DateTime.Now ? 1 : 0);

    }

  }

 

  public string FullName

  {

    get { return FirstName + " " + LastName; }

  }

 

  public int Age

  {

    get { return _age; }

  }

}

Imagine you’d want a copy of this customer. You can take care of this in some other class and copy all values. But you’ll get the first name of your customer capitalized, and that might not be what you want or need. And shouldn’t this functionality be in the Customer class itself? For this we create a copy constructor, as shown in the next example:

/// <summary>

/// Copy constructor, creates a deep copy of passed object.

/// </summary>

/// <param name="customer"></param>

public Customer(Customer customer)

{

  this._firstName = customer._firstName;

  this._lastName = customer._lastName;

  this.BirthDate = customer.BirthDate;

}

As you can see, the new constructor takes one parameter of the same type as its own class. When analyzing this constructor, we see a deep copy of the firstname and lastname fields, but not for the birthdate field.

As said, we probably want a deep copy of the firstname, so we won’t get our original firstname capitalized. This is possible, because every class can see the private members of any (instantiated) class of the same type, which is default OO behavior. So our constructor can actually see the private fields _firstName and _lastName of the passed class.However with the birthdate, we’d want to set its value through the BirthDate property. That way, the age is immediately set, which would not happen had we set the private field _birthDate.

So now we can start using our copy constructor to copy our original customer, like this:

cust2 = new Customer(cust1);

cust2.FirstName = "Laura";

 

string msg = string.Format("{0} is married to {1}", cust1.FullName, cust2.FullName);

Console.WriteLine(msg);

So now you know what a copy constructor is and how to use it. Be careful with every copy, to select the right method, using either the fields or properties. There’s no single solution, as you can see in the above example. But if you’re not sure, it’s probably better to always use deep copies and use the private fields, instead of the properties.

Upgrade to Community Server 2.0 complete!

Wow, this is it. It happened! I can relax again! We upgraded!

Yes it's true, you're currently looking at Community Server 2.0

Well, I can't really relax, because I'm still testing to see if everything's working, upload some stuff, test some more, etc, etc. And if you see anything that's not working, just send me an e-mail.

Oh, and a mesasge for our webloggers. Most never ever read mail they receive from BloggingAbout.NET, so hopefully they'll read it here! :-)
There are now two ways to upload your files and pictures. As you can see in the picture below, there are some strange looking icons. I'm talking about the icon in the lower-right corner, and the exact same icon a bit to the left. The icon on the right is to insert images or files from either the Photos or Files galleries. The other one is to insert images from your personal directory.

FreeTextBoxUploads.png

So when to choose which method? Well just remember that nice photos go into the gallery, and little screenshot thingies like the one in this post, go into your personal images folder on disk. Same about files. It's just so the Photo gallery and file downloads will stay a bit clean!

Have fun!

BloggingAbout.NET Upgrading today [Update]

I'll be upgrading the website in an hour, because I really feel like it. Hope everything goes well!

Update 11:00 GMT+1
Still working on the update. I haven't changed much yet, but was fixing something for webloggers who want to upload pictures. Everything's still going great, just keep crossing those fingers!

CodePlex, the GotDotNet for TFS

CodePlex is a new website launched by Microsoft last week. I have a hard team keeping up with all the rss feeds (1000+ unread posts) so perhaps this was thrown all over the internet, I just didn't know! ;-)

Anyway, we all know GotDotNet is the open source repository for developers where they are able to host their projects and share their hard work with the world, and be able to track issues and discuss the work. CodePlex is about the same, but it runs entirely on Team Foundation Server. Currently there aren't that much projects online and its website is currently still blazing fast, compared to GotDotNet. We can only hope it stays this way.

The good news is that you can now download the TFS Team Explorer to be able to download and upload sources. If you've already got Team System installed, here's how to connect to CodePlex.

Via Vertigo

BloggingAbout.NET Upgrade Notification

It's finally going to happen, BloggingAbout.NET is upgrading to Community Server 2.0.

In the past we installed some functionality to be able to upload files and images, using a somewhat adjusted web based backend. It's a long story, but in the end we cannot just tear down this functionatlity because Community Server simply does not support this. It does support uploading files and images in version 2.0, but those are uploaded to the gallery. And as we want to keep the gallery somewhat clean, we're not really into upload all kinds of tiny screenshots. So that was the main problem, but we're happy to say we solved it now.

This will result in the near future in some downtime. We expect the upgrade to take up an hour or so. So if your RSS Reader reports an error or the site itself shows a lot of errors or no pages at all, don't start to panic. ;-)

Oh, by the way, because we seriously can't wait any longer to upgrade, we're upgrading to the default CS skin with the little green tree in the upper left corner. If anyone feels like creating a logo, don't hesitate to contact me and draw us something nice!