473,799 Members | 3,029 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CStr() vs. .ToString()

Book I am reading says that Cstr() is best method for efficency and safety
however it doesnt compare that method of the .ToString() method.

Which is best.

Thanks
Feb 18 '06 #1
101 19252
IMHO (and this has been debated for quite some time now), you should view
all of the old "data-type" specific functions as legacy functions and no
longer use them. Instead, use the more object-oriented methods of a type.

CStr, CBool, CDbl, CInt, etc. would all be replaced with CType or .ToString
All date/time functions would be replaced with methods and properties of the
Date class
All string functions would be replaced with methods and properties of the
String class.

et all.
"Sean" <Se**@discussio ns.microsoft.co m> wrote in message
news:68******** *************** ***********@mic rosoft.com...
Book I am reading says that Cstr() is best method for efficency and safety
however it doesnt compare that method of the .ToString() method.

Which is best.

Thanks

Feb 18 '06 #2
guy
both points of view are valid, however i use the vb string methods as the
compiler makes optimzations that can improve performance over the native .Net
methods, also i find the vb methods read better.

cheers

guy

"Scott M." wrote:
IMHO (and this has been debated for quite some time now), you should view
all of the old "data-type" specific functions as legacy functions and no
longer use them. Instead, use the more object-oriented methods of a type.

CStr, CBool, CDbl, CInt, etc. would all be replaced with CType or .ToString
All date/time functions would be replaced with methods and properties of the
Date class
All string functions would be replaced with methods and properties of the
String class.

et all.
"Sean" <Se**@discussio ns.microsoft.co m> wrote in message
news:68******** *************** ***********@mic rosoft.com...
Book I am reading says that Cstr() is best method for efficency and safety
however it doesnt compare that method of the .ToString() method.

Which is best.

Thanks


Feb 18 '06 #3
for my clarification
..ToString() = native .Net methods
CStr() = vb String methods?

from my understanding in the book I am reading Cstr() etc is the perfered
way however it doesnt address .ToString()

"guy" wrote:
both points of view are valid, however i use the vb string methods as the
compiler makes optimzations that can improve performance over the native .Net
methods, also i find the vb methods read better.

cheers

guy

"Scott M." wrote:
IMHO (and this has been debated for quite some time now), you should view
all of the old "data-type" specific functions as legacy functions and no
longer use them. Instead, use the more object-oriented methods of a type.

CStr, CBool, CDbl, CInt, etc. would all be replaced with CType or .ToString
All date/time functions would be replaced with methods and properties of the
Date class
All string functions would be replaced with methods and properties of the
String class.

et all.
"Sean" <Se**@discussio ns.microsoft.co m> wrote in message
news:68******** *************** ***********@mic rosoft.com...
Book I am reading says that Cstr() is best method for efficency and safety
however it doesnt compare that method of the .ToString() method.

Which is best.

Thanks


Feb 18 '06 #4

"guy" <gu*@discussion s.microsoft.com > wrote in message
news:21******** *************** ***********@mic rosoft.com...
both points of view are valid, however i use the vb string methods as the
compiler makes optimzations that can improve performance over the native
.Net
methods, also i find the vb methods read better.
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.

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.


cheers

guy

"Scott M." wrote:
IMHO (and this has been debated for quite some time now), you should view
all of the old "data-type" specific functions as legacy functions and no
longer use them. Instead, use the more object-oriented methods of a
type.

CStr, CBool, CDbl, CInt, etc. would all be replaced with CType or
.ToString
All date/time functions would be replaced with methods and properties of
the
Date class
All string functions would be replaced with methods and properties of the
String class.

et all.
"Sean" <Se**@discussio ns.microsoft.co m> wrote in message
news:68******** *************** ***********@mic rosoft.com...
> Book I am reading says that Cstr() is best method for efficency and
> safety
> however it doesnt compare that method of the .ToString() method.
>
> Which is best.
>
> Thanks
>
>


