Groups | Blog | Home
all groups > dotnet academic > august 2007 >

dotnet academic : When to use variables, objects and CTYPE?


winlin
8/30/2007 7:56:02 PM
Hello
Under what conditions "must" you use "CTYPE" when working with variables and
PvdG42
8/31/2007 6:55:12 PM
[quoted text, click to view]

Can you provide some sort of contextual information on what you want to do
that you believe needs ctype?
Normally, if I need to make a specific conversion that requires "forcing, I
use explicit_cast<type>(argument).
Wannano
11/27/2007 9:57:26 PM
You must use CType when your code has the OPTION STRICT ON either in the
project properties or in the VB Code itself and when the conversion is a
narrowing one.

For instance, you can always write: Dim i as Integer
Dim d as Double = i
But if you have set "Option strict On" in the project properties or in the
code, Dim d as Double

Dim i as Integer = d will not
compile,

you have to write: Dim d as Double

Dim i as Integer = CType(d, double)
(it's a way to tell the compiler that you know what you are
doing)
For objects, if Customer Inherits from Person, you can always write: Dim C
as new Customer

Dim P as Person = C
But with Option Strict On, if you write Dim C as new Customer
Dim P as Person =
C
Dim D as Customer
= P, the compiler will not accept the last statement
it will accept: Dim C as new Customer
Dim P as Persomn = C
Dim D as Customer = CType(P, Customer)

I hope I am clear enough. Sometimes my students understand what I say.

Marc Biotteau, MCT

[quoted text, click to view]

AddThis Social Bookmark Button