<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://msmvps.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Joacim's view on stuff : c#, visual basic</title><link>http://msmvps.com/blogs/joacim/archive/tags/c_2300_/visual+basic/default.aspx</link><description>Tags: c#, visual basic</description><dc:language>en</dc:language><generator>CommunityServer 2008.5 SP2 (Build: 40407.4157)</generator><item><title>Creating an IE plug-in</title><link>http://msmvps.com/blogs/joacim/archive/2011/11/15/creating-an-ie-plug-in.aspx</link><pubDate>Tue, 15 Nov 2011 15:59:22 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1802529</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1802529</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2011/11/15/creating-an-ie-plug-in.aspx#comments</comments><description>&lt;p&gt;I was recently approached by a customer about an intriguing project, unfortunately I can’t tell you too much about it since I’m under an NDA, but it involved creating an add-in for Internet Explorer and a custom Windows Service which would act as the backend. I’ve never created any add-ins for IE before but as a long time Windows developer I know that the Microsoft web browser is heavily dependent on COM. Since I’ve been doing mainly .NET development for the last 10 years or so, and sort of left the COM world behind me, I first started looking for some form of framework that would allow me to develop the add-in using managed code.&lt;/p&gt;  &lt;p&gt;I found two different frameworks that I started evaluating. The first is an open source project originally created by Microsoft called &lt;a href="http://archive.msdn.microsoft.com/SpicIE" target="_blank"&gt;SpicIE&lt;/a&gt; and the second is a commercial product called &lt;a href="http://www.add-in-express.com/programming-internet-explorer/" target="_blank"&gt;Add-In-Express for Internet Explorer&lt;/a&gt;. The latter is from the company Add-In Express Ltd who has specialized themselves on creating add-in frameworks and has a wide range of products to create add-ins for Office and IE in both .NET and in VCL for Delphi.&lt;/p&gt;  &lt;p&gt;Both of these frameworks takes care of all the COM communication and implements all the necessary COM interfaces so you can concentrate on the more important things, namely what your add-in is supposed to accomplish.&lt;/p&gt;  &lt;p&gt;In my case I needed to be able to create a toolbar button and an IE bar, which are those side panes similar to how Favorites and History are displayed within IE. There was no problem creating these in either framework but the Add-In-Express also had an Advanced IE Bar control with some additional features which I liked a lot. They also have visual designers which simplified everything and I was able to create a stub application, that demonstrated the final design, within a few minutes. It took me a bit longer to do the same using SpicIE, mainly because the documentation is a bit sparse (as the case often is with open source projects), but it comes with several demo projects that you can learn from. Add-In Express on the other hand have several video tutorials available on their &lt;a href="http://www.add-in-express.com/" target="_blank"&gt;website&lt;/a&gt;, which of course is a lot faster way to learn the basics without digging through a bunch of source code.&lt;/p&gt;  &lt;p&gt;SpicIE also comes with this reservation:&lt;/p&gt;  &lt;p&gt;&lt;b&gt;Important: SpicIE is released independently of Internet Explorer and related products. It is not an officially supported product by the Internet Explorer team. While SpicIE is a valuable tool for prototyping and testing Internet Explorer extensions, it is not recommended for use with released software.&lt;/b&gt;&lt;/p&gt;  &lt;p&gt;For me there was no question about which framework we would use for this particular project and if you would ever need to create a commercial product that requires an add-in for Internet Explorer I highly recommend &lt;a href="http://www.add-in-express.com/programming-internet-explorer/" target="_blank"&gt;Add-In Express&lt;/a&gt;, you’ll get a very stable product and excellent support from the team at Add-In Express Ltd, but this of course comes with a price tag so if you’re just a hobby programmer then go ahead and give SpicIE a try.&lt;/p&gt;  &lt;p&gt;The next thing we needed to do was put a team together to start developing this particular system. Since the customers budget was rather tight my manager decided that we should use our offshore company in India for the development of the actual add-in while I should concentrate on the architecture and create the backend system.&lt;/p&gt;  &lt;p&gt;I think I have to write a blog post about the experience I’ve had with this and other projects when the development team sits in different continents, with different cultures and in different time zones. It’s both exciting, fun, and very frustrating at the same time, but that have to wait for some other day.&lt;/p&gt;  &lt;p&gt;Have fun!&lt;/p&gt;&lt;div class="wlWriterHeaderFooter" style="text-align:right;margin:0px;padding:4px 0px 4px 0px;"&gt;&lt;a href="http://digg.com/submit?url=http%3a%2f%2fmsmvps.com%2fblogs%2fjoacim%2farchive%2f2011%2f11%2f15%2fcreating-an-ie-plug-in.aspx&amp;amp;title=Creating+an+IE+plug-in"&gt;&lt;img src="http://digg.com/img/badges/100x20-digg-button.png" width="100" height="20" alt="Digg This" title="Digg This" border="0" style="border:0;" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1802529" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/add-in/default.aspx">add-in</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/internet+explorer/default.aspx">internet explorer</category></item><item><title>When OOP stinks and when you instead should leave a smell behind…</title><link>http://msmvps.com/blogs/joacim/archive/2010/08/06/when-oop-stinks-and-when-you-instead-should-leave-a-smell-behind.aspx</link><pubDate>Fri, 06 Aug 2010 14:45:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1775480</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1775480</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2010/08/06/when-oop-stinks-and-when-you-instead-should-leave-a-smell-behind.aspx#comments</comments><description>&lt;p&gt;I just saw an interesting video blog post by the CTO of &lt;a target="_blank" href="http://www.devexpress.com/"&gt;Devexpress&lt;/a&gt; entitled &lt;a target="_blank" href="http://tv.devexpress.com/#PacManCode.movie"&gt;Pac-Man and Object-Oriented Programming&lt;/a&gt;, in which Julian talks about how we&amp;rsquo;ve been doing object-oriented programming for 20 years now (or even more than that if you come from the SmallTalk world) and how we, as OOP/OOD programmers and designers think about programming. He then quickly moves on to comparing that to a game of Pac-Man.&lt;/p&gt;
&lt;p&gt;Now Pac-Man was a great video game developed by Namco in Japan back in 1979 and it was released in Europe and the US the year after. The game was an instant success, maybe because it was different from most of the other video games of the time, such as Space Invaders, Galaxy, and Asteroids, which was mainly shoot &amp;lsquo;em up games in which you should kill and/or destroy everything you saw (much unlike video games of today where we have 3D FPS games in which you walk around and kill/destroy everything you see&amp;hellip; Oh, wait there isn&amp;rsquo;t that much of a difference, is there?). In Pac-Man you play this pie-chart looking character that walks around in a maze eating dots at the same time as you&amp;rsquo;re trying to avoid getting caught by one of the four colorful ghosts that are, for some reason, hunting you.&lt;/p&gt;
&lt;p&gt;Whoops&amp;hellip; Sorry! I got a bit nostalgic there for a moment and sort of strayed away from the topic. This blog post was supposed to be about object oriented programming and design and not about gaming. So let&amp;rsquo;s get back on track, shall we?&lt;/p&gt;
&lt;p&gt;If you, as an OOP developer, would design the Pac-Man game today, how would you go about it? You should probably create a class for Pac-Man and another for the ghosts that hunts him. Well, they might actually be subclasses of a common &lt;em&gt;character&lt;/em&gt; class since they do share some very basic logic such as they can&amp;rsquo;t walk through walls and other basic elements that you can think of. But they would still be 2 different classes. You might also have a Maze class that draws the actual maze and have some other logic like knowing which level you&amp;rsquo;re playing and such. The dots and power ups might also be different classes.&lt;/p&gt;
&lt;p&gt;But the logic of the ghosts aren&amp;rsquo;t that easy. How will they go about to chase Pac-Man? They obviously need to know about his position but how do you calculate the closest path to get to him? The ghosts can&amp;rsquo;t go through a wall so you must calculate the nearest path around the maze. Doing that in every game cycle on a modern day computer is not such a big deal, but Pac-Man came out back in 1979 and the CPUs used at the time couldn&amp;rsquo;t do that and still produce a fast pacing game.&lt;/p&gt;
&lt;p&gt;The solution Julian suggested was that maybe the ghosts in the game are like blood-hounds and Pac-Man is given away a scent as he moves around in the maze. The square in the maze Pac-Man just left gets a value that as Pac-Man moves on decreases in value until the scent has evaporated. This way the ghosts has no real knowledge about Pac-Man at all since they are just following a scent. The scent itself is now a property of the maze object and not of the ghost object. Julian called this anti-object oriented programming since the logic is not in the &amp;ldquo;real-world&amp;rdquo; object of the ghost (is that really a real-world object?). So by changing the logic of the ghost not to actually hunt Pac-Man but to follow a scent when they find it and also added some logic to the maze class, the anti-object, which you probably only used for drawing before. &lt;/p&gt;
&lt;p&gt;I love this example since it&amp;rsquo;s an example of thinking-out-of-the-box when it comes to OOP and OOD. Instead of always trying very hard to make the classes as close to real-world objects as possible make a few changes and simplify the whole idea.&lt;/p&gt;
&lt;p&gt;To be completely fair here, this is actually not how the AI of the ghosts in the original game was designed. If you&amp;rsquo;re interested I invite you to read &lt;a target="_blank" href="http://home.comcast.net/~jpittman2/pacman/pacmandossier.html"&gt;the Pac-Man Dossier&lt;/a&gt; especially chapter 3 and chapter 4 that explains how the actual AI for the ghosts was created. But the fact that the actual design was different is beside the point. Julian showed a simple solution to a hard problem you might face when you&amp;rsquo;re doing object oriented design.&lt;/p&gt;
&lt;p&gt;Have fun!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1775480" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/OOD/default.aspx">OOD</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/OOP/default.aspx">OOP</category></item><item><title>Microsoft is switching the light but will it turn on or off?</title><link>http://msmvps.com/blogs/joacim/archive/2010/08/04/microsoft-switches-the-light-but-will-it-turn-on-or-off.aspx</link><pubDate>Wed, 04 Aug 2010 15:04:12 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1775354</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>6</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1775354</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2010/08/04/microsoft-switches-the-light-but-will-it-turn-on-or-off.aspx#comments</comments><description>&lt;p&gt;During the VSLive! keynote in Redmond, Microsoft yesterday announced a new Visual Studio product called &lt;a href="http://msdn.microsoft.com/en-au/lightswitch/default.aspx" target="_blank"&gt;LightSwitch&lt;/a&gt;.&lt;/p&gt;  &lt;p&gt;LightSwitch is a new SKU for Visual Studio that will allow people to create line of business (LOB) applications for the desktop and the cloud without writing a single line of code. It will also be shipped with future version of Visual Studio Pro and above. In a way it reminds me of Access but with the difference that it can use different data sources, including Sharepoint and Azure SQL. Since the announcement there have been a wild discussion within the MVP programmers community, especially among VB and C# MVPs if this is a good or a bad thing.&lt;/p&gt;  &lt;p&gt;We have seen tools like this before that are aimed to power users who aren’t full-fledged programmers and when a professional programmer have to take over the project it’s a mess with business logic code mixed up with UI code and data access code. I have not yet made up my mind if this is good or bad, but I do see some great opportunities with LightSwitch. Since it generates .Net code, in either C# or VB, and that it does seem to use an n-tier design I feel that this tool might become very successful. Of course we have to wait until the public beta is released later this month before we can actually see if the generated code lives up to the high standard professional developers demand.&lt;/p&gt;  &lt;p&gt;Some of the discussion has questioned if this should have been released as an Office tool rather than as a Visual Studio SKU and even though that is a valid point I truly feel that the fact this tool is made by the Visual Studio team rather than the Office team the code generation will be better since the Office team isn’t really interested in creating development tools but also that more professionals might use this for prototyping or getting a quick start application that they continue to build on. If this will be the actual usage of the tool I don’t care if a power user instead of a programmer does the initial prototyping.&lt;/p&gt;  &lt;p&gt;In either case I’m looking forward to giving LightSwitch a try as soon as the beta have been released. Several videos will be released showcasing LightSwitch during the next few days/weeks on the &lt;a href="http://msdn.microsoft.com/en-au/lightswitch/default.aspx" target="_blank"&gt;LightSwitch home page&lt;/a&gt;. Have a look and make up your own mind. Jason Zander, who held the presentation at VSLive, also wrote an excellent &lt;a href="http://blogs.msdn.com/b/jasonz/archive/2010/08/03/introducing-microsoft-visual-studio-lightswitch.aspx" target="_blank"&gt;blog post&lt;/a&gt; about LightSwitch that demonstrates what it does and what it can be used for.&lt;/p&gt;  &lt;p&gt;So what do you think? Are tools that hides the complexity of programming a good or a bad thing? Feel free to leave a comment.&lt;/p&gt;  &lt;p&gt;Have fun.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1775354" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/lightswitch/default.aspx">lightswitch</category></item><item><title>At your service (part II) – ASMX Web Service vs WCF</title><link>http://msmvps.com/blogs/joacim/archive/2009/09/16/at-your-service-part-ii-asmx-web-service-vs-wcf.aspx</link><pubDate>Wed, 16 Sep 2009 15:55:00 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1723827</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>1</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1723827</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2009/09/16/at-your-service-part-ii-asmx-web-service-vs-wcf.aspx#comments</comments><description>&lt;p&gt;&lt;strong&gt;Introduction&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;&lt;a href="http://msmvps.com/blogs/joacim/archive/2009/09/15/at-your-service-part-i-wcf-tutorial-in-vb.aspx"&gt;Last time&lt;/a&gt; I demonstrated how to develop a Windows Communication Foundation (WCF) service. This time we&amp;rsquo;re going to look at the differences between a ASP.Net WebService and a WCF service. The biggest difference between them is probably that a WebService only can be activated via HTTP and they are tightly coupled with ASP.Net HTTP pipeline. A WCF service on the other hand can be bound to a large variety of network protocols. It also doesn&amp;rsquo;t require that it is hosted on a web server, it can be hosted in a console application, in a Windows Forms app, or in a WPF application. However WCF does have an ASP.Net compatibility mode setting to enable WCF services to be configured like a web service and also mimic their behavior.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Data representation&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A WebService relies on the XmlSerializer to serialize types into XML and back to a .Net type. The following is a list of the things XmlSerializer can serialize to and from XML.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Only the public properties (or fields) of an object. &lt;/li&gt;
&lt;li&gt;Collections can be serialized only if the implement the IEnumerable or ICollection interfaces. &lt;/li&gt;
&lt;li&gt;Classes that implements the IDictionary interface, such as Hashtable, cannot be serialized into XML. &lt;/li&gt;
&lt;li&gt;You can use attributes from the System.Xml.Serialization namespace on your own classes and members to control how instances of that class will be serialized. &lt;/li&gt;
&lt;li&gt;You can also generate classes that can be serialized into XML from definitions of the types in XML Schema using the &lt;em&gt;xsd.exe&lt;/em&gt; command line tool. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Point 4 and 5 describes how you can define complex types (classes) that can be serialized to and from XML.&lt;/p&gt;
&lt;p&gt;In WCF you would also usually begin with the definition of complex types which is done adding the &lt;em&gt;DataContractAttribute&lt;/em&gt; and the &lt;em&gt;DataMemberAttribute&lt;/em&gt; to the class and its members, as described in my &lt;a href="http://msmvps.com/blogs/joacim/archive/2009/09/15/at-your-service-part-i-wcf-tutorial-in-vb.aspx"&gt;earlier post&lt;/a&gt;. The &lt;em&gt;DataContractAttribute&lt;/em&gt; can be used on classes or structures and the &lt;em&gt;DataMemberAttribute&lt;/em&gt; on properties and/or fields, which can be either private or public.&lt;/p&gt;
&lt;p&gt;A few important differences between the &lt;em&gt;DataContractSerializer &lt;/em&gt;and the &lt;em&gt;XmlSerializer &lt;/em&gt;are:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &lt;em&gt;XmlSerializer&lt;/em&gt; serialize all public fields and properties, while you have better control using the &lt;em&gt;DataContractSerializer.&lt;/em&gt; &lt;/li&gt;
&lt;li&gt;The &lt;em&gt;DataContractSerializer&lt;/em&gt; has better performance over &lt;em&gt;XmlSerializer&lt;/em&gt;. &lt;/li&gt;
&lt;li&gt;The &lt;em&gt;DataContractSerializer&lt;/em&gt; can serialize Hashtables. &lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;&lt;strong&gt;Service Development&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;To develop a WebService you would normally decorate a class with the &lt;em&gt;WebServiceAttribute &lt;/em&gt;and each method with the &lt;em&gt;WebMethodAttribute&lt;/em&gt;. This is however not an optimal solution since it does not constitute a contract for the operations performed by the service. ASP.Net 2.0 tried to rectify that by allowing you to use those attributes on an interface as well. That is the preferred method of doing it since the service contract can be reused with various classes that can implement that interface.&lt;/p&gt;
&lt;p&gt;A WCF service requires you to create a contract, since the &lt;em&gt;ServiceContractAttribute&lt;/em&gt; only can be used on an interface. So a WebService allows you to constitute a contract but doesn&amp;rsquo;t enforce it while a WCF service do.&lt;/p&gt;
&lt;p&gt;A WebService only incorpartes &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.basichttpbinding.aspx"&gt;BasicHttpBinding&lt;/a&gt; while a WCF service can use a wide range of bindings, such as &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.wshttpbinding.aspx"&gt;WSHttpBinding&lt;/a&gt;, &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.nettcpbinding.aspx"&gt;NetTcpBinding&lt;/a&gt;, &lt;a target="_blank" href="http://msdn.microsoft.com/en-us/library/system.servicemodel.netnamedpipebinding.aspx"&gt;NetNamedPipeBinding&lt;/a&gt;, amongst others.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Hosting the service&lt;/strong&gt;&amp;nbsp; &lt;/p&gt;
&lt;p&gt;ASP.Net web services are hosted on a Web Server. A WCF service can be hosted on a Web Server but can just as well be hosted by a Console- or Windows Forms application.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I don&amp;rsquo;t want to make any conclusions about which technique is the best to use, because that depends on the specification. A WebService is usually easier to develop but are somewhat limited. A WCF service is more versatile but can be harder to develop. To make a true comparison you should really compare the two techniques if you want to host the service on a Web Server, such as IIS, since if you want to use another host you can&amp;rsquo;t use an ASP.Net WebService. I just leave the judgment up to you.&lt;/p&gt;
&lt;p&gt;Have fun!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1723827" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/wcf/default.aspx">wcf</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/asp.net/default.aspx">asp.net</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/webservice/default.aspx">webservice</category></item><item><title>GetType and TypeOf confusion</title><link>http://msmvps.com/blogs/joacim/archive/2009/08/31/gettype-and-typeof-confusion.aspx</link><pubDate>Mon, 31 Aug 2009 05:18:46 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1719641</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>4</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1719641</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2009/08/31/gettype-and-typeof-confusion.aspx#comments</comments><description>&lt;p&gt;Both VB and C# have an operator called &lt;em&gt;&lt;font color="#0000ff"&gt;TypeOf&lt;/font&gt;&lt;/em&gt; (or &lt;em&gt;&lt;font color="#0000ff"&gt;typeof&lt;/font&gt;&lt;/em&gt; in C#) but they perform two completely different things.&lt;/p&gt;  &lt;p&gt;In VB there are also two kind of &lt;em&gt;GetType&lt;/em&gt;() calls, the &lt;em&gt;object&lt;/em&gt;.GetType() method which is part of the .Net framework and the VB language specific &lt;font color="#0000ff"&gt;GetType&lt;/font&gt;() &lt;em&gt;operator&lt;/em&gt;.&lt;/p&gt;  &lt;p&gt;Are you confused yet? Don’t worry, in this article I will try to explain the difference between these operators and the &lt;em&gt;object&lt;/em&gt;.GetType() method.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;The TypeOf, typeof, GetType operators&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;The VB &lt;font color="#0000ff"&gt;TypeOf&lt;/font&gt; operator is used together with the &lt;font color="#0000ff"&gt;Is&lt;/font&gt; keyword and is used for checking if an object is of a particular type.&lt;/p&gt;  &lt;pre class="code"&gt;result = &lt;span style="color:blue;"&gt;TypeOf &lt;/span&gt;x &lt;span style="color:blue;"&gt;Is String&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;If “x” above is a string then “result” would be True otherwise it is set to False. This operator have existed in VB since long before .Net was born. The equivalent for the &lt;font color="#0000ff"&gt;TypeOf &lt;/font&gt;operator in C# is simply called the &lt;font color="#0000ff"&gt;is&lt;/font&gt; operator.&lt;/p&gt;

