CStr() vs. .ToString()

H

Herfried K. Wagner [MVP]

Scott M. said:
The VB 6.0 way are not methods, they are functions. The .NET way are
object methods. The VB.NET compiler does NOT optimize the VB 6.0
functions to work BETTER than the natvie .NET object methods.

Does this really matter? The JIT compiler could inline the type conversion
functions.
To answer your question, ToString would be my suggestion, rather than
CStr(). ToString is a method of the Object Type, and since all classes
inherit from Object, all objects have this method.

'ToString' and 'CStr' serve different purposes. 'ToString' won't work on
'Nothing' references, which is a downside /if/ a 'Nothing' reference should
be converted to an empty string. That's exactly where 'CStr' can be used.
 
G

Guest

Thanks CMM, what you wrote is what I thought was true and I was looking for a
comfirmation.

As far as Scott goes I was refering to a "method" using the meaning of the
word "a way to". For my purposes it couldnt matter less if its a function or
a method.
 
S

Scott M.

The VB 6.0 way are not methods, they are functions. The .NET way are
Does this really matter? The JIT compiler could inline the type
conversion functions.

I was merely pointing out the inacurracy of the previous post. I'll leave
it up to the reader to determine if they care about the details.
'ToString' and 'CStr' serve different purposes. 'ToString' won't work on
'Nothing' references, which is a downside /if/ a 'Nothing' reference
should be converted to an empty string. That's exactly where 'CStr' can
be used.

As stated by others already, good programming practice would dictate that
data should be validated before being converted. So, I suppose I could say,
"Does it really matter since any good programmer would be checking for
nothing before converting the data?".
 
S

Scott M.

Because CStr() wouldn't throw an exception if it encountered a Nothing
value, which could cause unexpected bugs in the application. ToString would
throw an exception and thus make it easier to determine the problem.
 
S

Scott M.

To add to this, you can't use DirectCast on anything but Reference Types
anyway, so CType will need to be used for all numeric casting anyway.
 
S

Scott M.

Then you should absolutely go with your preference, which is what I said in
the first place. I merely indicated what my preference was and why.

Good luck!
 
H

Herfried K. Wagner [MVP]

Scott M. said:
Because CStr() wouldn't throw an exception if it encountered a Nothing
value, which could cause unexpected bugs in the application. ToString
would throw an exception and thus make it easier to determine the problem.

Well, but there are cases where I'd call 'CStr' intentionally because I want
a 'Nothing' reference to be converted to an empty string!
 
S

Scott M.

I hear what you are saying, but as I said in the previous post, using CStr()
wouldn't allow you to discriminate between those Nothing values that you
intended to run across and Nothing values that inadvertantly may happen. As
others have said, a simple If statement that checks for the Nothing value
would allow you to get a Nothing value and convert it to an empty string
while still maintaining the integrety of the code.
 
H

Herfried K. Wagner [MVP]

Scott M. said:
I hear what you are saying, but as I said in the previous post, using
CStr() wouldn't allow you to discriminate between those Nothing values that
you intended to run across and Nothing values that inadvertantly may
happen. As others have said, a simple If statement that checks for the
Nothing value would allow you to get a Nothing value and convert it to an
empty string while still maintaining the integrety of the code.

'CStr' is different from 'ToString'. It's obvious that it doesn't behave
like 'ToString', and the additional behavior is publically well-known. So
using 'CStr' *iff* it makes sense does not break the integrity of the code.
It's the developer's fault if it breaks integrity.
 
C

CMM

data should be validated before being converted. So, I suppose I could
say, "Does it really matter since any good programmer would be checking
for nothing before converting the data?".

They are doing it! Except they're using CStr to do it! It "checks" for the
programmer. Unless you actually need to do something ELSE based on whether a
string is Nothing, it's astronomically stupid to do what you say. I mean,
come on....

If s is Nothing Then
Return ""
Else
Return s.ToString()
End If

Why? That's exactly what CStr does.
 
