CType() what's the difference?

  • Thread starter Thread starter Marcel
  • Start date Start date
M

Marcel

Hi,

I've found some sample code to put some text into Word. I'm testing this
code to learn from it, but I can't find an explanation for the use of CType
in the following code. Can someone explain the use of CType?

app = CType(GetObject(Nothing, "Word.Application"), Word.Application)
app.Selection.Text = "Hi, this is text."

Also does:

app = GetObject(Nothing, "Word.Application")
app.Selection.Text = "Hi, this is text."

Both do work.

Regards,

Marcel
 
First is an early bound call to the object; compiler knows exactly what
method to call at compile and won't let you call something by mistake
including checking all parameter types and results.

Second is a late bound call probably using IDispatch interface. Even though
it works, it is bad practice to use late binding.

Stick to using proper object types whenever you can instead of calling
methods on the "object" type.
 
Marcel,

In other words than Greg and Shariq, however the same

CType and DirectCast tells what "Type" the "Object" is at compile time and
it will be set in the code.
Without that it has to be found at Runtime and therefore is your program at
runtime slower.

When you have Option Strict On in top of your program, you are not allowed
*not* to tell what object is used.

Therefore we mostly tell in these newsgroups that early binding is with
Option Strict On

As I describe it often

With Option Strict On
performance of VBNet = C#
Without
VBNet = VB6

I hope that this add something extra to the text from Greg and Shariq?

Cor
 
Greg, Shariq and Cor,

Thank you all for your replys. Now it's clear to me what to do and to
behave.

Regards,

Marcel Kollenaar
 
Cor Ligthert said:
As I describe it often

With Option Strict On
performance of VBNet = C#
Without
VBNet = VB6

The last line is wrong. You could use early binding in VB6 too, and you can
use reflection to mimic VB.NET's late binding in C#:

\\\
Dim App As Word.Application
Set App = New Word.Application
..
..
..
Call App.Quit
///
 
As I describe it often
The last line is wrong. You could use early binding in VB6 too, and you
can use reflection to mimic VB.NET's late binding in C#:

Did I deny something, the sample is about performance, and just something in
general?

Cor
 
Cor Ligthert said:
Did I deny something, the sample is about performance, and just something
in general?

You saud that without 'Option Strict On' VB.NET's performance is the same as
the performance of VB6, which is simply not true.
 
Herfried K. Wagner said:
You saud that without 'Option Strict On' VB.NET's performance is the same as
the performance of VB6, which is simply not true.


I don't think it accurately describes the situation either. It is far too general.

VB6 users don't always use late binding for that statement to reflect the facts.
Likewise, code could be written in .Net with Option Strict On, and then when
the project is finished, have the option reset to Off and not see any difference.

It is not the setting of that option that makes the difference, but the architecture
of the code in either language that is the deciding factor....

LFS



as such a statment indicates.
 
Larry Serflaten said:
I don't think it accurately describes the situation either. It is far too
general.

VB6 users don't always use late binding for that statement to reflect the
facts.
Likewise, code could be written in .Net with Option Strict On, and then
when
the project is finished, have the option reset to Off and not see any
difference.

It is not the setting of that option that makes the difference, but the
architecture
of the code in either language that is the deciding factor....

Well put.
 
Larry,

Larry Serflaten said:
I don't think it accurately describes the situation either. It is far too
general.

VB6 users don't always use late binding for that statement to reflect the
facts.
Likewise, code could be written in .Net with Option Strict On, and then
when
the project is finished, have the option reset to Off and not see any
difference.

It is not the setting of that option that makes the difference, but the
architecture
of the code in either language that is the deciding factor....

That's true. Nevertheless, I assume that Cor used 'Option Strict Off' as a
synonym for using late binding, because it allows late binding.
 
Larry,
Likewise, code could be written in .Net with Option Strict On, and then
when
the project is finished, have the option reset to Off and not see any
difference.
This is a good point!

When I need to use Late Binding I may actually have Option Strict On
temporarily to help eliminate any areas of the code that I do not want Late
Binding or Implicit conversions, then put Option Strict Off to run the code.
Granted I then have a handful of errors on statements where I am using Late
Binding, which is where I would like to see "ducks" added to VB.NET.

http://www.panopticoncentral.net/archive/2004/07/12/1393.aspx

http://boo.codehaus.org/Duck+Typing

COM interop has a couple of places where Late Binding is useful, such as CDO
& other "VBScript" COM object models which are based on Object.

Ducks basically allow Option Strict Off on a per variable level.


BTW: This is an interesting article on Late Binding in VB.NET:
http://blogs.msdn.com/cambecc/archive/2004/06/27/166868.aspx

Hope this helps
Jay
 
Herfried,

This was in my starting message
Therefore we mostly tell in these newsgroups that early binding is with
Option Strict On

And about this thread, every applications can be written rotten (And let us
not talk about a program that add 1 to 1).

What I stated just a general idea what can happen, and not an accademical
proof.

One of the performance benefits with VBNet can in my opinion be archieved by
consequently using early binding. (And that is easier to archieve with
Option Strict On).

Cor
 
Cor,

Cor Ligthert said:
One of the performance benefits with VBNet can in my opinion be archieved
by consequently using early binding. (And that is easier to archieve with
Option Strict On).

Full ACK.
 

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

Back
Top