Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

When I started VB.NET a year and so ago, I was straight from College and did not know VB. While searching for stuff at the internet I used to use backward compatible functions such as Cint and other functions without knowing that they are VB 6 functions that are still there to support backward compatibility. Now I also know that it is not good to call these functions and we should try to avoid them as much as possible.

 

I read in a book that DirectCast is much faster than CType and it is recommended to use.

 

A question is Ctype a backward comatible function, was it present in VB 6 ??

 

Thx in advance

Dream as if you'll live forever, live as if you'll die today
  • Administrators
Posted

Ctype and DirectCast are slightly different in how they operate.

Ctype is a generic conversion function and can replace the VB6 CInt, CDbl etc. However under .Net you are better off using the newer methods :

 

CInt can be replaced with Integer.Parse(), CDbl with Double.Parse, CStr with .ToString() etc.

 

DirectCast will not convert a variable of one datatype to another datatype, only allow you to assign the variable to a DataType that is the same type (or interface, or in the same inheritance hierarchy).

 

i.e.

In a button click event the first parameter passed in is declared as object - but would really contain the button control that was clicked.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim b As Button
b = DirectCast(sender, Button)

 

would assign the button to the variable b, we could then use b and it's methods / properties in a type safe way. This would work because the variable sender is really a button.

 

However

Dim i As Integer = 45
Dim s As String
s = DirectCast(i, String)    	'would not work
s= CType(i, String)		'would work
s=i.ToString()			'would work and is the .Net way

 

in this case i is not a string DirectCast wouldn't work.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Thx for the answer, I get your point.

 

However I would really like to add that, CInt() and its sisters form VB6 take an object as an argument where as Integer.Parse and it sisters in .NET take a string as an argument, which adds an extra step (change the argument to string if it is not)

 

i.e.

 

'using Cint to change a string to an integer
Dim s as string = "2"
Dim i as integer = Cint(s)

'using Integer.Parse
Dim s as string = "2"
Dim i as integer = Integer.Parse(s)

'pretty much the same. However if the variable isnt a string, ex Single or any type

Dim s as Single = 2.3
Dim i as integer = Cint(s) 'works fine , I know it would yield to its ok

'using Integer.Parse
'this adds an extra step....
Dim s as Single = 2.3
Dim i as integer = Integer.Parse(s.ToString())

 

In this case, I agree we shouldnot use Cint(), but arent we better in terms of speed with using CType()

 

One more thing, we need to mention here that in case we are SURE that the casting wont cause an exception then using DirectCast is better because it is faster than Ctype() because it DirectCast can deliver better performance because it never calls methods in the Microsoft.VisualBasic DLL.

 

One final thing I understand that you placed your example to clarify stuff but you cannot in the first place use DirectCast() from integer to any value since DirectCast can only take reference types as parameters not value types.

However

Dim i As Integer = 45
Dim s As String
s = DirectCast(i, String)    	'would not work
s= CType(i, String)		'would work
s=i.ToString()			'would work and is the .Net way

[/Quote]

Dream as if you'll live forever, live as if you'll die today
  • Administrators
Posted

The other problem with CType is it is VB.Net only - if you need to port your code to another .Net language then you would have to use the .Net methods anyway.

 

Even though the .Parse methods only take a string rather than an object the CInt etc. will still require some conversion behind the scenes , it's just that the 'guess work' on how the conversion is performed is hidden from us.

You could use the .Net Convert class though to acheive this result.

 

dim i as integer
Dim sin As Single = 100.45
i = Convert.ToInt32(sin)

 

 

By using the .Parse methods at least we can provide hints through .the overloaded versions in regard to formatting the string may contain (thousands separator, currency symbol etc.) giving us some control over the process.

 

Finally you are correct about DirectCast requiring reference types, should really have tested my sample code ;)

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...