Proper programming semantics?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon Slaughter

    Proper programming semantics?

    When programming, say, a control should I use the objects of the control
    directly, reference them from this, or use, if possible, the ones passed
    through event arguments?

    e.g., in a paint event I can use this. or just access the fields and methods
    of the control directly or use the painteventargs argument passed. Say I
    want to draw a line on the control.

    What I'm a little confused about is when to use "this.". I know its mainly
    used for qualification on the current instance of the object but is there
    any other reason to use it? And is there any reason to use the event args
    when one can use the current instance directly?

    Thanks,
    Jon


  • Scott M.

    #2
    Re: Proper programming semantics?

    Using "this" or not results in the exact same result, so it doesn't matter.

    Some folks like to use "this" because when you type the "." after "this",
    you'll get intellSense helping you remember what the rest of your class's
    members are and you'll actually wind up doing less typing with less chances
    for a mistakenly referenced class member. Of course, there are other
    shortcuts you can take to remind yourself of the names of your class members
    besides using "this" (like beginning the member name and typing CTRL +
    Space), so some people don't use "this".

    The only time you must use "this" is when the class needs to refer to
    instances of itself. Other than that, it's up to you which to use.


    "Jon Slaughter" <Jon_Slaughter@ Hotmail.comwrot e in message
    news:xj63h.4262 $9v5.2529@newss vr29.news.prodi gy.net...
    When programming, say, a control should I use the objects of the control
    directly, reference them from this, or use, if possible, the ones passed
    through event arguments?
    >
    e.g., in a paint event I can use this. or just access the fields and
    methods of the control directly or use the painteventargs argument passed.
    Say I want to draw a line on the control.
    >
    What I'm a little confused about is when to use "this.". I know its mainly
    used for qualification on the current instance of the object but is there
    any other reason to use it? And is there any reason to use the event args
    when one can use the current instance directly?
    >
    Thanks,
    Jon
    >

    Comment

    • Bruce Wood

      #3
      Re: Proper programming semantics?


      Jon Slaughter wrote:
      When programming, say, a control should I use the objects of the control
      directly, reference them from this, or use, if possible, the ones passed
      through event arguments?
      >
      e.g., in a paint event I can use this. or just access the fields and methods
      of the control directly or use the painteventargs argument passed. Say I
      want to draw a line on the control.
      >
      What I'm a little confused about is when to use "this.". I know its mainly
      used for qualification on the current instance of the object but is there
      any other reason to use it? And is there any reason to use the event args
      when one can use the current instance directly?
      As to whether to use the event argument (usually the first argument,
      sender) or just reference the control directly, it doesn't matter
      unless you have several controls' events routed to the same event
      handler.

      Some people, for example, prefer to reduce the number of event handler
      methods by combining them where they do the same thing, and using the
      "sender" argument to decide upon which control to act.

      Comment

      • Dave Sexton

        #4
        Re: Proper programming semantics?

        Hi Jon,

        Its common uses are best explained here:

        "this (C# Reference)"


        --
        Dave Sexton

        "Jon Slaughter" <Jon_Slaughter@ Hotmail.comwrot e in message
        news:xj63h.4262 $9v5.2529@newss vr29.news.prodi gy.net...
        When programming, say, a control should I use the objects of the control
        directly, reference them from this, or use, if possible, the ones passed
        through event arguments?
        >
        e.g., in a paint event I can use this. or just access the fields and methods
        of the control directly or use the painteventargs argument passed. Say I
        want to draw a line on the control.
        >
        What I'm a little confused about is when to use "this.". I know its mainly
        used for qualification on the current instance of the object but is there
        any other reason to use it? And is there any reason to use the event args
        when one can use the current instance directly?
        >
        Thanks,
        Jon
        >

        Comment

        • Jon Skeet [C# MVP]

          #5
          Re: Proper programming semantics?

          Scott M. <NoSpam@NoSpam. comwrote:
          Using "this" or not results in the exact same result, so it doesn't matter.
          >
          Some folks like to use "this" because when you type the "." after "this",
          you'll get intellSense helping you remember what the rest of your class's
          members are and you'll actually wind up doing less typing with less chances
          for a mistakenly referenced class member. Of course, there are other
          shortcuts you can take to remind yourself of the names of your class members
          besides using "this" (like beginning the member name and typing CTRL +
          Space), so some people don't use "this".
          >
          The only time you must use "this" is when the class needs to refer to
          instances of itself. Other than that, it's up to you which to use.
          And also if you have a local variable available with the same name as
          an instance variable. This can be common in constructors:

          public class Person
          {
          string name;
          int age;

          public Person (string name, int age)
          {
          this.name = name;
          this.age = age;
          }
          }

          Some naming conventions prevent this, but others don't.

          --
          Jon Skeet - <skeet@pobox.co m>
          http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
          If replying to the group, please do not mail me too

          Comment

          Working...