VB6 WithEvents in C#? How?

chuawenching

Well-known member
Joined
Jul 26, 2003
Messages
66
Hi there.

I search google that i can use delegate and events to replace WithEvents.. but i had 1 vb6 sample code.. which i not sure how to code it in c#

Code:
====
private WithEvents ParA as EngineParSystem

private Sub ParA_NewPar(VelocX as Single, VelocY as Single, VelocZ as Single)

LifeS = CurrentLifeT

End Sub

So how to code delegates and event with that in c#?

Any help?

Thanks.

Regards,
Chua Wen Ching :p
 
There is no equivilant in C#, since you need to manually add the handler anyway. Youll also need to create a custom EventHandler and EventArgs class and delegate.

Look for information on event handling on C# for more information.
 
ok thanks...

I had looked into some of them...

but i am trying my best to relate it...

Anyway thanks for the reply..

Regards,
Chua Wen Ching :p
 
Your VB.NET sample is quite incomplete:

[VB]
private WithEvents ParA as EngineParSystem

private Sub ParA_NewPar(VelocX as Single, VelocY as Single, VelocZ as Single) Handles ParA.SomeEvent

LifeS = CurrentLifeT

End Sub
[/VB]

As VolteFace has stated, WithEvents has no direct equivalent in C#. Translated to C#, your code will look something like this:

EngineParSystem ParA;
...
ParA.SomeEvent += new System.EventHandler(ParA_NewPar);

<ParA_NewPar sub somewhere...>
 
oh thanks...

Just wondering, i had a library created in vb.net, instead of converting it into c#, can i just load the entire library source code, and call it directly into c#.

I never try before, but i heard it can right?

Regards,
Chua Wen Ching :p
 
Its not possible to mix up .vb and .cs code files in one project. Whats possible is compiling the vb component and using it in C#, or the other way around. Google ".NET code converters" if you need help in converting codes.
 
dunno if this helps... here is what I have used

...first create a delegate...

public class ClassName

public ClassName()
{
public delegate void PlusButtonClickEventHandler(object sender, System.EventArgs e);

//define the event...

public event PlusButtonClickEventHandler PlusButtonClick;

}

// within the class you are referencing the control/class add
// an event constructor...

...

public class SomeOtherClass : System.Windows.Forms.Form

public SomeOtherClass()
{

this.form.PlusButtonClick += new ClassName.PlusButtonClickEventHandler(Function_Name);


// then add the function


private void Function_Name(object sender, System.EventArgs e)
{
//do stuff;
}

}

CS
 
Back
Top