B

Bob Lehmann

Would you make the same argument for CInt?

Say you wanted to exclude unanswered questions on a survey where you will
average a 0 to 4 rating.
0 <> Nothing.

Bob Lehmann
 
S

Scott M.

Well, thanks for calling me stupid. If you would calm down and READ my
other posts, I have addressed why CStr() is actually more dangerous to use
than ToString.

Try to just understand what I'm saying for a moment...If you rely on CStr()
to do your validation checks, then it will allow all Nothing values and
would not be able to alert YOU the programmer to a validation error unless
you add an if statement of your own that checks for "".

Your example code of what I'd write is inaccurate given that if s Is
Nothing, I wouldn't want and empty string in its place, I'd probably WANT an
exception.

If you just want to rant to hear yourself, fine - - but you seem like you
are being rather close minded to the benefits of structuring your code in a
more object oriented way. You also seem to believe that saving a few
keystrokes of code is always better than not adding a few more important
lines of code.

Let me ask you something, what would you write if you didn't want to proceed
with your string data when the user hasn't entered any data? I imagine the
code would look very similar to what you wrote below as an example of what
you'd need to write with ToString. And given that the 2 solutions are so
similar, why not choose the more OO way of writing it?
 
H

Herfried K. Wagner [MVP]

Scott M. said:
Try to just understand what I'm saying for a moment...If you rely on
CStr() to do your validation checks, then it will allow all Nothing values
and would not be able to alert YOU the programmer to a validation error
unless you add an if statement of your own that checks for "".

Your example code of what I'd write is inaccurate given that if s Is
Nothing, I wouldn't want and empty string in its place, I'd probably WANT
an exception.

Huh?! You are mixing up cases. CMM and I simply said that generally
recommending not to use 'CStr' at all is plain stupid. If you /want/ an
exception to be thrown, simply do not use 'CStr'!
 
S

Scott M.

So using 'CStr' *iff* it makes sense does not break the integrity of the
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Well, yeah! Yiou seem to now agree with me that using CStr() doesn't
*always* make sense. That's all I've been trying to say...CStr() can get
you into trouble in certain situations. And when it does, it can be
difficult to troubleshoot where the problem is due to the *valid*, but empty
string floating around in memory. ToString can get you into trouble as
well, but it's a whole lot easier to see where the problem is in the
ToString scenario. And if you want to avoid the empty string possibility of
CStr(), you are going to need an IF statement to check the string, just as
you would need to check the string for Nothing using ToString, so there's no
code savings that others have indicated there would be.

As for the additional behavior of CStr() being well known, I highly
disagree. This whole thread (and countless others) started off with the OP
confused over the difference between CStr() and .ToString.
 
H

Herfried K. Wagner [MVP]

Scott M. said:
Well, yeah! Yiou seem to now agree with me that using CStr() doesn't
*always* make sense. That's all I've been trying to say...CStr() can get
you into trouble in certain situations.

Well, I'm glad we agree on this point too :).
As for the additional behavior of CStr() being well known, I highly
disagree. This whole thread (and countless others) started off with the
OP confused over the difference between CStr() and .ToString.

I think this problem always arises if people are using tools they do not
fully understand.
 
B

Bob Lehmann

think this problem always arises if people are using tools they do not
fully understand.

Which could be avoided if best practices were followed to begin with, and
not fueled by bad advice.

Bob Lehmann
 
M

m.posseth

Yes and then the question is who defines the best practices and guidelines
???

cause past week i showed some examples from the best practices and
guidelines book i have ( oficial MS press book written by the core
reference writer )
and you should have seen the thread :)

so question is ........ who defines this then ,,,,, every sole developer for
himself apperently ,,,, so in the end we stay with the same coding mess


regards

M. Posseth [MCP]
 
C

Cor Ligthert [MVP]

Herfried,
Huh?! You are mixing up cases. CMM and I simply said that generally
recommending not to use 'CStr' at all is plain stupid.

Good catch,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top