&lt;pre class="code"&gt;result = x &lt;span style="color:blue;"&gt;is string&lt;/span&gt;;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;The &lt;font color="#0000ff"&gt;typeof&lt;/font&gt; operator in C# on the other hand returns an instance of the System.Type class containing type declarations of the type you pass to it.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:#2b91af;"&gt;Type &lt;/span&gt;t = &lt;span style="color:blue;"&gt;typeof&lt;/span&gt;(&lt;span style="color:blue;"&gt;string&lt;/span&gt;);&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The VB equivalent of the C# typeof operator is the GetType operator.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;t &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = &lt;span style="color:blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;The reason for the different names are simply because of the fact that TypeOf was already a reserved keyword in VB and I guess the C# team didn’t have VB in their mind when they designed C#. :)&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The difference between Object.GetType and the GetType operator&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;On a trivial level, the Object.GetType() method operates on an object instance while the GetType (and C#’s typeof) operator operates on a type.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;s &lt;span style="color:blue;"&gt;As String
Dim &lt;/span&gt;t &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = s.GetType()
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;t2 &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = &lt;span style="color:blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color:blue;"&gt;String&lt;/span&gt;)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;There are no differences between “t” and “t2” in the above code. So why do we have to have both of them? Well, you might not know what type a certain reference is made of in which case you obviously can’t use the GetType operator since that requires that you pass the type. Have a look at the following example:&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Public Class &lt;/span&gt;MyBaseClass
&lt;span style="color:blue;"&gt;End Class

