C# equivalent for VB Class DEFAULT method

  • Thread starter Thread starter Mortimer Schnurd
  • Start date Start date
M

Mortimer Schnurd

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?
 
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("fieldName")

Where the above is shorthand for:

pobjRs.Fields.Item("fieldName").Value

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

Hope this helps.
 
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:

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.
 
Mortimer Schnurd said:
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.


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

Hans Kesting
 
Back
Top