473,396 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

CType() what's the difference?

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
15 2200
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
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
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
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
"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
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
"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
>> 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
"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

"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
"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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mark Kamoski | last post by:
Hi-- What is the difference between Convert.ToString(obj) and CType(obj, String)? (Assume obj is a variable of type Object.) Please advise. Thank you.
4
by: Mike Cooper | last post by:
There is something about inherited classes I evidently don't know... I wrote the following class: Class Class1 inherits System.Windows.Forms.DataGridTextBoxColumn End Class There is...
6
by: Ot | last post by:
I apparently have a bit to learn about Casting and Conversion. I have been thinking of them as the same but a discussion in another thread leads me to believe that this is wrong thinking. I...
7
by: Brian Henry | last post by:
is there any speed diffrences between doing Ctype or directcast? I know about the inherite diffrences, but process usage time wise, does one take up more cycles then the other? thanks
3
by: Filip D'haene | last post by:
Hi, I want to make a function like CType but can't figure out how to do this. Function ConvertWhithoutException(ByVal Item As Object, ByVal aType As Type) As Object Dim output As Object =...
4
by: Todd Plambeck | last post by:
Is there a more C# like alternative to CType? I don't like using the VB6esque "C" conversion methods(CStr, CInt, etc) . I'd prefer to user something like Convert.ToInt32(), but can't find a...
7
by: Joe | last post by:
Hello All: Does anyone know the difference between CType(x,Integer) vs. Integer.Parse(x)? Is there a performance advantage to one or the other? TIA, -- Joe
5
by: c_shah | last post by:
Very beginner question.. What's difference between cstr vs .ToString vs Ctype for converting to String?
19
by: Taras_96 | last post by:
Hi all, A poster at http://bytes.com/forum/thread60652.html implies that using strtoupper in transform doesn't work because ctype.h may define strtoupper as a macro: "The problem is that most...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.