Feb 18 '06 #5
CMM
I don't think that's true at all. First off, CStr,CBool, Etc. are all just
easier-to-read specialized wrappers around CType..... which in and of itself
is a special "VB" construct. The true "non-VB" casting operator is
DirectCast(...) .

Contrary to what Scott M says you're SUPPOSED to use them. What's the point
of using VB if you're not going to use its special methods that make your
life easier? This is straight from the VB documentation:

"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."

In addition, although I'm a guy who always initializes strings and objects
as soon as possible rather than have them sit around until my algorithm uses
them- so I don't really care about the following- but, there's an advantage
to using VB's functions: they interpret "Nothing." You can't use the OO
methods on a string that is "Nothing" (s.ToUpper for instance won't work).

--
-C. Moya
www.cmoya.com
"Scott M." <s-***@nospam.nosp am> wrote in message
news:O7******** ******@TK2MSFTN GP14.phx.gbl...
IMHO (and this has been debated for quite some time now), you should view
all of the old "data-type" specific functions as legacy functions and no
longer use them. Instead, use the more object-oriented methods of a type.

CStr, CBool, CDbl, CInt, etc. would all be replaced with CType or
.ToString
All date/time functions would be replaced with methods and properties of
the Date class
All string functions would be replaced with methods and properties of the
String class.

et all.
"Sean" <Se**@discussio ns.microsoft.co m> wrote in message
news:68******** *************** ***********@mic rosoft.com...
Book I am reading says that Cstr() is best method for efficency and
safety
however it doesnt compare that method of the .ToString() method.

Which is best.

Thanks


Feb 18 '06 #6
>> You can't use the OO methods on a string that is "Nothing"

Why would you want to?

Oh, I get it, you're too lazy to check the data before performing an
operation it. Isn't checking your data a 100 level concept?

I'm guessing that On Error Resume Next is one of your friends too.

Bob Lehmann

"CMM" <cm*@nospam.com > wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
I don't think that's true at all. First off, CStr,CBool, Etc. are all just
easier-to-read specialized wrappers around CType..... which in and of itself is a special "VB" construct. The true "non-VB" casting operator is
DirectCast(...) .

Contrary to what Scott M says you're SUPPOSED to use them. What's the point of using VB if you're not going to use its special methods that make your
life easier? This is straight from the VB documentation:

"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."

In addition, although I'm a guy who always initializes strings and objects
as soon as possible rather than have them sit around until my algorithm uses them- so I don't really care about the following- but, there's an advantage to using VB's functions: they interpret "Nothing." You can't use the OO
methods on a string that is "Nothing" (s.ToUpper for instance won't work).

--
-C. Moya
www.cmoya.com
"Scott M." <s-***@nospam.nosp am> wrote in message
news:O7******** ******@TK2MSFTN GP14.phx.gbl...
IMHO (and this has been debated for quite some time now), you should view all of the old "data-type" specific functions as legacy functions and no
longer use them. Instead, use the more object-oriented methods of a type.
CStr, CBool, CDbl, CInt, etc. would all be replaced with CType or
.ToString
All date/time functions would be replaced with methods and properties of
the Date class
All string functions would be replaced with methods and properties of the String class.

et all.
"Sean" <Se**@discussio ns.microsoft.co m> wrote in message
news:68******** *************** ***********@mic rosoft.com...
Book I am reading says that Cstr() is best method for efficency and
safety
however it doesnt compare that method of the .ToString() method.

Which is best.

Thanks



Feb 18 '06 #7
Wow, what happened to put you in this mood?
Checking if a variable actually contains data before doing a convert to
upper case is simply too much code. If you can avoid that it makes the code
a lot better readable.
"Bob Lehmann" <no****@dontbot herme.zzz> wrote in message
news:ep******** ******@TK2MSFTN GP12.phx.gbl...
You can't use the OO methods on a string that is "Nothing"
Why would you want to?

Oh, I get it, you're too lazy to check the data before performing an
operation it. Isn't checking your data a 100 level concept?

I'm guessing that On Error Resume Next is one of your friends too.

Bob Lehmann

"CMM" <cm*@nospam.com > wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
I don't think that's true at all. First off, CStr,CBool, Etc. are all
just
easier-to-read specialized wrappers around CType..... which in and of

itself
is a special "VB" construct. The true "non-VB" casting operator is
DirectCast(...) .

Contrary to what Scott M says you're SUPPOSED to use them. What's the

point
of using VB if you're not going to use its special methods that make your
life easier? This is straight from the VB documentation:

"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."

In addition, although I'm a guy who always initializes strings and
objects
as soon as possible rather than have them sit around until my algorithm

uses
them- so I don't really care about the following- but, there's an

advantage
to using VB's functions: they interpret "Nothing." You can't use the OO
methods on a string that is "Nothing" (s.ToUpper for instance won't
work).

--
-C. Moya
www.cmoya.com
"Scott M." <s-***@nospam.nosp am> wrote in message
news:O7******** ******@TK2MSFTN GP14.phx.gbl...
> IMHO (and this has been debated for quite some time now), you should

view > all of the old "data-type" specific functions as legacy functions and
> no
> longer use them. Instead, use the more object-oriented methods of a type. >
> CStr, CBool, CDbl, CInt, etc. would all be replaced with CType or
> .ToString
> All date/time functions would be replaced with methods and properties
> of
> the Date class
> All string functions would be replaced with methods and properties of the > String class.
>
> et all.
>
>
> "Sean" <Se**@discussio ns.microsoft.co m> wrote in message
> news:68******** *************** ***********@mic rosoft.com...
>> Book I am reading says that Cstr() is best method for efficency and
>> safety
>> however it doesnt compare that method of the .ToString() method.
>>
>> Which is best.
>>
>> Thanks
>>
>>
>
>



Feb 18 '06 #8
Give that man a Kewpie doll!
"Bob Lehmann" <no****@dontbot herme.zzz> wrote in message
news:ep******** ******@TK2MSFTN GP12.phx.gbl...
You can't use the OO methods on a string that is "Nothing"
Why would you want to?

Oh, I get it, you're too lazy to check the data before performing an
operation it. Isn't checking your data a 100 level concept?

I'm guessing that On Error Resume Next is one of your friends too.

Bob Lehmann

"CMM" <cm*@nospam.com > wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
I don't think that's true at all. First off, CStr,CBool, Etc. are all
just
easier-to-read specialized wrappers around CType..... which in and of

itself
is a special "VB" construct. The true "non-VB" casting operator is
DirectCast(...) .

Contrary to what Scott M says you're SUPPOSED to use them. What's the

point
of using VB if you're not going to use its special methods that make your
life easier? This is straight from the VB documentation:

"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."

In addition, although I'm a guy who always initializes strings and
objects
as soon as possible rather than have them sit around until my algorithm

uses
them- so I don't really care about the following- but, there's an

advantage
to using VB's functions: they interpret "Nothing." You can't use the OO
methods on a string that is "Nothing" (s.ToUpper for instance won't
work).

--
-C. Moya
www.cmoya.com
"Scott M." <s-***@nospam.nosp am> wrote in message
news:O7******** ******@TK2MSFTN GP14.phx.gbl...
> IMHO (and this has been debated for quite some time now), you should

view > all of the old "data-type" specific functions as legacy functions and
> no
> longer use them. Instead, use the more object-oriented methods of a type. >
> CStr, CBool, CDbl, CInt, etc. would all be replaced with CType or
> .ToString
> All date/time functions would be replaced with methods and properties
> of
> the Date class
> All string functions would be replaced with methods and properties of the > String class.
>
> et all.
>
>
> "Sean" <Se**@discussio ns.microsoft.co m> wrote in message
> news:68******** *************** ***********@mic rosoft.com...
>> Book I am reading says that Cstr() is best method for efficency and
>> safety
>> however it doesnt compare that method of the .ToString() method.
>>
>> Which is best.
>>
>> Thanks
>>
>>
>
>



Feb 18 '06 #9
>> Checking if a variable actually contains data before doing a convert ..
is simply too much code.
You aren't serious, are you?
If you can avoid that it makes the code a lot better readable. You mean like the sentence above? I'm sure you meant "a lot more better
readablest".

Happy coding, pal.

Bob Lehmann

"Martin" <x@y.com> wrote in message
news:uu******** ******@TK2MSFTN GP11.phx.gbl...
Wow, what happened to put you in this mood?
Checking if a variable actually contains data before doing a convert to
upper case is simply too much code. If you can avoid that it makes the code a lot better readable.
"Bob Lehmann" <no****@dontbot herme.zzz> wrote in message
news:ep******** ******@TK2MSFTN GP12.phx.gbl... You can't use the OO methods on a string that is "Nothing"


Why would you want to?

Oh, I get it, you're too lazy to check the data before performing an
operation it. Isn't checking your data a 100 level concept?

I'm guessing that On Error Resume Next is one of your friends too.

Bob Lehmann

"CMM" <cm*@nospam.com > wrote in message
news:%2******** **********@TK2M SFTNGP09.phx.gb l...
I don't think that's true at all. First off, CStr,CBool, Etc. are all
just
easier-to-read specialized wrappers around CType..... which in and of

itself
is a special "VB" construct. The true "non-VB" casting operator is
DirectCast(...) .

Contrary to what Scott M says you're SUPPOSED to use them. What's the

point
of using VB if you're not going to use its special methods that make your life easier? This is straight from the VB documentation:

"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."

In addition, although I'm a guy who always initializes strings and
objects
as soon as possible rather than have them sit around until my algorithm

uses
them- so I don't really care about the following- but, there's an

advantage
to using VB's functions: they interpret "Nothing." You can't use the OO
methods on a string that is "Nothing" (s.ToUpper for instance won't
work).

--
-C. Moya
www.cmoya.com
"Scott M." <s-***@nospam.nosp am> wrote in message
news:O7******** ******@TK2MSFTN GP14.phx.gbl...
> IMHO (and this has been debated for quite some time now), you should

view
> all of the old "data-type" specific functions as legacy functions and
> no
> longer use them. Instead, use the more object-oriented methods of a

type.
>
> CStr, CBool, CDbl, CInt, etc. would all be replaced with CType or
> .ToString
> All date/time functions would be replaced with methods and properties
> of
> the Date class
> All string functions would be replaced with methods and properties of

the
> String class.
>
> et all.
>
>
> "Sean" <Se**@discussio ns.microsoft.co m> wrote in message
> news:68******** *************** ***********@mic rosoft.com...
>> Book I am reading says that Cstr() is best method for efficency and
>> safety
>> however it doesnt compare that method of the .ToString() method.
>>
>> Which is best.
>>
>> Thanks
>>
>>
>
>



Feb 19 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
7709
by: Bill L. | last post by:
We recently noticed that the vb.net CStr function yields different results than the vb6 version when converting SQL decimal data. If, for example, the data is defined in SQL as decimal(19,10), the vb.net CStr function will return ten digits to the right of the decimal, regardless of the data value. In this case, if the data value is 5, the vb.net CStr function will return 5.0000000000. The vb6 CStr function will simply return 5. In other...
5
10474
by: c_shah | last post by:
Very beginner question.. What's difference between cstr vs .ToString vs Ctype for converting to String?
7
2769
by: John | last post by:
Hi I have a WinForm app with a bound form. When user enters a value in field rateid I lookup the respective rate amount from a table and assign it to field rate.I am using the DLookup function to achieve this (full code given below). This function returns a value of type object so I need to convert it to string. If I use the function as below (using ToString) it works fine; Me.txtRate.Text = DLookup(<parameters here>).ToString
0
9685
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10249
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9068
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5461
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5584
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4138
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2937
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.