473,938 Members | 16,113 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Applicati on"), Word.Applicatio n)
app.Selection.T ext = "Hi, this is text."

Also does:

app = GetObject(Nothi ng, "Word.Applicati on")
app.Selection.T ext = "Hi, this is text."

Both do work.

Regards,

Marcel
Nov 21 '05 #1
15 2250
The second probably wont work with Option Strict On.

Greg

"Marcel" <m.*********@ho me.nl> wrote in message
news:32******** *****@individua l.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.Applicati on"), Word.Applicatio n)
app.Selection.T ext = "Hi, this is text."

Also does:

app = GetObject(Nothi ng, "Word.Applicati on")
app.Selection.T ext = "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.*********@ho me.nl> wrote in message
news:32******** *****@individua l.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.Applicati on"), Word.Applicatio n)
app.Selection.T ext = "Hi, this is text."

Also does:

app = GetObject(Nothi ng, "Word.Applicati on")
app.Selection.T ext = "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.*********@ho me.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.Applicati on"), Word.Applicatio n)
app.Selection.T ext = "Hi, this is text."

Also does:

app = GetObject(Nothi ng, "Word.Applicati on")
app.Selection.T ext = "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.Applicatio n
Set App = New Word.Applicatio n
..
..
..
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******** ******@TK2MSFTN GP11.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.Applicatio n
Set App = New Word.Applicatio n
.
.
.
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.*********@ho me.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
21436
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
3849
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 absolutely no added functionality to it.
6
10838
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 found this in the VB Language reference: <quote> The DirectCast keyword introduces a type conversion operation. You use it the same way you use the CType keyword.... Both keywords take an expression to be converted as the first argument, and
7
2724
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
1399
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 = Nothing Try output = CType(Item, aType) Catch ex As Exception
4
4872
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 equivalent for converting objects. Thanks, Todd
7
2697
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
10487
by: c_shah | last post by:
Very beginner question.. What's difference between cstr vs .ToString vs Ctype for converting to String?
19
3267
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 implementations of the standard C <ctype.h> header define functions like toupper/tolower/etc as macros. To make it work in STL algorithms, you have to include <cctypeheader instead of <ctype.h>. At least on my PC (Debian/gcc 3.3),...
0
11512
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9853
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8209
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7377
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6072
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6286
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4900
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4442
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3495
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.