J
Jedrzej Miadowicz
I recently (re)discovered data binding in Windows Forms thanks to its
advances in Visual Studio 2005. As I looked a little deeper, however, I
realize that it still suffers from an irksome tendency to stick a whole
bunch of literal strings in my code. Quite frankly, I consider them a
plague imposed on developers by the RAD designers that come with Visual
Studio. The problem with using literal strings to refer to controls, data
sources, objects, etc. and their properties is quite obvious: if any of them
are changed by hand in code, or if the programmer onadvertently removes a
letter from one of such magic strings no one will notice until the
application is run and this very piece of code gets executed most often with
unpleasant consequences (typically some type of an ugly exception).
The problem could be completely eliminated if we could use some construct
that could be verified by the compiler at compile time. In this case if a
typo occurred the project simply wouldn't build. Unfortunately, as things
stand right now there appears to be no way of doing this.
As I thought about it some more it occurred to me that the solution
shouldn't be too hard to provide. All that is needed is a new operator that
the compiler could evaluate in very much the same fashion that
typeof(ClassName) works. I would call this operator
nameof(ClassName.PropertyName) or perhaps nameof(objectName.PropertyName),
or something similar. It seems to me that the compiler has easy access to
this type of information, or else it wouldn't be able to build my code.
If such a construct existed I could rewrite this line:
new System.Windows.Forms.Binding("EditValue", someObject, "LastName", true)
to look like this
new System.Windows.Forms.Binding(nameof(myTextBox.Text), someObject,
nameof(someObject.LastName), true)
and be entirely compile-time verifiable.
This same concept could be used not only in data binding but in many other
contexts where the designers currently stick literal strings. It could also
be implemented differently to return not just the name of the property or
method of a given class, but actually return the full descriptors that could
be used for further "reflection".
I would like to here some comments about this. Is there a technical reason
why such a construct cannot be added to the language?
Andrew
advances in Visual Studio 2005. As I looked a little deeper, however, I
realize that it still suffers from an irksome tendency to stick a whole
bunch of literal strings in my code. Quite frankly, I consider them a
plague imposed on developers by the RAD designers that come with Visual
Studio. The problem with using literal strings to refer to controls, data
sources, objects, etc. and their properties is quite obvious: if any of them
are changed by hand in code, or if the programmer onadvertently removes a
letter from one of such magic strings no one will notice until the
application is run and this very piece of code gets executed most often with
unpleasant consequences (typically some type of an ugly exception).
The problem could be completely eliminated if we could use some construct
that could be verified by the compiler at compile time. In this case if a
typo occurred the project simply wouldn't build. Unfortunately, as things
stand right now there appears to be no way of doing this.
As I thought about it some more it occurred to me that the solution
shouldn't be too hard to provide. All that is needed is a new operator that
the compiler could evaluate in very much the same fashion that
typeof(ClassName) works. I would call this operator
nameof(ClassName.PropertyName) or perhaps nameof(objectName.PropertyName),
or something similar. It seems to me that the compiler has easy access to
this type of information, or else it wouldn't be able to build my code.
If such a construct existed I could rewrite this line:
new System.Windows.Forms.Binding("EditValue", someObject, "LastName", true)
to look like this
new System.Windows.Forms.Binding(nameof(myTextBox.Text), someObject,
nameof(someObject.LastName), true)
and be entirely compile-time verifiable.
This same concept could be used not only in data binding but in many other
contexts where the designers currently stick literal strings. It could also
be implemented differently to return not just the name of the property or
method of a given class, but actually return the full descriptors that could
be used for further "reflection".
I would like to here some comments about this. Is there a technical reason
why such a construct cannot be added to the language?
Andrew