Why use anything besides Ctype to convert datatypes?

G

Guest

Hello
Everything in .Net is an object and CTYPE converts one datatype to another
datatype so why use Cstr, Cint or any other method to convert from one
datatype to another?
 
A

Armin Zingler

winlin said:
Hello
Everything in .Net is an object and CTYPE converts one datatype to
another datatype so why use Cstr, Cint or any other method to
convert from one datatype to another?

Because it's shorter. "CInt(s)" is shorter than "CType(s, Integer)"


Armin
 
H

Heikki Leivo

Hello
Everything in .Net is an object and CTYPE converts one datatype to another
datatype so why use Cstr, Cint or any other method to convert from one
datatype to another?

For backwards compatibility or for those who prefer to write in vb6 style.

-h-
 
G

Guest

winlin said:
Hello
Everything in .Net is an object and CTYPE converts one datatype to another
datatype so why use Cstr, Cint or any other method to convert from one
datatype to another?

For when you want to do one kind of conversion, but not allow another.
There are several different ways of converting values, and they all fit
in different situations.

CType(stringValue, Integer) will for example convert a null string into
zero, but that is not always desirable. Sometimes you want a null value
to be considered illegal, and not silently converted.

Another example is casting of a known type. The DirectCast method is a
slightly faster, and it will only allow casting a reference into it's
actual type.
 
A

Armin Zingler

Heikki Leivo said:
For backwards compatibility or for those who prefer to write in vb6
style.

Why is using the shorter, straight version "vb6 style"? These are all
commands, specialized on the destination type, that are even compiled
inline.


Armin
 
G

Guest

CType(x, Integer) is identical to CInt.
They will both convert a null string to zero.

In general, CType has identical behavior to the shorter CInt, CLng, etc.
macros (they aren't functions, but part of VB language syntax).
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
 
G

Guest

Absolutely.
CType, CInt, CLng, etc. are all a legit part of VB syntax and are not
related to any legacy libraries.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
 
G

Guest

David said:
CType(x, Integer) is identical to CInt.
They will both convert a null string to zero.

Yes, of course. Other methods won't, like Int32.Parse for example.
 

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