473,388 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

What is "this" in object constructors?

mrjohn
32
Hullo, I've been learning C# and I had a question. I'm creating a class, and I was writing the constructor. When I was assigning values to it's variables, I found that the compiler seems to take it both with and without the "this" keyword. Does it make a difference whether I use it or not?

Expand|Select|Wrap|Line Numbers
  1. public class Car
  2.     {
  3.         private float weight, gasTank;
  4.         public Car(float myWeight, float myGasTank)
  5.         {
  6.             this.weight = myWeight;
  7.             this.gasTank = myGasTank;
  8.         }
May 21 '09 #1
3 1314
tlhintoq
3,525 Expert 2GB
Nope. No difference. this. is merely specifying the scope.

In theory you could have a "weight" in one class, and a "weight" in another class. You would want to be clear about which one you mean.

class1.weight
class2.weight

In this case when the search starts to find a 'weight' the only one is the one in .this
May 21 '09 #2
mrjohn
32
Cool beans. Thanks.
May 21 '09 #3
artov
40
It is quite common to use same name for object's instances and parameters in constructors, like

Expand|Select|Wrap|Line Numbers
  1. public class Appointment
  2. {
  3.    float duration;
  4.    string name;
  5.    DateTime time;
  6.  
  7.    public Appointment(string name, DateTime time, float duration)
  8.    {
  9.       this.name = name;
  10.       this.time = time;
  11.       this.duration = duration;
  12.    }
  13. ..
  14. }
This make the code easier to read and if you use environment like Visual Studio or SharpDevelop, it is easier to select the right constructor, if there are several.
May 22 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mike Kamermans | last post by:
I'm trying to determine where inside an XHTML's DOM a javascript function is triggered, but when I use the construction of calling a function with "this" as parameter and then checking the event...
4
by: Armel Asselin | last post by:
Hello, I'm working on a Javascript interpreter; when I execute this code I cannot figure out why the inner "c" function returns the input widget as "this"... could someone tell ?? <input...
10
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
9
by: aden | last post by:
I have read the years-old threads on this topic, but I wanted to confirm what they suggest. . . Can the this pointer EVER point to a type different from the class that contains the member...
3
by: Oliver Saunders | last post by:
can someone please tell me why everytime i try and pass this to a js function i can't seem to use it for anything: <html> <head> <script language="javascript"> function a(fromlink) {...
9
by: Player | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hello all. I am in the process of teaching myself C# and I think I am doing OK. I have learnt how to how to call the right constructor of a...
6
by: Marty | last post by:
Hi, I have a class that I modified to be static. It is now a public sealed class and all function are static, no more constructor but a init() function to do the constructor job. This class...
2
by: danny.dion | last post by:
Hi ! I have a question about JScript : I have an object class wich dynamically creates a control in the page. Then it binds an event to that control, pointing on one of its methods (the...
2
by: Bryan | last post by:
Hello all, Can anyone explain when one should use the "document" object and when one should use the "this" object? Also, is the "self" object the same as the "document" or "this" object?
2
by: bednarz.thomas | last post by:
I have the following business Object(s): public class ParentObject { public ParentObject(string somestring) { ...- } ....
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.