Hi Waqas,

Apologies if I seemed rude in my post. We get far too many posts from
people who find it too demanding on their precious time to google for
the most basic stuff. Since you did not mention the research you had
already done (a cardinal omission, IMO !), my tone was none too
polite.

I had already tried the conversion at the Dev Fusion site (IMO, the
best converter available) before posting a response. I also observed
its inability to convert the line which attaches the Event handler and
that was the reason why I inserted the hint to look up the AddHandler
statement! ;-)

This is all about understanding Events and Delegates. To illustrate
the concept, I created a sample class, WiaClass:
(**A syntax highlighted version of the code is available at
<http://dotnetdevelopment.pastebin.com/f5f79fc91> **)

---
Public Class WiaClass
  'Let VB generate the Delegates automatically.
  Public Event OnDeviceDisconnected(ByVal sender As Object, ByVal e As
EventArgs)
  Public Event OnTransferComplete(ByVal sender As Object, ByVal e As
EventArgs)

  Public Sub TransferAllFunds()
    RaiseEvent OnTransferComplete(Nothing, Nothing)
    Disconnect()
  End Sub

  Private Sub Disconnect()
    RaiseEvent OnDeviceDisconnected(Nothing, Nothing)
  End Sub
End Class
---

Calling code subscribes to this as follows:
---
Public Class MyWindowsForm
  Private Sub CreateManager()
    Dim wiaMgr As New WiaClass()
    AddHandler wiaMgr.OnDeviceDisconnected, AddressOf
OnDeviceDisconnectedHandler
    AddHandler wiaMgr.OnTransferComplete, AddressOf
OnTransferCompleteHandler

    'Call the method that raises the events.
    wiaMgr.TransferAllFunds()
  End Sub

  Private Sub OnDeviceDisconnectedHandler(ByVal sender As Object,
ByVal e As EventArgs)
    MessageBox.Show("Device disconnected!!")
  End Sub

  Private Sub OnTransferCompleteHandler(ByVal sender As Object, ByVal
e As EventArgs)
    MessageBox.Show("Transfer Completed!!")
  End Sub
End Class
---

When more handlers are needed for the same Event type, we can declare
a Delegate type as follows:

---
Public Class WiaClass
  Public Delegate Sub _IWiaEvents_OnDeviceDisconnectedEventHandler
(ByVal sender As Object, ByVal e As EventArgs)
  Public Delegate Sub _IWiaEvents_OnTransferCompleteEventHandler(ByVal
sender As Object, ByVal e As EventArgs)
  Public Event OnDeviceDisconnected As
_IWiaEvents_OnDeviceDisconnectedEventHandler
  Public Event OnTransferComplete As
_IWiaEvents_OnTransferCompleteEventHandler

  Public Sub TransferAllFunds()
    RaiseEvent OnTransferComplete(Nothing, Nothing)
    Disconnect()
  End Sub

  Private Sub Disconnect()
    RaiseEvent OnDeviceDisconnected(Nothing, Nothing)
  End Sub

End Class
---

Calling code can access it as before or in the following way (which is
similar to the code you require converted to VB) :
---
Private Sub CreateManager()
  Dim wiaMgr As New WiaClass()
  AddHandler wiaMgr.OnDeviceDisconnected, New
WiaClass._IWiaEvents_OnDeviceDisconnectedEventHandler(AddressOf
wia_OnDeviceDisconnectedHandler)
  AddHandler wiaMgr.OnTransferComplete, New
WiaClass._IWiaEvents_OnTransferCompleteEventHandler(AddressOf
wia_OnTransferCompleteHandler)

  'Call the method that raises the events.
  wiaMgr.TransferAllFunds()
End Sub
---


On Aug 6, 12:55 am, waqas <[email protected]> wrote:
> Hi Cerebrus,
>
> Thanks for taking to the time to look at my query. Let me say that I
> have used one of the online free code converters. Using this 
> one:http://www.developerfusion.com/tools/convert/vb-to-csharp/
>
> I was able to convert the entire project to CV. It seemed to work well
> except that the code I quoted above. This was converted to:
>
> Private Sub CreateManager()
>     Try
>         wiaMgr = New WiaClass()
>
>         wiaEvtDisc = New _IWiaEvents_OnDeviceDisconnectedEventHandler
> (Me.wia_OnDeviceDisconnected)
>         wiaMgr.OnDeviceDisconnected += wiaEvtDisc
>
>         wiaEvtTransfer = New _IWiaEvents_OnTransferCompleteEventHandler
> (Me.wia_OnTransferComplete)
>         wiaMgr.OnTransferComplete += wiaEvtTransfer
>     Catch generatedExceptionName As Exception
>
>         MessageBox.Show(Me, "Create WIA manager object failed", "WIA",
> MessageBoxButtons.OK, MessageBoxIcon.[Stop])
>         Application.[Exit]()
>     End Try
> End Sub
>
> The problem is, as I mentioned the line wiaMgr.OnDeviceDisconnected +=
> wiaEvtDisc returns the following error:
>
> " 'Public Event OnDeviceDisconnected(Id As String)' is an event, and
> cannot be called directly. Use a 'RaiseEvent' statement to raise an
> event."
>
> And this is where my problems lie. I am aware of the .Net statements
> AddHandler and RaiseEvent. I use them on a regular basis when I create
> controls at run time . But in this case I am dealing with an event in
> a seperate class and am not sure how to deal with it.
>
> Btw there are two event call backs in the form:
>
> Private wiaEvtDisc As _IWiaEvents_OnDeviceDisconnectedEventHandler
> Private wiaEvtTransfer As _IWiaEvents_OnDeviceDisconnectedEventHandler
>
> Any help would be appreciated. Sorry for not being more verbose in my
> original post.
>

Reply via email to