Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have a small problem. I dont know how to convert a .net4 addhandler to .net2
Can someone give me a example or a link?
Or a definition of how it's named, so i can search google.

The event signature don't equals the ASub signature.

VB
    Dim name As String = "TikElentrik"
    Dim someThing As New SomeThing(name)

    Event SomeEvent(ByVal Name As String) ' Importend
    addhandler SomeEvent, sub(arg) ASub(name,someThing) '.NET 4.0
    'How to get it in NET 2.0

Public Sub ASub(ByVal Name As String, ByVal Something As something)
    'Doing things
End Sub


In C# its somthing like this
C#
string name = "TikElentrik";
SomeThing someThing = new SomeThing(name);

public event SomeEventEventHandler SomeEvent; //Importend
SomeEvent += arg => ASub(name, someThing); //.NET 4.0
//How to get it in NET 2.0

public void ASub(string Name, SomeThing Something)
{
	//Doing Things
}


Thanks,
TikElentrik
Posted
Updated 13-Sep-11 0:23am
v4

According to MSDN here[^] and here[^] the statement is the same in both versions. However, neither of them seem to match what you have coded.
 
Share this answer
 
Comments
TikElentrik 13-Sep-11 4:20am    
My problem is not how to add the handler but, the part after the comma. The sub(arg) ASub(name,SomeThing). It has to do with lambada expressions or something like that.

The code in c# was: SomeEvent += (args) => ASub(name,someThing)
Richard MacCutchan 13-Sep-11 4:45am    
Sorry, I have not yet got to grips with Lambdas. Maybe you should stick to the old fashioned straight VB.NET.
TikElentrik 13-Sep-11 5:01am    
no problem
That's what i am trying to do but don't know how yet. I find it klote(in dutch) home much time i need sometimes to find out things. Weeks go by on a simple application of a couble of 10000 lines. Always surfing the net on how i need to right something.

Thanks.
There is no difference between .Net 4.0 and .Net 2.0 event handling in VB. You need to declare the method first, and ensure the event handler has the same signature as the event declaration.
Try something like this:-

VB
Public Event someEvent(ByVal name As String, ByVal b As String)

   Public Sub Form1()
       AddHandler someEvent, AddressOf SubA
   End Sub

   Private Sub SubA(ByVal name As String, ByVal b As String)

   End Sub


Hope this helps
 
Share this answer
 
v3
Comments
TikElentrik 13-Sep-11 4:56am    
I am sorry, i have forgotten to add the event itself
it looks like: Event SomeEvent(ByVal Name As String)

The problem is the event signature don't match the delegate signature. In .NET4 you can use:
Addhandler someEvent, sub(arg) ASub(Name,someThing)
And then it match the signature of the event. its this part:
sub(arg) SubA(Name,someThing)
i need help with. In .net2 was it not yet invented i think
Thanks.
Wayne Gaylard 13-Sep-11 5:02am    
.Net 2.0 does not have Lambda expressions, and so you are going to have to use regular syntax. VB.Net lambdas have to return a value(They do not support Anonymous Delegates, which is a Lambda which does not return a value, which you are trying to do), and so cannot be used in event handling signatures. Therefore, you are going to have to create your method with the same signature as the event. It is as simple as that. There is no other way. To be honest I don't think you can do this even with Lambdas and .Net 4.0. The method has to have the same signature period.
TikElentrik 13-Sep-11 5:17am    
i know that .net2 does not have Lambada expressions. to ("To be honest I don't think you can do this even with Lambdas and .Net 4.0.") I don't actualy know how it named mayby its a function of vb2010 i don't know. I learned everything from the net I have never spoke with someone about programming. to (" The method has to have the same signature period.") I will try that. Thanks now i know already something more
Thanks to the persons who tried to help me. I found a solution.
Friend Class Solution
    Public Class SomeThing
        'A bunch of stuff
        Public Name As String
        Public Sub New(ByVal Name As String)
            Me.Name = Name
        End Sub
    End Class

    Public Event someEvent(ByVal Name As String) 'Importend

    Private NotInheritable Class EventHelper
        Private someThing As SomeThing
        Private parent As Solution

        Public Sub New(ByVal parent As Solution, ByVal someThing As SomeThing)
            Me.parent = parent
            Me.someThing = someThing
        End Sub

        Public Sub ASub(ByVal Name As String)
            Me.parent.ASub(Name, Me.someThing)
        End Sub
    End Class

    Private Sub test()
        Dim name As String = "TikElentrik"
        Dim someThing As New SomeThing(name)

        ' Where the problem was
        'addhandler SomeEvent , sub(arg) ASub(name,someThing) '.NET 4.0
        'How to get it in NET 2.0
        AddHandler someEvent, AddressOf New EventHelper(Me, someThing).ASub
    End Sub

    Public Sub ASub(ByVal Name As String, ByVal Something As something)
        'Doing things
    End Sub
End Class

If there is a better one, please let me know

Mvg,
TikElentrik.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900