CType()有什么区别? [英] CType() what's the difference?

查看:81
本文介绍了CType()有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我找到了一些示例代码,可以将一些文本放入Word中。我正在测试这个

代码以便从中学习,但我在下面的代码中找不到使用CType

的解释。有人可以解释使用CType吗?


app = CType(GetObject(Nothing,Word.Application),Word.Application)

app。 Selection.Text ="这是文字。


也是:


app = GetObject(Nothing," Word) .Application")

app.Selection.Text ="这是文字。


两者都有效。


问候,


Marcel

解决方案

第二个可能不适用于Option Strict On。


Greg


" Marcel" <米********* @ home.nl>在消息中写道

news:32 ************* @ individual.net ...


CType的解释。有人可以解释使用CType吗?

app = CType(GetObject(Nothing,Word.Application),Word.Application)
app.Selection.Text ="这是文本。

也是:

app = GetObject(Nothing," Word.Application")
app.Selection.Text ="这是文字。

两者都有效。

问候,

Marcel



首先是对象的早期绑定调用;编译器确切地知道在编译时调用的是什么

方法并且不会让你错误地调用某些东西

包括检查所有参数类型和结果。


其次是可能使用IDispatch接口的后期绑定调用。即使

它有效,使用后期绑定也是不好的做法。


坚持使用适当的对象类型,而不是调用
对象上的
方法类型。


" Marcel" <米********* @ home.nl>在消息中写道

news:32 ************* @ individual.net ...


CType的解释。有人可以解释使用CType吗?

app = CType(GetObject(Nothing,Word.Application),Word.Application)
app.Selection.Text ="这是文本。

也是:

app = GetObject(Nothing," Word.Application")
app.Selection.Text ="这是文字。

两者都有效。

问候,

Marcel



Marcel,


换句话说,不是Greg和Shariq,但是相同的


CType和DirectCast告诉了什么"类型" 对象是在编译时和

它将在代码中设置。

如果没有它,它必须在运行时找到,因此是你的程序在

运行时速度较慢。


如果在程序顶部有Option Strict On,则不允许使用

*而不是*告诉使用什么对象。


因此我们主要在这些新闻组中告诉早期绑定是

Option Strict On


我经常描述


选项严格打开

VBNet的表现= C#

没有

VBNet = VB6


我希望这能为Greg和Shariq的文字增加一些额外的东西吗?


Cor


" Marcel" < m。********* @ home.nl>



我找到一些示例代码将一些文本放入Word 。我正在测试这个代码以便从中学习,但我在下面的代码中找不到使用
CType的解释。有人可以解释使用CType吗?

app = CType(GetObject(Nothing,Word.Application),Word.Application)
app.Selection.Text ="这是文本。

也是:

app = GetObject(Nothing," Word.Application")
app.Selection.Text ="这是文字。

两者都有效。

问候,

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

解决方案

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



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



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



这篇关于CType()有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