tags:

views:

231

answers:

9

Hello! How can I convert this piece of VB6 code into C#?

http://pastebin.com/f16e19351

I've tried on my own and got so far to:

http://pastebin.com/f7ca199f0

EDIT: Code I'm trying to translate exists here: http://www.codeproject.com/KB/vb-interop/MouseHunter.aspx

A: 

There is no equivalent to With in C# as far as I am aware and you need to explicitly list the object when referred to its member functions/properties.

Joel Goodwin
A: 

There is no C# equivalent.

Danail
+6  A: 

You haven't shown the EventThief code, which makes it impossible to tell, really. 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. 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.

EDIT: Okay, you've now shown the original EventThief code which does use Booleans... but you haven't shown your ported EventThief code. You wrote:

It says et.LEFT_UP is a short

... but it shouldn't be. In the original it's a Boolean, so why is it a short in your port?

Jon Skeet
I added a link in my question, see the edit. Just search for "eventthief" and you'll find it.
Zolomon
I don't know why it's a short, I use tlbimp as mentioned here: http://www.csharp-station.com/Articles/CSharpAndActiveXDlls.aspx to allow the ActiveX dll to be used in .NET and I take it that it converted it to a short instead or something? I'll just try and manually convert the whole thing to C#. Thanks for the help!
Zolomon
+2  A: 

Like so

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

becomes

EventStealingInfo.RIGHT_DOWN = true;
EventStealingInfo.RIGHT_UP = true;
klausbyskov
A: 

Can't you just change the type of the LEFT_UP to be a 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.

Sam Holder
I'll have to look that up. Thanks!
Zolomon
+4  A: 

I am not too sure but I think the following in 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.

This construct in C# is known as Object Initializer.

missingfaktor
There's a mistake in the C# code. If `EventStealingInfo` is a variable of type `EventThief` (like in the code of the OP), it'd have to be `var EventStealingInfo = new EventThief { ... }`.
stakx
@stakx : Thanks for pointing that out.
missingfaktor
A: 

The "with" keyword is just a shortcut to save retyping the variable name when you're setting multiple properties. There is no equivalent in 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? Can you simply make the LEFT_UP fields boolean instead?

Paolo
Look at the link in the edit in my question, search for "EventThief" and you'll find it.
Zolomon
Actually it also caches the object reference, which can save time if the object in question must be resolved by chasing down a nested series of properties - or worse yet a method call that creates an instance for temporary use. Of course you can also manually cache by creating a new temporary reference variable first and using that.
Bob Riemersma
+1  A: 

I think it's closer you can go:

EventThief EventStealingInfo = new EventThief()
{
    RIGHT_DOWN = true,
    RIGHT_UP = true
};
Rubens Farias
I'd go with this answer (except that it should really say `... = new EventThief()`). Object initializers are probably the closest thing to Visual Basic's `With` statement.
stakx
ops, fixed that, ty
Rubens Farias
+1  A: 

Can I call your proposal Option A.

  1. Take community VB6 code that creates a DLL for dealing with Windows hooks.
  2. Translate that to C#

Can I suggest Option B and Option C, which I think will be easier?

Option B
1. Start with Microsoft's C# code for dealing with Windows hooks.
2. Adapt it as necessary, looking at what API calls the VB6 code makes.

Option C
1. Take the built VB6 DLL from the community code.
2. Call that DLL from your new C# application via Interop.

MarkJ
I tried option 2 first, then I tried option 3 when the first failed. That's when I ran into the error with the wrong type in the EventThief struct, so I tried option 1 and I ran into another error that said something about the GUID being wrong and I got lost on what to do.
Zolomon