By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 915 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

CType() what's the difference?

P: n/a
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
Nov 21 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
The second probably wont work with Option Strict On.

Greg

"Marcel" <m.*********@home.nl> wrote in message
news:32*************@individual.net...
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

Nov 21 '05 #2

P: n/a
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" <m.*********@home.nl> wrote in message
news:32*************@individual.net...
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

Nov 21 '05 #3

P: n/a
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

"Marcel" <m.*********@home.nl>
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

Nov 21 '05 #4

P: n/a
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
Nov 21 '05 #5

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #6

P: n/a
Herfried,

This example hereafter is for VB6?

Marcel

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> schreef in bericht
news:ec**************@TK2MSFTNGP11.phx.gbl...
"Cor Ligthert" <no************@planet.nl> schrieb:
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
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #7

P: n/a
"Marcel" <m.*********@home.nl> schrieb:
This example hereafter is for VB6?


Yes.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #8

P: n/a
>> 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#:


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

Cor
Nov 21 '05 #9

P: n/a
"Cor Ligthert" <no************@planet.nl> schrieb:
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#:


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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #10

P: n/a

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote
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#:


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.

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.
Nov 21 '05 #11

P: n/a
"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:Og**************@TK2MSFTNGP11.phx.gbl...
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.
Nov 21 '05 #12

P: n/a
Larry,

"Larry Serflaten" <se*******@usinternet.com> schrieb:
>>> 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#:
>
> Did I deny something, the sample is about performance, and just
> something
> in general?


You said 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....


That's true. Nevertheless, I assume that Cor used 'Option Strict Off' as a
synonym for using late binding, because it allows late binding.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #13

P: n/a
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/arc...7/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/archiv...27/166868.aspx

Hope this helps
Jay

"Larry Serflaten" <se*******@usinternet.com> wrote in message
news:Og**************@TK2MSFTNGP11.phx.gbl...
"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote
>>> 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#:
>
> 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.

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.

Nov 21 '05 #14

P: n/a
Herfried,
That's true. Nevertheless, I assume that Cor used 'Option Strict Off' as
a synonym for using late binding, because it allows late binding.

This was in my starting messageTherefore 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


Nov 21 '05 #15

P: n/a
Cor,

"Cor Ligthert" <no************@planet.nl> schrieb:
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.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.