繁体   English   中英

将代码 C# 转换为 VB - Function 参考

[英]Convert code C# to VB - Function references

我有一些 C# 代码正在尝试转换为 VB.NET。 我在每个项目中都引用了一些外部依赖项。 C# 线是

TWriteFileCommand writeFile = (TWriteFileCommand)CommunicationLink.CreateCommand(TOPKernel.TCommand.CommandType.WriteFile);

我真的不明白等号后括号中的 TWriteFileCommand 在该语句中是什么意思。 当我尝试在 VB.NET 中执行此操作时,我不断收到错误,因为我无法获得对 CreateCommand function 的引用。 我得到的最接近的是:

Dim MyAppBase As OPSupport.App.AppBase
Dim writeFile As TWriteFileCommand
writeFile = MyAppBase.CommunicationLink.CreateCommand(TOPKernel.TCommand.CommandType.WriteFile)

但是,我在等号 @MyAppBase 后面有一个绿色下划线,它标记了一个错误“变量 MyAppBase 在被赋值之前被使用。运行时可能会导致 null 引用异常。”

我错过了什么? 为什么在 C# 代码中的实例很好,我需要什么来获得 VB.NET 中的等效实例? 请指教。

那是一个演员; 如果CreateCommand返回objectCommandBase或类似的东西 - 它会对TWriteFileCommand进行类型检查。

如果您正在编译 C# 代码 - 您可以使用反射器进行翻译吗? 这向您显示 C# 和等效的 VB 在下拉菜单中翻转。

我不确定MyAppBase在做什么; C# 中不存在的 - 为什么将它添加到 VB?

编辑我不“做”VB,但我在反射器中查找了它; (Foo)bar变为DirectCast(bar, Foo)

看起来CreateCommandCommunicationLink类型中的 static 方法。 要么,要么有一个名为CommunicationLink的属性,并且该属性的类型具有CreateCommand方法。

= 之后括号中的位是强制转换。 在 VB.NET 中使用CTypeDirectCast执行此操作。

()是 C# 从一种类型转换为另一种类型的方法之一。 VB 等价物是CType()

直接等价物是:

Dim writeFile As TWriteFileCommand 
writeFile = DirectCast(CommunicationLink.CreateCommand(TOPKernel.TCommand.CommandType.WriteFile), TWriteFileCommand)

在 C# 代码中贴标和实例化的 CommunicationLink 变量在哪里? 您需要确保 VB 代码对 object 使用相同的源。

我真的看不出Dim MyAppBase As OPSupport.App.AppBase是从哪里来的。

(TWriteFileCommand)而言,这是一种类型转换。 DirectCast具有(几乎?)相同的功能。

这是因为您没有为 MyAppBase 分配变量值:-)

Dim MyAppBase As OPSupport.App.AppBase = new OPSupport.App.AppBase()
Dim writeFile As TWriteFileCommand
writeFile = MyAppBase.CommunicationLink.CreateCommand(TOPKernel.TCommand.CommandType.WriteFile)

?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM