Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Translation From C#?

Status
Not open for further replies.

steverbs

Programmer
Jul 17, 2003
253
GB
Hi all.

Can anyone translate the following bit of C# code to VB.NET for me?

Code:
		public event EventHandler MyEvent;
		private void OnMyEvent()
		{
			if (MyEvent!= null)
				MyEvent(this, EventArgs.Empty); 
		}

Thank you.

Stephen.
 
It's fairly straightforward:
Code:
Public Event MyEvent As EventHandler 
Private Sub OnMyEvent() 
 MyEvent(Me, EventArgs.Empty) 
End Sub

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
So obvious when you think about it eh? It was the "if (MyEvent != null)" that was confusing me. I couldn't figure out why you'd need to check that.

Thanks for the help.

Stephen.
 
Oops missed out the If Statement...
Code:
Public Event MyEvent As EventHandler 
Private Sub OnMyEvent() 
If Not MyEvent Is Null Then
	MyEvent(Me, EventArgs.Empty) 
End If
End Sub

I would suggest either buying a VB.NET book or reading up on VB syntax on the internet if you are going to be programmming in it.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
But that doesn't work does it? I mean, you need to call RaiseEvent when referencing an Event. So in the first example you gave, you'd need to write:
Code:
Private Sub OnMyEvent()
    RaiseEvent MyEvent(Me, EventArgs.Empty)
End Sub

Also, I do know VB. It was the C# sharp I was having trouble with... and am still having trouble with ;p
I'm still not sure why you'd need to check if MyEvent is Nothing.
 
I've not needed to set up events in VB.NET yet, but I have done so many times in Delphi and I presume the underlying logic is pretty much the same.

If you are creating a component or control or some other resource that will be used elsewhere and you provide the opportunity for the client code to attach to one of your events, it is sensible to check whether or not the client has provided a handler. Your code may need to react differently depending on whether or not the client has subscribed to event.

I hope this makes sense.
 
Sorry you would need to call RaiseEvent (I simply wrote the VB version of the code and didn't check what it was doing. My fault!)
Also, I do know VB. It was the C# sharp I was having trouble with... and am still having trouble with ;p
Oh Ok - I assumed you had written it in C# and wanted to rewrite it in VB.

I'm still not sure why you'd need to check if MyEvent is Nothing.
I don't think you would have to check if it is null as it has just been initiated but you just asked for a conversion so I didn't go into the logistics of whether it was needed or not.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
OK. I see where you are coming from, but I have no idea how to implement that in VB.NET. I can't see anything about it in the documentation.

Also, while trying to translate this C# code I have come accross the following line that truly has me stumped:
Code:
public ClassName() : base() {
    BaseClassEvent += new BaseClassEventHandler(ClassName_BaseClassEvent);

I get the contructor definition line, but the next line doesn't make much sense to me.

Any ideas?

Thanks again for your help.

Stephen.
 
I think thats the C# way of using AddHandler

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
A good reference if you are trying to translate from C# to VB.NET is Microsoft's MCAD/MCSD for Windows Based Applications which shows both languages code side by side.

I think the VB equivalent is AddHandler
 
Right, so we'd then have:
Code:
Public Sub New()
    AddHandler Me.BaseClassEvent, AddressOf ClassName_BaseClassEvent

Would you say that's right?
 
Looks good to me although it doesn't necessarily have to be in Sub New.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Nice one, cheers.

Just one more question. Is there a 'short-hand' version of C#'s % operator in VB.NET?
 
OK. Not to worry I found it: 'Mod'

Thanks for all of your help.

Stephen.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top