Hello! How can I convert this piece of VB6 code into C#?
I've tried on my own and got so far to:
EDIT: Code I'm trying to translate exists here: http://www.codeproject.com/KB/vb-interop/MouseHunter.aspx
Hello! How can I convert this piece of VB6 code into C#?
I've tried on my own and got so far to:
EDIT: Code I'm trying to translate exists here: http://www.codeproject.com/KB/vb-interop/MouseHunter.aspx
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.
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?
Like so
With EventStealingInfo
.RIGHT_DOWN = True
.RIGHT_UP = True
End With
becomes
EventStealingInfo.RIGHT_DOWN = true;
EventStealingInfo.RIGHT_UP = true;
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.
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.
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?
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.
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.