Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB.NET
|
VB.NET General VB.NET discussions for issues that don't fall into other VB.NET forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 7th, 2003, 04:56 AM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default CType (When is it useful?)

Both codes below do the same thing when I press Button1. Anybody know if efficiency is a factor for using CType? Anybody ever coded using CType. Can you tell me where CType plays an important role in one's code? Obviously the code below is simple and I would prefer to use Code 1.

*** CODE 1 ***
        Dim objButton as Object
        objButton = Me.Button1
        objButton.text = "This is my button."

*** CODE 2 ***
        Dim objButton as Object
        objButton = Me.Button1 'button on the form
        CType(objButton, Button).Text = "This is my button"

Thanks.
 
Old October 7th, 2003, 08:09 AM
Authorized User
 
Join Date: Jun 2003
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi

Ctype is used to explicitly convert the object from one type to another type, this is mainly required when option strict is set to "ON", and can improve performance as the compiler has less to do. Another version of this is Direct Cast, this gives even better performance than CType.

HTH
Duncan

Duncan
 
Old October 7th, 2003, 10:17 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

LULU,

If you know that the variable "objButton" is going to be a Button, then you should dim it as such. This will eliminate the need for type conversion.

It's highly recommended that you code with Option Strict on. It greatly decreases errors in design-time by providing instant feedback on incorrect variable types as well as increasing performance. You should also always code with Option Explicit as well as that also increases performance. There are few occasions when you would need to code with either of these off.

Peter
 
Old October 7th, 2003, 10:19 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Duncan,

Can you explain "Direct Cast" a bit more? Are you referring to casting in C#? I have been working with .Net (99% VB) for over a year and I don't recall ever hearing that term. Perhaps I'm missing something vital.

Peter
 
Old October 7th, 2003, 10:37 AM
Authorized User
 
Join Date: Jun 2003
Posts: 63
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi peter

This is for VB.Net and I've pasted the blurb from MSDN

The DirectCast keyword introduces a type conversion operation. You use it the same way you use the CType keyword, as the following example shows:

Dim Q As Object = 2.37 ' Requires Option Strict to be Off.
Dim I As Integer = CType(Q, Integer) ' Succeeds.
Dim J As Integer = DirectCast(Q, Integer) ' Fails.
Both keywords take an expression to be converted as the first argument, and the type to convert it to as the second argument. Both conversions fail if there is no conversion defined between the data type of the expression and the data type specified as the second argument.

The difference between the two keywords is that CType succeeds as long as there is a valid conversion defined between the expression and the type, whereas DirectCast requires the run-time type of an object variable to be the same as the specified type. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType.

In the preceding example, the run-time type of Q is Double. CType succeeds because Double can be converted to Integer, but DirectCast fails because the run-time type of Q is not already Integer.

DirectCast throws an InvalidCastException error if the argument types do not match.

See Also
Implicit and Explicit Conversions | InvalidCastException Class



Duncan
 
Old October 7th, 2003, 10:43 AM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I was reading this thread and I noticed someone mentioned using Option Strict On. Well I tried this and about half my vb.net program became underlined in blue! I'm totally confused now on why I would ever want use this. Can someone look at the following lines and tell me what I am doing wrong?

Dim t1 As TextBox = e.Item.FindControl("tbJobNo")

Dim FileNameTxt As String = "Brochure" & Session("JobNo") & ".mac"

oWrite.WriteLine(drl.Item("CopyText").Trim)
 
Old October 7th, 2003, 11:29 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Yes, you should always use Option Strict. This forces the code to do "early binding" which is MUCH more efficient than "late binding". "Type safe" is another buzz phrase associated with this topic. By using Option Strict in your code, you are forcing all your type assignments to match up.

Here's what you'd need to do:

Dim t1 As TextBox = CType(e.Item.FindControl("tbJobNo"), TextBox)

Dim FileNameTxt As String = "Brochure" & CType(Session("JobNo"), String) & ".mac"

'Not sure what to do with this cause I don't know what "drl" is.
oWrite.WriteLine(drl.Item("CopyText").Trim)


Peter
 
Old October 7th, 2003, 12:19 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,

Thanks for the response. What does the mean?

Thanks,
Bob
 
Old October 7th, 2003, 12:22 PM
Registered User
 
Join Date: Jun 2003
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Peter,

Oops,

The [red] and [b} are just the attributes you used to highlight the changes. It didn't show up in my email. Sorry. Please disregard my last post.

Thanks,
Bob
 
Old October 7th, 2003, 10:43 PM
Registered User
 
Join Date: Oct 2003
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Duncan! I tried coding using Option Strict ON and now I can see the importance of CType. Thanks again. :)

Lulu





Similar Threads
Thread Thread Starter Forum Replies Last Post
VB Ch11 Using Profiles p415 - CType Rich57 BOOK: Beginning ASP.NET 2.0 BOOK VB ISBN: 978-0-7645-8850-1; C# ISBN: 978-0-470-04258-8 0 June 12th, 2008 05:58 AM
Return CType(Me.GetPropertyValue("Cult thuyvncr BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 1 December 16th, 2006 11:26 AM
CType string To Byte sirmilt BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 1 March 2nd, 2006 09:06 PM
CType Issues Dave0865 BOOK: ASP.NET Website Programming Problem-Design-Solution 3 March 23rd, 2004 12:50 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.