CStr vs .toString which is best to use

G

Guest

Using VB2005 is there any reason to use one of these functions over the
other? Are there circumstances in which the toString method must be used?

What is the difference between the two functions?
 
G

ge0193387

Using VB2005 is there any reason to use one of these functions over the
other? Are there circumstances in which the toString method must be used?

What is the difference between the two functions?

I always use ToString since CStr is a VB exclusive. Makes it slightly
easier for people coming from other languages to read since ToString
is a .NET method. As far as I know you'll get the same errors with
CStr as with ToString, if the object is Null it will crash
(DataReaders are my issue).

I do the same thing with Integers, doubles, and all the others.
Instead of using CInt or CDbl I use Convert.ToInteger or
Convert.ToDouble.
 
A

Armin Zingler

Darrell Wesley said:
Using VB2005 is there any reason to use one of these functions over
the other? Are there circumstances in which the toString method must
be used?

What is the difference between the two functions?

CStr is a keyword, whereas ToString is a function (method). CStr is compiled
inline and it creates code depending on the type of the passed object. It's
mainly there for people being used to it from previous VB versions. I
haven't used CStr in .Net anymore (because it's not obvious what it does in
which situations and it's also not very well documented).

The difference depends on which ToString function you use. Every type can
have it's own implementation.


Armin
 
G

Guest

I can see that you would have to use the ".to" functions for other languages
but in VB I wonder what the difference is. I read someplace that the VB
functions give more predictable results.
 
R

rowe_newsgroups

but in VB I wonder what the difference is.

When's Microsoft releasing the "Shared Source Code" for the .Net
framework? That would be the easiest way to see the internal
implementation.

Until then, you can use the MSIL disassembler or download Reflector to
see what the keyword does. IIRC it's simply a mapped call to one of
the methods in the Microsoft.VisualBasic namespace. I seem to remember
discussing this with Cor and Herfried a long time ago (only with
CInt), but can't find the post now...

Thanks,

Seth Rowe
 
G

Guest

Well the particular place I'm looking at is with a datareader with option
strict on and you need to convert the type Object to some defined data type.
In this case it's just casting a string to a string.

Option Strict On
Dim a as String
dim drResult as DataReader
a = cstr(drResult("a"))

a = drResult("a").toString
 
R

rowe_newsgroups

Well the particular place I'm looking at is with a datareader with option
strict on and you need to convert the type Object to some defined data type.
In this case it's just casting a string to a string.

Option Strict On
Dim a as String
dim drResult as DataReader
a = cstr(drResult("a"))

a = drResult("a").toString

I always liked using dr.GetValue(0).ToString(), which returns "" for a
dbNull value. The only bad thing is you lose the ability to reference
the column by name (which I never use anyway).

Thanks,

Seth Rowe
 
A

Armin Zingler

Darrell Wesley said:
Well the particular place I'm looking at is with a datareader with
option strict on and you need to convert the type Object to some
defined data type. In this case it's just casting a string to a
string.

Option Strict On
Dim a as String
dim drResult as DataReader
a = cstr(drResult("a"))

a = drResult("a").toString


If the data type of the field is String, use .ToString. It's the fastest way
because it just returns a pointer to itself without any conversion (and it's
listed by intellisense if you type ".").

If you would fill the data into a typed dataset, the field property is
already typed As String.


Armin
 
B

Bob Altman

I have read that you should use the VB "CStr" function rather than directly
calling ToString. (I thought I had read this in my venerable "Programming
Visual Basic" book by Francesco Balena, but I can't find the quote right
now.) The reason was that the VB compiler is smart enough to perform more
efficient string conversions for some types (for example, it's probably
smart enough to optimize away CStr(<string type>).
 
T

Tom Shelton

Bob Altman said:
I have read that you should use the VB "CStr" function rather than directly
calling ToString. (I thought I had read this in my venerable "Programming
Visual Basic" book by Francesco Balena, but I can't find the quote right
now.) The reason was that the VB compiler is smart enough to perform more
efficient string conversions for some types (for example, it's probably
smart enough to optimize away CStr(<string type>).

Using Reflector to look at identical VB.NET and C# code, I would say that in
MOST cases the use of CStr is less effiecient. The only time I can see an
advantage of using CStr is in the case that the value is already a string,
in that one case the VB.NET compiler optimizes away the call to .ToString.
In all other cases, it makes a call to a overload of Conversions.ToString,
which then calls the value.ToString method. So, essentially in every other
case, you get two function calls instead of one.
 
D

DK

Using Reflector to look at identical VB.NET and C# code, I would say that in
MOST cases the use of CStr is less effiecient. The only time I can see an
advantage of using CStr is in the case that the value is already a string,
in that one case the VB.NET compiler optimizes away the call to .ToString.
In all other cases, it makes a call to a overload of Conversions.ToString,
which then calls the value.ToString method. So, essentially in every other
case, you get two function calls instead of one.

Here is what the HELP section says for Type Conversion Functions (part
of the VB2005 help system)

As a rule, you should use the Visual Basic type conversion functions
in preference to the .NET Framework methods such as ToString(), either
on the Convert class or on an individual type structure or class. The
Visual Basic functions are designed for optimal interaction with
Visual Basic code, and they also make your source code shorter and
easier to read. In addition, the .NET Framework conversion methods do
not always produce the same results as the Visual Basic functions, for
example when converting Boolean to Integer. For more information, see
Troubleshooting Data Types.
 
T

Tom Shelton

Here is what the HELP section says for Type Conversion Functions (part
of the VB2005 help system)

As a rule, you should use the Visual Basic type conversion functions
in preference to the .NET Framework methods such as ToString(), either
on the Convert class or on an individual type structure or class. The
Visual Basic functions are designed for optimal interaction with
Visual Basic code, and they also make your source code shorter and
easier to read. In addition, the .NET Framework conversion methods do
not always produce the same results as the Visual Basic functions, for
example when converting Boolean to Integer. For more information, see
Troubleshooting Data Types.- Hide quoted text -

- Show quoted text -

They are basically made to work the exact same way as the originals.
But CStr just calls the objects .ToString method anyway via a call to
the visual basic runtimes Conversions class.
 

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