Andrew Ross

Hi,

I hope this is the right place to post this.

I found on www.netfx3.com a sample workflow application and it has a section of code that was written in C#, we need it in vb. I have worked with delegates before, but never like this..

Dictionary<string, Activity> activityIdToActivity = new Dictionary<string, Activity>();

Walker walker = new Walker();

walker.FoundActivity += delegate(Walker skedWalker, WalkerEventArgs eventArgs)

{

Activity activity = eventArgs.CurrentActivity;

activityIdToActivity.Add(activity.QualifiedName, activity);

};

walker.Walk(root);

My questtion would be, how would i do this in VB. If you would like to see all the code download the sample from here http://wf.netfx3.com/files/folders/sample_applications/entry4074.aspx, Where i'm talkin about is in the WorkflowAdminService Project...ClassesRemoteFromHost.vb..Line 965...

Thanks,
Andrew



Re: Visual Basic Language Convert Delegate

Dick Donny

Hi

The code you posted makes use of functionality that is not supported by the VB compiler, namely anonymous delegates and closures. To this end, the code cannot be directly converted; however, you can create something similar.

You will need to change the activityidtoactivity variable to be scoped at module level (or some other scope accessible by the class instance) so that our new foundactivity method can access it when invoked.

Simply then move the contents of the delegate declaration into a seperate procedure and create a delegate that points to that using the addressof function.

Hope it helps

Richard

private _activityidtoactivity as generic.dictionary(of string, activity) '' Module scope

private sub .....

_activityidtoactivity = new generic.dictionary(of string, activity)

dim walker as new walker

addhandler walker.foundactivity, addressof me.foundactivity

walker.walk(root)

removehandler walker.foundactivity, addressof me.foundactivity

end sub

private sub foundactivity(byval skedwalker as walker, byval eventargs as walkereventargs)

dim activity as activity = eventargs.currentactivity

_activityidtoactivity.add(activity.qualifiedname, activity)

end sub





Re: Visual Basic Language Convert Delegate

Andrew Ross

Hi,

Thank you very much... That is what i had thought about, but was not totaly sure if that is what i should do..

Andrew