Wednesday, November 30, 2022 ..:: ::.. Register  Login
 Bloggers Minimize

 Sponsors Minimize

 Users Online Minimize
Membership Membership:
Latest New User Latest: AdeleGadsden
New Today New Today: 96
New Yesterday New Yesterday: 65
User Count Overall: 18956

People Online People Online:
Visitors Visitors: 320
Members Members: 0
Total Total: 320

Online Now Online Now:

 Welcome to! Minimize

If you are a developer or designer who is creating cutting edge user interface experiences using Microsoft Expression Blend you've just found a great resource!  The purpose of this site is to share our experience with this brand new product.  As with any new piece of software there is a ton to discover.  Bugs will be revealed and workarounds discovered, hopefully this can be a resource to share in the discovery process!

While this site will focus largely on Expression Blend, it will also encompass WPF, Visual Studio, Silverlight, and other .net 3.5 technologies as they relate to the creation of great software.

Please login using your Microsoft LiveID or register a new account.  Doing so will let you comment on any of the blog entries on the site.  We look forward to helping you in your endeavors as well as you helping others in theirs!  We'd love to have more bloggers on our site...if you are interested please register and contact us!

Follow us:

 Anoymous Delegate Methods with local variables in VB Minimize
Location: BlogsSean Cullinan    
Posted by: Sean Cullinan 8/26/2008 8:49 PM

I'm going to start this post off with a small rant on two things.  First its the lack of sample code in VB for those of us working with .net 3.5 technologies.  Seriously, it sucks.  I appreciate those who put up code samples and of course they are going to do so in the language of their choice, but when its MSFT and their employees not giving us VB code it is frustrating.

Second, I think MSFT needs to keep C# and VB compatible which they are mostly, but not completely.  When converting code I run into a few problematic spots.  One is with anonomous delegates.  Personally I think that reading and following code with anonymous delegates is atrocious.  It just doesn't seem to make sense to me how code can be executing asynchronously in the middle of a function without tha function being active.  I just don't like it.  However, it does allow a scenario that is useful: you can utilize local variables in your inline anonymous delegate and all the state is maintained.   There is no way to do this intrinsicly in VB.

Here is an example in C# (psuedo C# because I'm winging it):

