Scott M. said:
Cor,
You are completely missing the point. My point is that writing
CStr(someString) over someObject.ToString is a throwback to the VB 6.0
style of object BASED programming and not consistent with the object
ORIENTED style of programming.
I hope you're teaching your students about encapsulation. Because that's
exactly what CStr is.
Also, the fact that by-design the .NET String object violates the strict
"object ORIENTED" notion that you seem to be harping on. For instance,
s.ToUpper() doesn't exactly perform a true OO operation (in that s isn't
modified). So there goes that.
In this context, yes there is a difference between a method and a language
function. As a teacher, when I have students new to this, the question is
always the same, "How do I know when you should use an object method vs.
using a language function and pass it some type's value?". Type CStr() in
VS.NET and it will turn blue. Blue is for language elements. Yes,
internally this "function" operates as a method, but from a coding *style*
perspective the developer is not using a method, they are using a language
construct.
Also known as a keyword, right? Well, that's what makes VB "VB."
Respectfully, if you don't like the things that make VB "VB," maybe you
shouldn't be using or teaching VB. C# seems much better suited to your
preferences, no?
For consistency sake as well as the fact that many of the old VB 6.0
"functions" don't offer any benefits over object methods, I prefer the OO
way.
They offer many benefits. Maybe it might help if somebody wrapped that all
up as Shared Methods in a "StringUtility" class? Does
StringUtility.CStr(...) make it easier for you to use? Would that make it
more "OO" to you?
As for CStr() in particular, I've said over and over that this is my
PREFERENCE and IMHO it doesn't need to be everyone's choice. Using
ToString on string values along with an Is Nothing check works in every
single situation. CStr() would need an IF Then check of its own to
validate its conversion, so the two approached amount to roughly the same
amount of code.
The latter accomplishes encapsulation. The former (your method) accomplishes
needlessly verbose code.
Now, CMM and Herfried have changed their original stance and admitted that
if you need to process Nothing differently than a string, you will have
problems with CStr().
I admitted nothing that isn't already obvious nor changed my stance. If you
need to treat Nothing differently than EmptyString then it's obvious you
wouldn't use CStr. Instead, it would behoove you to write your own helper
function.... like, SafeGetString(object, valueIfNothing) As String...
instead of littering If Is Nothing conditionals all over your code.
Again encapsulation is a good practice.
And in *my* travels, handling Nothing differently than a string value is
pretty darn common, which means using ToString makes perfect sense in
every situation I encounter.
In *my* travels, you usually want to treat Nothing as EmptyString. The
instance where this isn't true is rare and specialized. Indeed, the VB
language stresses this in that its Equality operators reinforce this notion.
I've alluded to this in another post in this thread... but, possibly VB is
not the language for you.
For advocating this, CMM and Herfried have decided to get personal and
call me stupid. I don't know CMM, but I wouldn't have expected that from
Herfried.
No one called you stupid... as a personal attack. If anything I said
offended you, I apologize.