469,315 Members | 1,628 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,315 developers. It's quick & easy.

Why do ASP.NET Settings Properties use CTYPE instead of DirectCast

If Visual Studio knows the type, why does the system-generated property use
CType instead of DirectCast? DirectCast is more efficient right?

For example - Here's what we have for a setting named
'StandardWebsiteUserRoleID'...
Public ReadOnly Property StandardWebsiteUserRoleID() As Integer
Get
Return CType(Me("StandardWebsiteUserRoleID"),Integer)
End Get
End Property

Thanks
Sep 3 '07 #1
3 2054
I think CType is a VB wrapper for DirectCast, and as such, it probably has
some additional checks that DirectCast doesn't have.

Mike Ober.

"Mike" <Mi**@discussions.microsoft.comwrote in message
news:6C**********************************@microsof t.com...
If Visual Studio knows the type, why does the system-generated property
use
CType instead of DirectCast? DirectCast is more efficient right?

For example - Here's what we have for a setting named
'StandardWebsiteUserRoleID'...
Public ReadOnly Property StandardWebsiteUserRoleID() As Integer
Get
Return CType(Me("StandardWebsiteUserRoleID"),Integer)
End Get
End Property

Thanks


Sep 4 '07 #2
Ctype doesn't throw an exception, DirectCast, while more efficient, assumes
that you know exactly what the type is. Additional behavior would be
controlled by having Option Strict and Option Explicit both set to "true" -
which you should always do with VB.NET.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Mike" wrote:
If Visual Studio knows the type, why does the system-generated property use
CType instead of DirectCast? DirectCast is more efficient right?

For example - Here's what we have for a setting named
'StandardWebsiteUserRoleID'...
Public ReadOnly Property StandardWebsiteUserRoleID() As Integer
Get
Return CType(Me("StandardWebsiteUserRoleID"),Integer)
End Get
End Property

Thanks
Sep 4 '07 #3
Your statement is exactly correct. So then why for Settings, when we do know
exactly what the type is, doesn't Visual Studio use DirectCast instead of
CTYPE?

It just seems logical to me that since we're sure of the type, Visual Studio
chooses to use CTYPE.

In the end it doesn't really matter, but it does make me curious.

"Peter Bromberg [C# MVP]" wrote:
Ctype doesn't throw an exception, DirectCast, while more efficient, assumes
that you know exactly what the type is. Additional behavior would be
controlled by having Option Strict and Option Explicit both set to "true" -
which you should always do with VB.NET.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Mike" wrote:
If Visual Studio knows the type, why does the system-generated property use
CType instead of DirectCast? DirectCast is more efficient right?

For example - Here's what we have for a setting named
'StandardWebsiteUserRoleID'...
Public ReadOnly Property StandardWebsiteUserRoleID() As Integer
Get
Return CType(Me("StandardWebsiteUserRoleID"),Integer)
End Get
End Property

Thanks
Sep 4 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Joeri | last post: by
13 posts views Thread by Carl Howarth | last post: by
6 posts views Thread by Ot | last post: by
8 posts views Thread by John A Grandy | last post: by
7 posts views Thread by Brian Henry | last post: by
5 posts views Thread by Andy | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.