private void Main() {
   mySub(1, btn1);
   mySub(2, btn2);
   mySub(3, btn3);

private void mySub (int i, button button1) { +=
      delegate(object sender, EventArgs e)

private void myRoutine(int i){

The interesting thing is that when btn1 is clicked then 1 will display in the messagebox, when btn2 is clicked 2 will display in the messagebox and when btn3 is clicked 3 will display in the messagebox.  IOW the state of the object i is maintained when the anonymous delegate is run for that particular instance of mySub.

How can one convert this to VB which doesn't support anonymous delegates?  At first glance you may just make a module level variable to store the value of i, add an event handler using addhandler, addressof myRoutine, then make myRoutine use that module level declaration.  That will not work, in the example above you would get the value "3" returned for all 3 of the buttons because th last call to mySub would set that module level variable.

Fortunately the solution is very simple, just use a private class that contains the state of any variables you want to maintain along with the function for the event handler.  Here we go in VB:

private sub main()
    mySub(1, btn1)
   mySub(2, btn2)
   mySub(3, btn3)
end sub

private class btnState
   public i as integer
    public sub click(sender as object, e as system.eventargs)
    end sub
end class

private sub mySub(i as integer, button1 as button)
    dim objState as new btnState
    objState.i = i
  addhandler, addressof myObj.Click
end sub

The object now contains the state of i as needed and also contains the delegated subroutine that will be called whenever a button is clicked.  Since each instance contains its own version of "i" you get the same results as you would from the C# code above!

To finish my rant, I really wish that they would add the missing pieces of VB that C# has so that code conversions would be easier to do.  If not, PLEASE GIVE US MORE VB SAMPLES!

Hope this is helpful!




Permalink |  Trackback

Comments (2)   Add Comment    By coach outlet store online on 5/2/2014 2:59 AM
your wedding <a href="" title="true religion outlet">true religion outlet</a>'t just one of the <a href="" title="">coach factory outlet</a> days of your <a href="" title="Louis Vuitton bags">louis vuitton handbags</a> - it's also one of <a href="" title="coach outlet">coach outlet</a> longest. The <a href="" title="coach outlet">coach outlet store online</a> shoe is essential for getting <a href="" title="isabel marant">isabel marant shoes</a> hours of <a href="" title="">coach factory outlet</a> Save up to 50% off <a href="" title="timberland">timberland shoes</a> entire <a href="" title="coach factory">coach factory outlet</a> site when you <a href="" title="">coach factory</a> up for the <a href="" title="coach outlet">coach outlet</a> email list membership. (expires January 11, 2022) Enjoy <a href="" title="coach factory">coach factory outlet</a> on your <a href="" title="michael kors">michael kors</a> of $150 or more.<a href="" title="louis uitton">louis vuitton purses</a> up to 60% off <a href="" title="coach factory">coach factory outlet</a> handbags when you <a href="" title="michael kors outlet">michael kors outlet online</a> prices on over 3,000 <a href="" title="coach outlet store online">coach outlet store online</a>. Hoochie mama! <a href="" title="marc by marc jacobs">marc by marc jacobs outlet</a> doing so <a href="" title="prada outlet">prada outlet</a> of ultra skin-tight <a href="" title="prada outlet">prada outlet</a> like they're <a href="" title="">coach factory outlet online</a> to be <a href="" title="true religion">true religion jeans</a> apart. <a href="" title="giuseppe zanotti">giuseppe zanotti sneakers</a> shoe sale, <a href="" title="timberland">timberland boots</a> sandals, flats, high heels <a href="" title="michaelkors">michael kors purses</a> at up to 50% off <a href="" title="coach factory outlet">coach factory outlet online</a> shipping on $150. I <a href="" title="michael kors outlet">michael kors outlet</a> some new patio <a href="" title="cheap jerseys">cheap jerseys</a> and planted an <a href="" title="toms shoes">toms shoes</a> and I'm <a href="" title="michael kors outlet">michael kors outlet online</a> it so much. yes <a href="" title="">coach factory outlet</a> is required. John Tulin, chief executive of Randa Accessories' <a href="" title="louis uitton outlet online">louis uitton outlet</a> division, will retire on May 16. Tulin, who <a href="" title="marc by marc jacobs">marc jacobs</a> in 1971, has been its CEO <a href="" title="true religion jeans">true religion jeans</a> 1995. <a href="" title="fitflop shoes">fitflop shoes</a> is an amazing <a href="" title="Coach Outlet Online">coach outlet online</a> that was <a href="" title="celine outlet">celine outlet</a> clean <a href="" title="supra">supra shoes</a> in Africa.<a href="" title="ralph lauren">Polo ralph lauren</a> and <a href="" title="celine handbags">celine handbags</a> a very happy, peaceful and <a href="" title="alexander wang">alexander wang handbags</a> Year 2014. May the cares of 2013 be put behind you and <a href="" title="fitflop sandals">fitflop sandals</a> of new <a href="" title="sac burberry">sac burberry</a> and <a href="" title="toms shoes">toms usa</a> in 2014! <a href="" title="giuseppe zanotti">giuseppe zanotti</a> color or texture, each <a href="" title="alexander wang">alexander wang shoes</a> its own unique appeal. What <a href="" title="tiffany and co jewelery">tiffany jewelry</a> eye with the <a href="" title="michael kors">michael kors outlet</a> Bag though was <a href="" title="tiffany & co">tiffany and co</a> US the texture!

Re: Anoymous Delegate Methods with local variables in VB    By sfdgssfgbs on 7/25/2014 10:00 PM
<H1><a href="" title="retro 6 jordans"><strong>retro 6 jordans</strong></a></H1><br><a href="" title="michael kors sales"><strong>michael kors sales</strong></a><br><H1><a href=""><strong>oakley sunglasses usa</strong></a></H1><br><a href=""><strong>michael kors jewelry on sale</strong></a><br>[][b]michael kors tortoise watch[/b][/url]<br> coach outlet bags<br>

Your name:
Add Comment   Cancel 

 Search Minimize

 Archive Minimize

 Blendblog Survey Minimize
Our next client application will be written using:

Which Tablet Do you expect to buy?

Submit Survey  View Results

Copyright 2011 by   Terms Of Use  Privacy Statement
DotNetNuke® is copyright 2002-2022 by DotNetNuke Corporation