Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old February 27th, 2013, 03:36 PM
Registered User
Points: 23, Level: 1
Points: 23, Level: 1 Points: 23, Level: 1 Points: 23, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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?
  #2 (permalink)  
Old February 27th, 2013, 06:47 PM
Rod Stephens's Avatar
Wrox Author
Points: 3,166, Level: 23
Points: 3,166, Level: 23 Points: 3,166, Level: 23 Points: 3,166, Level: 23
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2006
Location: , , .
Posts: 647
Thanks: 2
Thanked 96 Times in 95 Posts
Default

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

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)
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 11:02 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.