[dba-VB] C#: this.prefix

Shamil Salakhetdinov shamil at smsconsulting.spb.ru
Wed Jul 23 14:06:11 CDT 2008


Hi Gustav,

That's waste of HDD space for sure IMO. Probably somebody "paranoid" and
overcautious with VB6/VBA programming experience background did write this
code generator at MS. I'm serious :)

FYI: I almost never use 

this.

in my C# coding - I do use camelCase naming conventions and it works very
well for me - never had any confusion with scope, as well as my
VS2005/VS2008 C# compiler :)

Here is the case when this. *is* necessary - but this (pun intended:)) is a
bad coding style:

    public class ThatParent
    {
        public string Name = "Parent Name";
    }

    public class ThisChild : ThatParent
    {
        public new string Name = "Child Name";

        public void Test()
        {
            Console.WriteLine("Parent:" + base.Name);
            Console.WriteLine("Child:" + this.Name);
            Console.WriteLine("Whose name is this ?:" + Name); 
        }
    }


They (MS) do generate this. everywhere to prevent confusions like that one
above as controls' names could be similar to the names of the properties and
methods in WinForm class inheritance hierarchy:

System..::.Object
  System..::.MarshalByRefObject
    System.ComponentModel..::.Component
      System.Windows.Forms..::.Control
        System.Windows.Forms..::.ScrollableControl
          System.Windows.Forms..::.ContainerControl
            System.Windows.Forms..::.Form


Thank you.

--
Shamil


-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Wednesday, July 23, 2008 7:40 PM
To: dba-vb at databaseadvisors.com
Subject: Re: [dba-VB] C#: this.prefix

Hi Shamil et al

Sorry to return to this perhaps pedantic question, but I noticed that the
auto generated code in the .Designer.cs files are 100% flooded with either
the this. prefix or the full namespace.

Why would the generator do this if it was not necessary? Is it just being
overcautious?

/gustav


>>> shamil at smsconsulting.spb.ru 02-07-2008 18:57 >>>
Hi Gustav,

I almost never use such prefixing - and I did use Me. a lot in VBA...

I do use full namespace prefix if needed.

Using this. prefix could disturb a lot as for C# programming is usual to
make a lot of code refactorings with moving code snippets from here to there
etc.

I do use CamelCase postfix naming convention - and it helps and works well
for me (Imagine that after almost ten years of using LNRC (and Hungarian)
naming conventions in VBA and VB6!)...

--
Shamil

-----Original Message-----
From: dba-vb-bounces at databaseadvisors.com 
[mailto:dba-vb-bounces at databaseadvisors.com] On Behalf Of Gustav Brock
Sent: Wednesday, July 02, 2008 7:55 PM
To: dba-vb at databaseadvisors.com 
Subject: [dba-VB] C#: this.prefix

Hi all

How rigorously are you prefixing controls and variables with "this."?

Is it a good or bad coding practice to use it when not strictly needed?
Do I understand it right, that it is only needed to separate, say, a method
in the current namespace from an identically named and public method in
another namespace?

I've found, that I use it a lot - old habit from "Me." in VBA, I guess.

/gustav



_______________________________________________
dba-VB mailing list
dba-VB at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/dba-vb
http://www.databaseadvisors.com




More information about the dba-VB mailing list