Public Class &lt;/span&gt;MyDerivedClass
  &lt;span style="color:blue;"&gt;Inherits &lt;/span&gt;MyBaseClass
&lt;span style="color:blue;"&gt;End Class

Module &lt;/span&gt;Test
  &lt;span style="color:blue;"&gt;Public Sub &lt;/span&gt;ShowType(&lt;span style="color:blue;"&gt;ByVal &lt;/span&gt;obj &lt;span style="color:blue;"&gt;As &lt;/span&gt;MyBaseClass)
    &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;t &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = obj.GetType()
    &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;t2 &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = &lt;span style="color:blue;"&gt;GetType&lt;/span&gt;(MyBaseClass)
    Console.WriteLine(t)
    Console.WriteLine(t2)
  &lt;span style="color:blue;"&gt;End Sub

  Public Sub &lt;/span&gt;Main()
    &lt;span style="color:blue;"&gt;Dim &lt;/span&gt;myObject &lt;span style="color:blue;"&gt;As New &lt;/span&gt;MyDerivedClass
    ShowType(myObject)
    Console.ReadLine()
  &lt;span style="color:blue;"&gt;End Sub
End Module&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;In this example the &lt;em&gt;ShowType&lt;/em&gt; method takes a &lt;em&gt;MyBaseClass&lt;/em&gt; parameter and &lt;em&gt;t2&lt;/em&gt; uses the GetType operator to get the System.Type representation of &lt;em&gt;MyBaseClass.&lt;/em&gt; So when you write out &lt;em&gt;t2&lt;/em&gt; to the console it will write &lt;strong&gt;MyBaseClass&lt;/strong&gt;. However when you use the GetType method on the &lt;em&gt;obj&lt;/em&gt; parameter it will write &lt;strong&gt;MyDerivedClass&lt;/strong&gt; to the console. It is legal to pass a reference to &lt;em&gt;MyDerivedClass&lt;/em&gt; to the &lt;em&gt;ShowType&lt;/em&gt; method since it inherits from &lt;em&gt;MyBaseClass&lt;/em&gt; so that will be a &lt;a href="http://msmvps.com/blogs/joacim/archive/2009/08/31/boxing-and-unboxing-in-net.aspx"&gt;widening conversion&lt;/a&gt;. So inside the &lt;em&gt;ShowType &lt;/em&gt;method we will never know if the parameter contains the type we have declared it as or if it contains any subclass of it, to find out we need to use the &lt;em&gt;Object.&lt;/em&gt;GetType method.&lt;/p&gt;

