C# equivalent for VB Class DEFAULT method

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mortimer Schnurd

    C# equivalent for VB Class DEFAULT method

    In VB, I could assign a "Default" to a class method. In that way, a
    reference to the Class always returns the result of that
    method/property (i.e. a reference to a textbox control always returns
    the contents of textbox.text).
    What is the C# equivalent? Is it the Indexer for that class?

    --
    John Wood
  • Nicholas Paldino [.NET/C# MVP]

    #2
    Re: C# equivalent for VB Class DEFAULT method

    Mortimer,

    The indexer is the only thing on a class that can be called by default.
    Personally, I think that it is very dangerous to have such a thing (not
    indexers, but default properties), because it creates ambiguities in the
    code. One of my biggest pet peeves was ADO, when using a recordset, you
    could do:

    ' pobjRs is an ADODB.Recordset
    pobjRs("fieldNa me")

    Where the above is shorthand for:

    pobjRs.Fields.I tem("fieldName" ).Value

    The latter piece of code gives a much better idea of what the intent is,
    IMO.

    Hope this helps.

    --
    - Nicholas Paldino [.NET/C# MVP]
    - mvp@spam.guard. caspershouse.co m

    "Mortimer Schnurd" <fugetaboutit@h otsmail.com> wrote in message
    news:dg8o60tnvu m5qmkn7nmtfq53g gnnvqvr1l@4ax.c om...[color=blue]
    > In VB, I could assign a "Default" to a class method. In that way, a
    > reference to the Class always returns the result of that
    > method/property (i.e. a reference to a textbox control always returns
    > the contents of textbox.text).
    > What is the C# equivalent? Is it the Indexer for that class?
    >
    > --
    > John Wood[/color]


    Comment

    • Mortimer Schnurd

      #3
      Re: C# equivalent for VB Class DEFAULT method

      On Thu, 1 Apr 2004 09:19:00 -0500, in msg
      <uC9TBS$FEHA.26 64@TK2MSFTNGP11 .phx.gbl>, "Nicholas Paldino [.NET/C#
      MVP]" <mvp@spam.guard .caspershouse.c om> wrote:
      [color=blue]
      >Mortimer,
      >
      > The indexer is the only thing on a class that can be called by default.
      >Personally, I think that it is very dangerous to have such a thing (not
      >indexers, but default properties), because it creates ambiguities in the
      >code. One of my biggest pet peeves was ADO, when using a recordset, you
      >could do:[/color]

      Thanks Nicholas, that does help. I do agree with your philosophy
      regarding defaults and I rarely ever use them in code, However,
      assigning a default in a VB class made it very easy for me to identify
      content while debugging and "hovering" over an object.

      --
      Regards,
      John Wood

      --
      John Wood a.k.a Mortimer Schnurd

      Comment

      • Hans Kesting

        #4
        Re: C# equivalent for VB Class DEFAULT method


        "Mortimer Schnurd" <fugetaboutit@h otsmail.com> wrote in message
        news:21bo60l5q1 lt4ft0cb3s62p1r 6drg92ntr@4ax.c om...[color=blue]
        > On Thu, 1 Apr 2004 09:19:00 -0500, in msg
        > <uC9TBS$FEHA.26 64@TK2MSFTNGP11 .phx.gbl>, "Nicholas Paldino [.NET/C#
        > MVP]" <mvp@spam.guard .caspershouse.c om> wrote:
        >[color=green]
        > >Mortimer,
        > >
        > > The indexer is the only thing on a class that can be called by[/color][/color]
        default.[color=blue][color=green]
        > >Personally, I think that it is very dangerous to have such a thing (not
        > >indexers, but default properties), because it creates ambiguities in the
        > >code. One of my biggest pet peeves was ADO, when using a recordset, you
        > >could do:[/color]
        >
        > Thanks Nicholas, that does help. I do agree with your philosophy
        > regarding defaults and I rarely ever use them in code, However,
        > assigning a default in a VB class made it very easy for me to identify
        > content while debugging and "hovering" over an object.
        >
        > --
        > Regards,
        > John Wood
        >
        > --
        > John Wood a.k.a Mortimer Schnurd
        > http://www.loosemarbles.com[/color]


        Would an override of the ToString() method help here?

        Hans Kesting


        Comment

        Working...