Wrox Programmer Forums
BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer
This is the forum to discuss the Wrox book Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer by Rod Stephens; ISBN: 9780470596906
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old February 27th, 2013, 03:36 PM
Registered User
Join Date: Feb 2013
Posts: 5
Thanks: 2
Thanked 0 Times in 0 Posts
Default Lesson 23 Ex. 1 (Complex Numbers)

I was reviewing this example for a little bit until the hint finally clicked. I opened the books code to help understand what was happening. The methods make sense, however something else caught my eye.

ComplexNumber class's methods, AddTo for example

public ComplexNumber AddTo(ComplexNumber c1)
ComplexNumber result = new ComplexNumber();
result.Real = this.Real + c1.Real;
result.Imaginary = this.Imaginary + c1.Imaginary;
return result;

I dont really understand the "this" keyword very well. and in this example I removed all the "this" keywords and it still worked perfectly. IE remove this.Real and turn it to just Real.

From what I do understand of (this), it represents the current object in the code that is executing. So this would usually just represent a class, and represent all of its fields or parameters. But I'm confused on why it would be used in this example?
Old February 27th, 2013, 06:47 PM
Rod Stephens's Avatar
Wrox Author
Join Date: Jan 2006
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts

You're exactly right. The value "this" is the object whose code is currently executing.

If you omit "this," then by default the program looks at the current object's properties to resolve unresolved references. In this case, if you say just "Real," the program looks around for a variable named Real but doesn't find one locally so it looks at the current object's properties and finds one named Real.

Sometimes I (and others) use "this" to emphasize the fact that the code is working with the current object as opposed to some other object or a local variable. In this case I did it to make it clear that there are two objects involved: this and c1.

But it's purely stylistic. You don't need to use "this" if you don't want to.

Rod Stephens, Microsoft MVP

Essential Algorithms: A Practical Approach to Computer Algorithms

(Please post reviews at Amazon or wherever you shop!)
The Following User Says Thank You to Rod Stephens For This Useful Post:
Mech_1000 (February 27th, 2013)

Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 23 C# code Crimson_Reefer BOOK: Knight's 24-Hour Trainer: Microsoft SQL Server 2008 Integration Services 0 April 18th, 2012 08:21 PM
Errata Lesson 23 pg.284 RMADANAT BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 February 2nd, 2012 03:16 PM
Ex 23-3 zavodney BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 2 September 18th, 2011 11:22 PM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.