简体   繁体   English

C#和VB6:如何将“ with”语句转换为C#?

[英]C# & VB6: How to convert 'with' statement to C#?

How can I convert this piece of VB6 code into C#? 如何将这段VB6代码转换为C#?

http://pastebin.com/f16e19351 http://pastebin.com/f16e19351

I've tried on my own and got so far to: 我已经尝试过,到目前为止:

http://pastebin.com/f7ca199f0 http://pastebin.com/f7ca199f0

EDIT: Code I'm trying to translate exists here: http://www.codeproject.com/KB/vb-interop/MouseHunter.aspx 编辑:我要翻译的代码在这里存在: http : //www.codeproject.com/KB/vb-interop/MouseHunter.aspx

You haven't shown the EventThief code, which makes it impossible to tell, really. 您还没有显示EventThief代码,这使您很难分辨。 But in general: 但一般来说:

With expression
   .Foo = a
   .Bar = b
End With

would translate to 将转化为

var x = expression;
x.Foo = a;
x.Bar = b;

(Of course you can specify the type explicitly...) (当然,您可以明确指定类型...)

The commonality here is that expression is only evaluated once. 这里的共同点是expression仅被评估一次。 In the particular code you showed, there's no need for an extra variable of course, as the expression is only the local variable in the first place. 在您显示的特定代码中,当然不需要额外的变量,因为表达式首先只是局部变量。

Your actual error looks like it's just to do with the types of EventThief.RIGHT_DOWN etc rather than with the WITH statement. 您的实际错误似乎仅与EventThief.RIGHT_DOWN等类型有关,而不是与WITH语句有关。

EDIT: Okay, you've now shown the original EventThief code which does use Booleans... but you haven't shown your ported EventThief code. 编辑:好的,你现在已经显示原EventThief代码确实使用布尔...但你还没有表现出你的移植 EventThief代码。 You wrote: 你写了:

It says et.LEFT_UP is a short 它说et.LEFT_UP很短

... but it shouldn't be. ...但是不应该这样。 In the original it's a Boolean , so why is it a short in your port? 在最初,它是一个Boolean ,那么为什么您的端口short呢?

The following in VB VB中的以下内容

With EventStealingInfo
    .RIGHT_DOWN = True
    .RIGHT_UP = True
End With

can be roughly translated to 可以大致翻译成

var EventStealingInfo = new EventThief(){
    RIGHT_DOWN = true,
    RIGHT_UP = true
};

where RIGHT_UP and RIGHT_DOWN are public properties in the EventStealingInfo class. 其中RIGHT_UPRIGHT_DOWNEventStealingInfo类中的公共属性。

This construct in C# is known as Object Initializer . C#中的这种构造称为对象初始化器

Like so 像这样

With EventStealingInfo
    .RIGHT_DOWN = True
    .RIGHT_UP = True
End With

becomes 变成

EventStealingInfo.RIGHT_DOWN = true;
EventStealingInfo.RIGHT_UP = true;

I think it's closer you can go: 我认为您可以走近一点:

EventThief EventStealingInfo = new EventThief()
{
    RIGHT_DOWN = true,
    RIGHT_UP = true
};

Can I call your proposal Option A . 我可以将您的提案称为选项A。

  1. Take community VB6 code that creates a DLL for dealing with Windows hooks. 以社区VB6代码创建用于处理Windows挂钩的DLL。
  2. Translate that to C# 将其翻译为C#

Can I suggest Option B and Option C, which I think will be easier? 我可以建议我认为会更容易的方案B和方案C吗?

Option B 选项B
1. Start with Microsoft's C# code for dealing with Windows hooks. 1.从Microsoft的C#代码开始处理Windows挂钩。
2. Adapt it as necessary, looking at what API calls the VB6 code makes . 2.根据需要对其进行调整,查看调用VB6代码的 API。

Option C 选项C
1. Take the built VB6 DLL from the community code . 1.从社区代码中获取内置的VB6 DLL。
2. Call that DLL from your new C# application via Interop . 2.通过Interop从新的C#应用​​程序中调用该DLL。

据我所知,C#中没有与With等效的对象,并且在引用对象的成员函数/属性时需要明确列出该对象。

没有等效的C#。

Can't you just change the type of the LEFT_UP to be a bool? 您不能仅将LEFT_UP的类型更改为bool吗?

looking at your code and the way you are using the EventThief, you might want to use a flag enumeration so you can set individual bits and then do bitwise comparisons. 查看代码和使用EventThief的方式,您可能希望使用标志枚举,以便可以设置各个位,然后进行按位比较。

The "with" keyword is just a shortcut to save retyping the variable name when you're setting multiple properties. 关键字“ with”只是在设置多个属性时保存重新键入变量名的快捷方式。 There is no equivalent in C#. C#中没有等效项。

Even if there were you would still have an issue that you're apparently trying to assign a boolean to a short data type. 即使您仍然遇到问题,您显然仍试图将布尔值分配给短数据类型。

What's in the EventThief class? EventThief类中有什么? Can you simply make the LEFT_UP fields boolean instead? 您可以简单地将LEFT_UP字段设为布尔值吗?

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

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