Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

vb6

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

like image 409
Zolomon Avatar asked Jan 15 '10 11:01

Zolomon


3 Answers

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 image 166
Jon Skeet Avatar answered Oct 18 '22 21:10

Jon Skeet


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.

like image 41
missingfaktor Avatar answered Oct 18 '22 22:10

missingfaktor


Like so

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

becomes

EventStealingInfo.RIGHT_DOWN = true;
EventStealingInfo.RIGHT_UP = true;
like image 45
Klaus Byskov Pedersen Avatar answered Oct 18 '22 21:10

Klaus Byskov Pedersen