&lt;p&gt;Another difference between the method and the operator is when you’re dealing with value types.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;i &lt;span style="color:blue;"&gt;As Integer
Dim &lt;/span&gt;t &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = i.GetType()
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;t2 &lt;span style="color:blue;"&gt;As &lt;/span&gt;Type = &lt;span style="color:blue;"&gt;GetType&lt;/span&gt;(&lt;span style="color:blue;"&gt;Integer&lt;/span&gt;)&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;In order to call &lt;em&gt;i.GetType()&lt;/em&gt; above &lt;em&gt;i&lt;/em&gt; must first be converted to an Object which means a &lt;a href="http://msmvps.com/blogs/joacim/archive/2009/08/31/boxing-and-unboxing-in-net.aspx"&gt;boxing conversion&lt;/a&gt; has to be done. Using the operator no boxing conversions are made which in this case makes that call faster.&lt;/p&gt;

&lt;p&gt;I hope this removes some of the confusion about this subject, if not please feel free to leave a comment.&lt;/p&gt;

&lt;p&gt;Have fun!&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1719641" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/GetType/default.aspx">GetType</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/TypeOf/default.aspx">TypeOf</category></item><item><title>Boxing and Unboxing in .Net</title><link>http://msmvps.com/blogs/joacim/archive/2009/08/31/boxing-and-unboxing-in-net.aspx</link><pubDate>Mon, 31 Aug 2009 01:41:32 GMT</pubDate><guid isPermaLink="false">d67277c4-116b-43f1-b688-e9ef184ea916:1719615</guid><dc:creator>Joacim Andersson</dc:creator><slash:comments>9</slash:comments><wfw:commentRss xmlns:wfw="http://wellformedweb.org/CommentAPI/">http://msmvps.com/blogs/joacim/rsscomments.aspx?PostID=1719615</wfw:commentRss><comments>http://msmvps.com/blogs/joacim/archive/2009/08/31/boxing-and-unboxing-in-net.aspx#comments</comments><description>&lt;p&gt;In this article I will try to explain the concepts of Boxing and Unboxing. There are two types of objects within the .Net framework, &lt;em&gt;value types&lt;/em&gt; and &lt;em&gt;reference types&lt;/em&gt;. Value types are stored on the &lt;a href="http://en.wikipedia.org/wiki/Call_stack" target="_blank"&gt;stack&lt;/a&gt; while reference types are always stored on the &lt;a href="http://en.wikipedia.org/wiki/Dynamic_memory_allocation" target="_blank"&gt;heap&lt;/a&gt;. Converting a value type into a reference type is called Boxing while converting it back from a reference type to a value type is called Unboxing.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Value Types&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Value types are primitive types like System.Int32 (Integer in VB, int in C#) or System.Double. These types are mapped directly to the Framework Class Library (FCL) and are all derived from System.ValueType. Apart from the regular primitive types (integer, long, single, double, char, and so on) Enumerations and Structures also inherits from System.ValueTypes so they are also stored on the stack.&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Reference Types&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;All classes are reference types and they are stored on the heap. So when you create a new instance of a class using the &lt;font color="#000080"&gt;new&lt;/font&gt; keyword, memory are allocated on the heap for the object and the memory address is returned. Strings are a special case since they are also reference types, even though they are often treated as if they where value types, but that’s not the case. The difference is that you don’t have to create an instance of a string in the same manner as you would with another class.&lt;/p&gt;  &lt;p&gt;Reference types are also Nullable, meaning that you can set the reference to Null (Nothing in VB).&lt;/p&gt;  &lt;p&gt;&lt;strong&gt;Widening and Narrowing conversions&lt;/strong&gt;&lt;/p&gt;  &lt;p&gt;Before we go into the Boxing and Unboxing I would like to take a moment to explain the difference between a widening and a narrowing conversion. A widening conversion is always safe since there is never any risk of any data loss. For example converting an Integer into a Long (System.Int64) is a widening conversion. There is no integer value that doesn’t fit into a Long so you can always do that. However doing the opposite, converting a Long into an Integer is more risky since there are a chance that you will lose some data, since a Long can contain a number that is too high (or too low) to fit into an integer.&lt;/p&gt;  &lt;p&gt;You can always use an implicit conversion when you do a widening conversion.&lt;/p&gt;  &lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;&lt;span style="color:black;"&gt;i &lt;/span&gt;&lt;span style="color:blue;"&gt;As Integer &lt;/span&gt;&lt;span style="color:red;"&gt;= 3
&lt;/span&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;&lt;span style="color:black;"&gt;l &lt;/span&gt;&lt;span style="color:blue;"&gt;As Long
&lt;/span&gt;&lt;span style="color:black;"&gt;l &lt;/span&gt;&lt;span style="color:red;"&gt;= &lt;/span&gt;&lt;span style="color:black;"&gt;i &lt;/span&gt;&lt;span style="color:green;"&gt;&amp;#39;Implicit conversion
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;However to do the opposite, a narrowing conversion, you need to do an explicit conversion.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:black;"&gt;i &lt;/span&gt;&lt;span style="color:red;"&gt;= &lt;/span&gt;&lt;span style="color:blue;"&gt;CType&lt;/span&gt;&lt;span style="color:black;"&gt;(l, &lt;/span&gt;&lt;span style="color:blue;"&gt;Integer&lt;/span&gt;&lt;span style="color:black;"&gt;) &lt;/span&gt;&lt;span style="color:green;"&gt;&amp;#39;Explicit conversion&lt;br /&gt;&amp;#39;or
&lt;/span&gt;&lt;span style="color:black;"&gt;i &lt;/span&gt;&lt;span style="color:red;"&gt;= &lt;/span&gt;&lt;span style="color:blue;"&gt;CInt&lt;/span&gt;&lt;span style="color:black;"&gt;(l) &lt;/span&gt;&lt;span style="color:green;"&gt;&amp;#39;Explicit conversion
&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;If you don’t do an explicit conversion when you’re doing a narrowing conversion you will get a compilation error so your project will not compile (Note, VB can allow you to do an implicit conversion if you have Option Strict set to Off, something I strongly advice you not to have, C# however will never allow it). If the Long value does not fit into the Integer you will however still get a runtime error since the system will then throw a System.OverflowExeption. So when you do a narrowing conversion you must be sure that it can be done.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Doing Boxing/Unboxing&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As mentioned earlier Boxing refers to the conversion of a value type to a reference type.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;&lt;span style="color:black;"&gt;i &lt;/span&gt;&lt;span style="color:blue;"&gt;As Integer &lt;/span&gt;&lt;span style="color:red;"&gt;= 3
&lt;/span&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;&lt;span style="color:black;"&gt;o &lt;/span&gt;&lt;span style="color:blue;"&gt;As Object
&lt;/span&gt;&lt;span style="color:black;"&gt;o &lt;/span&gt;&lt;span style="color:red;"&gt;= &lt;/span&gt;&lt;span style="color:black;"&gt;i &lt;/span&gt;&lt;span style="color:green;"&gt;&amp;#39;Boxing the Integer into an object (reference type)&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;Above I do an implicit conversion of an integer to an object. This is possible since it’s a widening conversion so there is no risk of any data loss. However doing the opposite, converting the object to an integer is a narrowing conversion, where there is a risk that data will be lost.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:black;"&gt;i &lt;/span&gt;&lt;span style="color:red;"&gt;= &lt;/span&gt;&lt;span style="color:black;"&gt;o &lt;/span&gt;&lt;span style="color:green;"&gt;&amp;#39;Implicitly doing a narrowing conversion (not allowed)
&lt;/span&gt;&lt;span style="color:black;"&gt;i &lt;/span&gt;&lt;span style="color:red;"&gt;= &lt;/span&gt;&lt;span style="color:blue;"&gt;CInt&lt;/span&gt;&lt;span style="color:black;"&gt;(o) &lt;/span&gt;&lt;span style="color:green;"&gt;&amp;#39;Explicitly doing a narrowing conversion (allowed)&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;In C# if you have boxed a particular type you must unbox it to the same type. You can not box an integer to an object and then unbox it to a long.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;int &lt;/span&gt;i = 3;
&lt;span style="color:blue;"&gt;long &lt;/span&gt;l;
&lt;span style="color:blue;"&gt;object &lt;/span&gt;o;
o = i;
l = (&lt;span style="color:blue;"&gt;long&lt;/span&gt;)o; &lt;span style="color:green;"&gt;//will throw a System.InvalidCastException&lt;/span&gt;&lt;/pre&gt;

&lt;p&gt;&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;For the above to work, you must first unbox it to an integer and then cast it to a long.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;font color="#0000ff"&gt;int&lt;/font&gt; i = 3;
&lt;span style="color:blue;"&gt;long &lt;/span&gt;l;
&lt;span style="color:blue;"&gt;object &lt;/span&gt;o;
o = i;
l = (&lt;font color="#0000ff"&gt;long&lt;/font&gt;)(&lt;span style="color:blue;"&gt;int&lt;/span&gt;)o;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;However the VB’s type conversion functions (CType and the more specific CInt, CLng, CDbl and so on) does allow this.&lt;/p&gt;

&lt;pre class="code"&gt;&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;i &lt;span style="color:blue;"&gt;As Integer &lt;/span&gt;= 3
&lt;span style="color:blue;"&gt;Dim &lt;/span&gt;l &lt;span style="color:blue;"&gt;As Long
Dim &lt;/span&gt;o &lt;span style="color:blue;"&gt;As Object
&lt;/span&gt;o = i
l = &lt;span style="color:blue;"&gt;CLng&lt;/span&gt;(o) &lt;span style="color:green;"&gt;&amp;#39;allowed&lt;/span&gt;&lt;/pre&gt;
&lt;a href="http://11011.net/software/vspaste"&gt;&lt;/a&gt;

&lt;p&gt;&lt;strong&gt;End words&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I hope that you by now have a better understanding of Boxing and Unboxing. So let me end this article by saying that you should avoid doing this if possible. Doing type conversion is time consuming, and that is especially true when it comes to Boxing and Unboxing since data have to be transferred back and forth between the stack and the heap. Using Generics, which has existed in .Net since version 2.0, is a much better approach than having to deal with value type to reference type conversion.&lt;/p&gt;

&lt;p&gt;I will cover Generics in a later article.&lt;/p&gt;

&lt;p&gt;Have fun.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;&lt;img src="http://msmvps.com/aggbug.aspx?PostID=1719615" width="1" height="1"&gt;</description><category domain="http://msmvps.com/blogs/joacim/archive/tags/vb/default.aspx">vb</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/visual+basic/default.aspx">visual basic</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/.net/default.aspx">.net</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/boxing/default.aspx">boxing</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/widening/default.aspx">widening</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/unboxing/default.aspx">unboxing</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/conversion/default.aspx">conversion</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/c_2300_/default.aspx">c#</category><category domain="http://msmvps.com/blogs/joacim/archive/tags/narrowing/default.aspx">narrowing</category></item></channel></rss>