Google Search

Google
 

Wednesday, July 23, 2008

Video: VB9 - Extension Methods

Presenter:Mike Taulty

In Visual Basic 9, it is possible to give the appearance of adding 'extension methods' to classes without changing the classes themselves. We'll explore how extension methods work in this session.

The following text is a software generated transcript of the video. Click on a minute link to jump to a location within the video


Minute 0

Oh and has done anything session it s one in a series of short videos we spent 10 minutes or so looking how it is its official studio net framework minus m Tolkien I work in the developer and platform group in Microsoft UK and is one of a series of sessions around prospective language changes coming in Visual Basic version 9 there was changes Pentagon to the banner of language integrated query or link but in these sessions were playing a part of little bit in having a look at the constituent pieces and in this particular video once they ve look at how we can use a new feature called extension methods in VB 9 gates are very visual studio 2005 at the May preview of the link technologies which gives me a preview of the benign language compiler and the idea of these extension methods is that we can make a class looks like it has methods that it doesn t actually have and it is provides a neat syntax for something it can already achieved today is just a nice

Minute 1

Tactical way of doing it is every imagine that they have a few classes that are very public class a must have a public must be and whether have that inherits a watch inherits a public last see is well balanced inherits big nylon of these glasses as you have any functionality on the modern moment let s go and add one or two properties to play with addresses at visit a year and add a property we will go ahead call this sounds on value cover the value of the property are the five and the go ahead and would make this property is a problem and that it protected should measure we can build so it has ever protected property to a now imagine a man and a situation where I want to take advantage of these new extension methods had either

Minute 2

Adding that organisers add a new module when he fastened to the project the go ahead at the class will cause my extensions extent Shalom is completely and allowed aboard an extension method on this class we can see here the way we phase extension method is it has to be a static or a shared method so ago I had a right to share the method will call their son say hello something like that I m going to attribute the class itself to say that this is an extension class and it will go ahead and fix thethat may not work and that actually the method is well to say that an extension method once again will have fixed theto make that work and all I did in this method is is right out

Minute 3

Hello extension one did a low dose will as go ahead here and extend parameter as a Vatican colour extended object as a finally when it is put this flat into arecall it extensions one sofa laughing into anow if we go out on a nodule with that in place those go ahead and declare ourselves and a food at the instance of a pair that we can do it is quite strange if if we bring in an import statement to that extension which is created which is extensions while not my extensions

Minute 4

The edit on instance here we can all say hello unless were the road went on the press are five better at 11 and a see that we are stepping into that particular instance and as our first parameter were yet the extended object being passed across so this is really a shortened syntax instead of writing a method that explicitly takes an instance of a week and is living an extension this method looks like it belongs to the datatype of a whole class of any in fact it doesn t have the compiler trick now but it s not debugging from second it s important to realise their these extensions are really being controlled by namespace is so little head and writer a duplicate of this as copy that code languages go ahead and write another extensions or add a new flat in here and will call

Minute 5

Is my extensions to really a little faces in here but will put this into a extensionstoclassicalmyextensionstoambleeithermethodcalledsayhellobutwillhavehellofromextensiontoneverforgottheoriginalmoduleatthispointsimplybychangingthisimportstatementfromextensionsonetoextensionstoreacttochangethemethodisgoingtobecalledintheseathatifwerunoutovertheirfiveatthispointandthenstepintothatmethodwithinourcallingextensiontoratherthanextensiononesowhatishappeningisthatessentiallyatthepointwherethecompilerseasoniscalledtosayhellonaturallygoesandlookstoseewhetherthedatethattheobjecttypeofaactuallyhasanethanolicorsayhelloasitisinthecompilergoesoffinsearchisallofthe

Minute 6

Scope namespace is to see if it can find a suitable definition and the thick animal call at now one of the compiler do it if you ve got an import statement foot to possible extension so we have a say hello method here and say hello method their and in that case the compiler fails because he doesn t know which one you actually want and so it is a view to work that out based upon which namespace you import also what happens if you have an extension method in an incident like this where you go and visit the class Eddie and you had a method service is a method now the class itself and if we can rebuild at that point

Minute 7

But I don t miss it back it somewhere to rebuild that one again plays break point impressive five then we ll see that whereas she stepping into the method on the glass itself so the compiler cup prefers methods on the flat itself to extension methods and is always going to call those methods in preference if you want to add a couple of other things if we remove this method on the glass itself is just blogging has removed that method so I m back to extension but instead of an instance of any year let s will have an instance of the sea so up nicely as you see it important to realise that by building an extension of a we ve also built an extension of any classes that derive from a something you see in this case and once again if a gratifying step in we will pop up in extension method naturally what would you

Minute 8

Customers is being faster than a bloody intrusive is a sea but we ve extended a and all types of the rye from a the last thing is important here is that we availability the access that you get to these extended objects inside this extension function 31 to realise that we do not have any more access to the class a value will expect just because the writing and extensions extends a full and mean by that is inside this routine here on a rethink all the standard object routines and standard object functions or we can t do is go ahead and call property here because it was protected so we don t access to protected a private data it s just like any regular user of the class a we get no special privileges because we are an extension to a we just get public access to age

Minute 9

Properly expected hopeful to summarise what was seen here we know now that extension method who is the illusion of adding methods to type you might have source at that time she might not it doesn t matter either still an extension methods to it and all this derived classes we thought that the access you get an extension method is just to the public members of the type you don t get any special access because you re an extension method and we saw also that the compiler will always call a method on the type itself in preference to an extension method as long as it can see a method that matches in terms of signature and see a method on the type itself that matches in terms of signature that is only called not the extension method where can you go to a more information on VB 9 and language integrated query over the site of an amnesty any continuing there on the screen and off that site you find a document that talks about the changes in the VB

Minute 10

Language that forthcoming about this it is not finished yet things could change and if you think that on this particular session feel free to mail me on just might got told the Microsoft com them that councillors and council watching the session and I hope to catch you next

Click here to go to original video page

No comments: