[{"data":1,"prerenderedAt":2587},["ShallowReactive",2],{"blog-tags-VB.NET":3},[4,95,720,1283,1415,1910],{"_path":5,"_dir":6,"_draft":7,"_partial":7,"_locale":8,"title":9,"description":10,"date":11,"category":12,"tags":13,"excerpt":18,"body":34,"_type":83,"_id":84,"_source":85,"_file":86,"_stem":87,"_extension":88,"url":89,"wordCount":90,"minutes":91,"commentCount":92,"image":93},"/blog/2009/2009-08-12-linq-to-sql-cheat-sheet","2009",false,"en","LINQ to SQL cheat sheet","","2009-08-12T12:55:33+00:00","Development",[14,15,16,17],".NET","LINQ","VB.NET","C#",{"type":19,"children":20},"root",[21,29],{"type":22,"tag":23,"props":24,"children":25},"element","p",{},[26],{"type":27,"value":28},"text","A few short words to say I’ve put together a cheat sheet for LINQ to SQL with one page for C# and another for VB.NET.",{"type":22,"tag":23,"props":30,"children":31},{},[32],{"type":27,"value":33},"It shows the syntax for a number of common query operations, manipulations and attributes and can be a very useful quick reference :)",{"type":19,"children":35,"toc":80},[36,45,49,53,71],{"type":22,"tag":37,"props":38,"children":44},"img",{"className":39,"src":42,"alt":43},[40,41],"screenshot","left","https://img.damieng.com/blog/LINQToSQLCheatSheet.png","Thumbnail of the LINQ to SQL Cheat Sheet PDF",[],{"type":22,"tag":23,"props":46,"children":47},{},[48],{"type":27,"value":28},{"type":22,"tag":23,"props":50,"children":51},{},[52],{"type":27,"value":33},{"type":22,"tag":54,"props":55,"children":56},"blockquote",{},[57],{"type":22,"tag":23,"props":58,"children":59},{},[60,62,69],{"type":27,"value":61},"Download ",{"type":22,"tag":63,"props":64,"children":66},"a",{"href":65},"https://dl.damieng.com/dotnet/LINQToSQLCheatSheet.pdf",[67],{"type":27,"value":68},"LINQ to SQL cheat sheet (PDF)",{"type":27,"value":70}," (76 KB)",{"type":22,"tag":23,"props":72,"children":73},{},[74],{"type":22,"tag":75,"props":76,"children":77},"em",{},[78],{"type":27,"value":79},"[)amien",{"title":10,"searchDepth":81,"depth":81,"links":82},2,[],"markdown","content:blog:2009:2009-08-12-linq-to-sql-cheat-sheet.md","content","blog/2009/2009-08-12-linq-to-sql-cheat-sheet.md","blog/2009/2009-08-12-linq-to-sql-cheat-sheet","md","/blog/2009/08/12/linq-to-sql-cheat-sheet/",58,1,14,{"className":94,"src":42,"alt":43},[40,41],{"_path":96,"_dir":97,"_draft":7,"_partial":7,"_locale":8,"title":98,"description":10,"date":99,"category":12,"tags":100,"excerpt":102,"body":118,"_type":83,"_id":713,"_source":85,"_file":714,"_stem":715,"_extension":88,"url":716,"wordCount":717,"minutes":718,"commentCount":719},"/blog/2008/2008-09-14-linq-to-sql-template-for-visual-studio-2008","2008","LINQ to SQL template for Visual Studio 2008","2008-09-14T23:29:51+00:00",[14,15,101,17,16],"T4",{"type":19,"children":103},[104,113],{"type":22,"tag":23,"props":105,"children":106},{},[107,109,111],{"type":27,"value":108},"A newer version of ",{"type":27,"value":110},"this LINQ to SQL template is available",{"type":27,"value":112},".",{"type":22,"tag":23,"props":114,"children":115},{},[116],{"type":27,"value":117},"If you want to customize the LINQ to SQL code generation phase in your project without additional tool dependencies this could be what you’re looking for.",{"type":19,"children":119,"toc":696},[120,133,137,241,248,355,361,366,373,386,392,397,415,421,447,453,458,464,469,475,480,486,491,497,502,508,513,526,589,615,651,657,679,684,689],{"type":22,"tag":54,"props":121,"children":122},{},[123],{"type":22,"tag":23,"props":124,"children":125},{},[126,127,132],{"type":27,"value":108},{"type":22,"tag":63,"props":128,"children":130},{"href":129},"/blog/2009/01/19/linq-to-sql-templates-updated-now-on-codeplex/",[131],{"type":27,"value":110},{"type":27,"value":112},{"type":22,"tag":23,"props":134,"children":135},{},[136],{"type":27,"value":117},{"type":22,"tag":138,"props":139,"children":140},"ul",{},[141,152,162,172,182,192,202,211,221,231],{"type":22,"tag":142,"props":143,"children":144},"li",{},[145,150],{"type":22,"tag":75,"props":146,"children":147},{},[148],{"type":27,"value":149},"11 Dec 2008",{"type":27,"value":151}," Fixes to association code with one-to-one’s and with no serialization required.",{"type":22,"tag":142,"props":153,"children":154},{},[155,160],{"type":22,"tag":75,"props":156,"children":157},{},[158],{"type":27,"value":159},"7 Dec 2008",{"type":27,"value":161}," Added IsDelayLoaded, IsUnique. Fixed association code, stored proc update overrides.",{"type":22,"tag":142,"props":163,"children":164},{},[165,170],{"type":22,"tag":75,"props":166,"children":167},{},[168],{"type":27,"value":169},"17 Nov 2008",{"type":27,"value":171}," Added IsInheritanceDefault read/emit IsDefault on type",{"type":22,"tag":142,"props":173,"children":174},{},[175,180],{"type":22,"tag":75,"props":176,"children":177},{},[178],{"type":27,"value":179},"26 Oct 2008",{"type":27,"value":181}," Emit column Name attribute if it doesn’t match the member (thanks Steele)",{"type":22,"tag":142,"props":183,"children":184},{},[185,190],{"type":22,"tag":75,"props":186,"children":187},{},[188],{"type":27,"value":189},"20 Oct 2008",{"type":27,"value":191}," Handle class naming better, fix associations for renamed or out-of-sequence keys and their access modifiers",{"type":22,"tag":142,"props":193,"children":194},{},[195,200],{"type":22,"tag":75,"props":196,"children":197},{},[198],{"type":27,"value":199},"25 Sep 2008",{"type":27,"value":201}," Figure out missing association keys by using either sides primary key",{"type":22,"tag":142,"props":203,"children":204},{},[205,209],{"type":22,"tag":75,"props":206,"children":207},{},[208],{"type":27,"value":199},{"type":27,"value":210}," Empty DBML name-space is now no namespace instead of “MyApplication”",{"type":22,"tag":142,"props":212,"children":213},{},[214,219],{"type":22,"tag":75,"props":215,"children":216},{},[217],{"type":27,"value":218},"23 Sep 2008",{"type":27,"value":220}," fixed stored procedures with 0 parameters",{"type":22,"tag":142,"props":222,"children":223},{},[224,229],{"type":22,"tag":75,"props":225,"children":226},{},[227],{"type":27,"value":228},"22 Sep 2008",{"type":27,"value":230}," fixed VB.NET IsForeignKey attribute for associations",{"type":22,"tag":142,"props":232,"children":233},{},[234,239],{"type":22,"tag":75,"props":235,"children":236},{},[237],{"type":27,"value":238},"18 Sep 2008",{"type":27,"value":240}," now generates stored procedures including insert/update/delete with concurrency checking.",{"type":22,"tag":242,"props":243,"children":245},"h2",{"id":244},"new-since-reloaded-version",[246],{"type":27,"value":247},"New since ‘reloaded’ version",{"type":22,"tag":138,"props":249,"children":250},{},[251,262,287,315,325,335,345],{"type":22,"tag":142,"props":252,"children":253},{},[254,260],{"type":22,"tag":255,"props":256,"children":257},"strong",{},[258],{"type":27,"value":259},"Inheritance",{"type":27,"value":261},": generates sub-classes with all properties and code mappings.",{"type":22,"tag":142,"props":263,"children":264},{},[265,269,271,278,280,286],{"type":22,"tag":255,"props":266,"children":267},{},[268],{"type":27,"value":16},{"type":27,"value":270},": ",{"type":22,"tag":272,"props":273,"children":275},"code",{"className":274},[],[276],{"type":27,"value":277},"CSharpDataContext.tt",{"type":27,"value":279}," is joined by a VB.NET emitting ",{"type":22,"tag":272,"props":281,"children":283},{"className":282},[],[284],{"type":27,"value":285},"VBNetDataContext.tt",{"type":27,"value":112},{"type":22,"tag":142,"props":288,"children":289},{},[290,295,297,305,307,314],{"type":22,"tag":255,"props":291,"children":292},{},[293],{"type":27,"value":294},"DataContract SP1 –",{"type":27,"value":296}," additional mode to emit SP1-compatible ",{"type":22,"tag":63,"props":298,"children":302},{"href":299,"rel":300},"https://msdn.microsoft.com/en-us/library/ms733127.aspx",[301],"nofollow",[303],{"type":27,"value":304},"DataContract serialization",{"type":27,"value":306}," via ",{"type":22,"tag":63,"props":308,"children":311},{"href":309,"rel":310},"https://oakleafblog.blogspot.com/2008/07/bidirectional-serialization-of-linq-to.html",[301],[312],{"type":27,"value":313},"Roger Jennings",{"type":27,"value":112},{"type":22,"tag":142,"props":316,"children":317},{},[318,323],{"type":22,"tag":255,"props":319,"children":320},{},[321],{"type":27,"value":322},"Composite keys",{"type":27,"value":324},": both as the primary key and as a foreign key in an association.",{"type":22,"tag":142,"props":326,"children":327},{},[328,333],{"type":22,"tag":255,"props":329,"children":330},{},[331],{"type":27,"value":332},"Type attributes",{"type":27,"value":334},": the data context and entity types can now be sealed or abstract as well as public, private, protected, internal or protected internal.",{"type":22,"tag":142,"props":336,"children":337},{},[338,343],{"type":22,"tag":255,"props":339,"children":340},{},[341],{"type":27,"value":342},"Associations",{"type":27,"value":344},": prevents foreign key values changing once the object association is made and updates parent side of one-to-many associations.",{"type":22,"tag":142,"props":346,"children":347},{},[348,353],{"type":22,"tag":255,"props":349,"children":350},{},[351],{"type":27,"value":352},"Stored procedures",{"type":27,"value":354},": generates method wrappers and associated methods to facilitate insert/update/delete with concurrency support.",{"type":22,"tag":242,"props":356,"children":358},{"id":357},"functionality-compared-to-designer",[359],{"type":27,"value":360},"Functionality compared to designer",{"type":22,"tag":23,"props":362,"children":363},{},[364],{"type":27,"value":365},"A primary goal in developing the template was to allow for easy switching between the template and the LINQ to SQL designer so things are very similar.",{"type":22,"tag":367,"props":368,"children":370},"h3",{"id":369},"missing",[371],{"type":27,"value":372},"Missing",{"type":22,"tag":138,"props":374,"children":375},{},[376,381],{"type":22,"tag":142,"props":377,"children":378},{},[379],{"type":27,"value":380},"Comprehensive sanity checking on the DBML.",{"type":22,"tag":142,"props":382,"children":383},{},[384],{"type":27,"value":385},"The Custom Tool Namespace and project namespaces are not pulled in when the DBML namespaces not specified.",{"type":22,"tag":367,"props":387,"children":389},{"id":388},"fixed",[390],{"type":27,"value":391},"Fixed",{"type":22,"tag":23,"props":393,"children":394},{},[395],{"type":27,"value":396},"The designer has a few bugs which helpfully this template doesn’t suffer from.",{"type":22,"tag":138,"props":398,"children":399},{},[400,405,410],{"type":22,"tag":142,"props":401,"children":402},{},[403],{"type":27,"value":404},"Modifying a table via a stored procedure using original values for concurrency will throw ChangeConflictException and not silently fail.",{"type":22,"tag":142,"props":406,"children":407},{},[408],{"type":27,"value":409},"Protected internal virtual property doesn’t forget to be virtual.",{"type":22,"tag":142,"props":411,"children":412},{},[413],{"type":27,"value":414},"Checks all associations based on a foreign key are not loaded before allowing change and not just the first one.",{"type":22,"tag":367,"props":416,"children":418},{"id":417},"improved",[419],{"type":27,"value":420},"Improved",{"type":22,"tag":138,"props":422,"children":423},{},[424,429,442],{"type":22,"tag":142,"props":425,"children":426},{},[427],{"type":27,"value":428},"Fully customizable with full source.",{"type":22,"tag":142,"props":430,"children":431},{},[432,434,440],{"type":27,"value":433},"Serialization mode to support DataContract improvements in .NET 3.5 SP1\nTo use uncomment the line ",{"type":22,"tag":272,"props":435,"children":437},{"className":436},[],[438],{"type":27,"value":439},"// data.Serialization = SerializationMode.DataContractSP1;",{"type":27,"value":441}," in xDataClasses.tt",{"type":22,"tag":142,"props":443,"children":444},{},[445],{"type":27,"value":446},"CanBeNull attribute generated for value types (useful when working with metadata).",{"type":22,"tag":242,"props":448,"children":450},{"id":449},"source-compared-to-designer",[451],{"type":27,"value":452},"Source compared to designer",{"type":22,"tag":23,"props":454,"children":455},{},[456],{"type":27,"value":457},"The designer generated code can be difficult to read and isn’t well suited to template generation so the output from this template is different in a number of ways:",{"type":22,"tag":367,"props":459,"children":461},{"id":460},"sequence",[462],{"type":27,"value":463},"Sequence",{"type":22,"tag":23,"props":465,"children":466},{},[467],{"type":27,"value":468},"Everything related to a column mapping (the storage variable, event signatures, attribute and the property itself) is batched together so it can be hit with a single loop making the template shorter and easier to work with.",{"type":22,"tag":367,"props":470,"children":472},{"id":471},"regions",[473],{"type":27,"value":474},"#regions",{"type":22,"tag":23,"props":476,"children":477},{},[478],{"type":27,"value":479},"Opinion may be divided on the usefulness of #regions in your own code but for code generation of large files I found it invaluable. There are regions for the logical parts of the data context and within each entity such as construction, column mapping, associations and serialization.",{"type":22,"tag":367,"props":481,"children":483},{"id":482},"style",[484],{"type":27,"value":485},"Style",{"type":22,"tag":23,"props":487,"children":488},{},[489],{"type":27,"value":490},"The code generated should be a little easier to follow, with if/else ordering, no redundant casts or extra brackets etc.",{"type":22,"tag":367,"props":492,"children":494},{"id":493},"namespace",[495],{"type":27,"value":496},"Namespace",{"type":22,"tag":23,"props":498,"children":499},{},[500],{"type":27,"value":501},"I don’t believe adding “this” everywhere or fully qualifying attributes and exceptions makes things easy to read. I realize this might cause some name conflicts for some people but it is easy to change yourself and means the code is shorter and easier to work with for the majority.",{"type":22,"tag":242,"props":503,"children":505},{"id":504},"getting-started",[506],{"type":27,"value":507},"Getting started",{"type":22,"tag":23,"props":509,"children":510},{},[511],{"type":27,"value":512},"Although I work on the LINQ to SQL team this template should be treated as a third-party sample and is not supported by Microsoft.",{"type":22,"tag":54,"props":514,"children":515},{},[516],{"type":22,"tag":23,"props":517,"children":518},{},[519,520],{"type":27,"value":61},{"type":22,"tag":63,"props":521,"children":523},{"href":522},"https://l2st4.codeplex.com/SourceControl/ListDownloadableCommits.aspx",[524],{"type":27,"value":525},"via CodePlex",{"type":22,"tag":527,"props":528,"children":529},"ol",{},[530,557,570],{"type":22,"tag":142,"props":531,"children":532},{},[533,535,541,543,548,550,555],{"type":27,"value":534},"Add the ",{"type":22,"tag":272,"props":536,"children":538},{"className":537},[],[539],{"type":27,"value":540},"L2ST4.ttinclude",{"type":27,"value":542}," and either ",{"type":22,"tag":272,"props":544,"children":546},{"className":545},[],[547],{"type":27,"value":277},{"type":27,"value":549}," or ",{"type":22,"tag":272,"props":551,"children":553},{"className":552},[],[554],{"type":27,"value":285},{"type":27,"value":556}," to your project depending on your language type",{"type":22,"tag":142,"props":558,"children":559},{},[560,562,568],{"type":27,"value":561},"Rename the ",{"type":22,"tag":272,"props":563,"children":565},{"className":564},[],[566],{"type":27,"value":567},"xDataContext.tt",{"type":27,"value":569}," to match your DBML file but with .tt extension instead of .dbml",{"type":22,"tag":142,"props":571,"children":572},{},[573,575,580,582,587],{"type":27,"value":574},"Set the existing DBML file’s .designer.cs/vb ",{"type":22,"tag":75,"props":576,"children":577},{},[578],{"type":27,"value":579},"Build Action",{"type":27,"value":581}," property to ",{"type":22,"tag":75,"props":583,"children":584},{},[585],{"type":27,"value":586},"None",{"type":27,"value":588}," to ignore the LINQ to SQL built-in code generation",{"type":22,"tag":23,"props":590,"children":591},{},[592,594,599,601,606,608,613],{"type":27,"value":593},"Note that the template will only regenerate when ",{"type":22,"tag":255,"props":595,"children":596},{},[597],{"type":27,"value":598},"it",{"type":27,"value":600}," has been changed so use ",{"type":22,"tag":75,"props":602,"children":603},{},[604],{"type":27,"value":605},"Run Custom Tool",{"type":27,"value":607}," from the template’s right-mouse button menu in ",{"type":22,"tag":75,"props":609,"children":610},{},[611],{"type":27,"value":612},"Solution Explorer",{"type":27,"value":614}," when you’ve changed the DBML.",{"type":22,"tag":23,"props":616,"children":617},{},[618,620,624,626,631,633,637,638,642,644,649],{"type":27,"value":619},"Should you wish to switch back to using the designer code then set the DBML file’s .designer.cs/vb ",{"type":22,"tag":75,"props":621,"children":622},{},[623],{"type":27,"value":579},{"type":27,"value":625}," to ",{"type":22,"tag":75,"props":627,"children":628},{},[629],{"type":27,"value":630},"Compile",{"type":27,"value":632}," and either remove the .tt and .ttinclude file for permanent removal or just set the .generated.cs/vb ",{"type":22,"tag":75,"props":634,"children":635},{},[636],{"type":27,"value":579},{"type":27,"value":625},{"type":22,"tag":75,"props":639,"children":640},{},[641],{"type":27,"value":586},{"type":27,"value":643}," to keep it around. VB.NET users will need to use ",{"type":22,"tag":75,"props":645,"children":646},{},[647],{"type":27,"value":648},"Show All Files",{"type":27,"value":650}," to see the .designer.vb file.",{"type":22,"tag":242,"props":652,"children":654},{"id":653},"customization",[655],{"type":27,"value":656},"Customization",{"type":22,"tag":23,"props":658,"children":659},{},[660,662,669,671,678],{"type":27,"value":661},"The template run-time built into Visual Studio 2008 is called T4 and requires no additional tools however if you do a lot of editing you might want to install the ",{"type":22,"tag":63,"props":663,"children":666},{"href":664,"rel":665},"https://www.t4editor.net/",[301],[667],{"type":27,"value":668},"Clarius T4 Editor",{"type":27,"value":670}," for syntax highlighting and also check out the treasure trove of T4 material that is ",{"type":22,"tag":63,"props":672,"children":675},{"href":673,"rel":674},"https://www.olegsych.com",[301],[676],{"type":27,"value":677},"Oleg Sych’s blog",{"type":27,"value":112},{"type":22,"tag":23,"props":680,"children":681},{},[682],{"type":27,"value":683},"The template is simple to follow, it loads the DBML file as an XML document then uses LINQ to XML to instantiate wrapper objects over the elements. This gives you a simple way to change default naming and behavior while making the template simpler to work with.",{"type":22,"tag":23,"props":685,"children":686},{},[687],{"type":27,"value":688},"Let me know how you get on by leaving a comment here.",{"type":22,"tag":23,"props":690,"children":691},{},[692],{"type":22,"tag":75,"props":693,"children":694},{},[695],{"type":27,"value":79},{"title":10,"searchDepth":81,"depth":81,"links":697},[698,699,705,711,712],{"id":244,"depth":81,"text":247},{"id":357,"depth":81,"text":360,"children":700},[701,703,704],{"id":369,"depth":702,"text":372},3,{"id":388,"depth":702,"text":391},{"id":417,"depth":702,"text":420},{"id":449,"depth":81,"text":452,"children":706},[707,708,709,710],{"id":460,"depth":702,"text":463},{"id":471,"depth":702,"text":474},{"id":482,"depth":702,"text":485},{"id":493,"depth":702,"text":496},{"id":504,"depth":81,"text":507},{"id":653,"depth":81,"text":656},"content:blog:2008:2008-09-14-linq-to-sql-template-for-visual-studio-2008.md","blog/2008/2008-09-14-linq-to-sql-template-for-visual-studio-2008.md","blog/2008/2008-09-14-linq-to-sql-template-for-visual-studio-2008","/blog/2008/09/14/linq-to-sql-template-for-visual-studio-2008/",907,5,24,{"_path":721,"_dir":722,"_draft":7,"_partial":7,"_locale":8,"title":723,"description":724,"date":725,"category":12,"tags":726,"excerpt":727,"body":738,"_type":83,"_id":1277,"_source":85,"_file":1278,"_stem":1279,"_extension":88,"url":1280,"wordCount":1281,"minutes":81,"commentCount":1282},"/blog/2007/2007-11-04-object-initializers-in-net-35","2007","Object Initializers in .NET 3.5","One compiler improvement in .NET 3.5 is the object initializers feature that lets you concisely set properties of an object as you create it.","2007-11-04T01:17:53+00:00",[14,17,16],{"type":19,"children":728},[729,733],{"type":22,"tag":23,"props":730,"children":731},{},[732],{"type":27,"value":724},{"type":22,"tag":23,"props":734,"children":735},{},[736],{"type":27,"value":737},"If you’ve ever used VB.NET you may well have found and enjoyed the with keyword to write code such as:",{"type":19,"children":739,"toc":1275},[740,744,748,911,916,994,999,1004,1009,1088,1093,1258,1263,1270],{"type":22,"tag":23,"props":741,"children":742},{},[743],{"type":27,"value":724},{"type":22,"tag":23,"props":745,"children":746},{},[747],{"type":27,"value":737},{"type":22,"tag":749,"props":750,"children":754},"pre",{"className":751,"code":752,"language":753,"meta":10,"style":10},"language-vb shiki shiki-themes everforest-light dracula","Dim myObj As MyClass\nmyObj = New MyClass()\nWith myObj\n  .ProductCode = \"ABC123\"\n  .Quantity = 5\n  .Cost = 567.89\nEnd With\n","vb",[755],{"type":22,"tag":272,"props":756,"children":757},{"__ignoreMap":10},[758,787,816,830,860,878,896],{"type":22,"tag":759,"props":760,"children":762},"span",{"class":761,"line":91},"line",[763,769,775,781],{"type":22,"tag":759,"props":764,"children":766},{"style":765},"--shiki-default:#3A94C5;--shiki-dark:#FF79C6",[767],{"type":27,"value":768},"Dim",{"type":22,"tag":759,"props":770,"children":772},{"style":771},"--shiki-default:#5C6A72;--shiki-dark:#F8F8F2",[773],{"type":27,"value":774}," myObj ",{"type":22,"tag":759,"props":776,"children":778},{"style":777},"--shiki-default:#F57D26;--shiki-dark:#FF79C6",[779],{"type":27,"value":780},"As",{"type":22,"tag":759,"props":782,"children":784},{"style":783},"--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic",[785],{"type":27,"value":786}," MyClass\n",{"type":22,"tag":759,"props":788,"children":789},{"class":761,"line":81},[790,795,800,805,811],{"type":22,"tag":759,"props":791,"children":792},{"style":771},[793],{"type":27,"value":794},"myObj ",{"type":22,"tag":759,"props":796,"children":797},{"style":777},[798],{"type":27,"value":799},"=",{"type":22,"tag":759,"props":801,"children":802},{"style":765},[803],{"type":27,"value":804}," New ",{"type":22,"tag":759,"props":806,"children":808},{"style":807},"--shiki-default:#8DA101;--shiki-dark:#50FA7B",[809],{"type":27,"value":810},"MyClass",{"type":22,"tag":759,"props":812,"children":813},{"style":771},[814],{"type":27,"value":815},"()\n",{"type":22,"tag":759,"props":817,"children":818},{"class":761,"line":702},[819,825],{"type":22,"tag":759,"props":820,"children":822},{"style":821},"--shiki-default:#F85552;--shiki-dark:#FF79C6",[823],{"type":27,"value":824},"With",{"type":22,"tag":759,"props":826,"children":827},{"style":771},[828],{"type":27,"value":829}," myObj\n",{"type":22,"tag":759,"props":831,"children":833},{"class":761,"line":832},4,[834,839,843,849,855],{"type":22,"tag":759,"props":835,"children":836},{"style":771},[837],{"type":27,"value":838},"  .ProductCode ",{"type":22,"tag":759,"props":840,"children":841},{"style":777},[842],{"type":27,"value":799},{"type":22,"tag":759,"props":844,"children":846},{"style":845},"--shiki-default:#DFA000;--shiki-dark:#E9F284",[847],{"type":27,"value":848}," \"",{"type":22,"tag":759,"props":850,"children":852},{"style":851},"--shiki-default:#DFA000;--shiki-dark:#F1FA8C",[853],{"type":27,"value":854},"ABC123",{"type":22,"tag":759,"props":856,"children":857},{"style":845},[858],{"type":27,"value":859},"\"\n",{"type":22,"tag":759,"props":861,"children":862},{"class":761,"line":718},[863,868,872],{"type":22,"tag":759,"props":864,"children":865},{"style":771},[866],{"type":27,"value":867},"  .Quantity ",{"type":22,"tag":759,"props":869,"children":870},{"style":777},[871],{"type":27,"value":799},{"type":22,"tag":759,"props":873,"children":875},{"style":874},"--shiki-default:#DF69BA;--shiki-dark:#BD93F9",[876],{"type":27,"value":877}," 5\n",{"type":22,"tag":759,"props":879,"children":881},{"class":761,"line":880},6,[882,887,891],{"type":22,"tag":759,"props":883,"children":884},{"style":771},[885],{"type":27,"value":886},"  .Cost ",{"type":22,"tag":759,"props":888,"children":889},{"style":777},[890],{"type":27,"value":799},{"type":22,"tag":759,"props":892,"children":893},{"style":874},[894],{"type":27,"value":895}," 567.89\n",{"type":22,"tag":759,"props":897,"children":899},{"class":761,"line":898},7,[900,906],{"type":22,"tag":759,"props":901,"children":903},{"style":902},"--shiki-default:#8DA101;--shiki-dark:#8BE9FD",[904],{"type":27,"value":905},"End",{"type":22,"tag":759,"props":907,"children":908},{"style":821},[909],{"type":27,"value":910}," With\n",{"type":22,"tag":23,"props":912,"children":913},{},[914],{"type":27,"value":915},"This is more concise than writing myObj several times over, especially if setting a large number of properties, but as C# has no such keyword many people resorted to providing helpful constructors to facilitate code like:",{"type":22,"tag":749,"props":917,"children":921},{"className":918,"code":919,"language":920,"meta":10,"style":10},"language-csharp shiki shiki-themes everforest-light dracula","MyClass myObj = new MyClass(\"ABC123\", 5, 567.89);\n","csharp",[922],{"type":22,"tag":272,"props":923,"children":924},{"__ignoreMap":10},[925],{"type":22,"tag":759,"props":926,"children":927},{"class":761,"line":91},[928,932,936,940,945,950,955,961,966,970,975,980,984,989],{"type":22,"tag":759,"props":929,"children":930},{"style":783},[931],{"type":27,"value":810},{"type":22,"tag":759,"props":933,"children":934},{"style":771},[935],{"type":27,"value":774},{"type":22,"tag":759,"props":937,"children":938},{"style":777},[939],{"type":27,"value":799},{"type":22,"tag":759,"props":941,"children":942},{"style":821},[943],{"type":27,"value":944}," new",{"type":22,"tag":759,"props":946,"children":947},{"style":783},[948],{"type":27,"value":949}," MyClass",{"type":22,"tag":759,"props":951,"children":952},{"style":771},[953],{"type":27,"value":954},"(",{"type":22,"tag":759,"props":956,"children":958},{"style":957},"--shiki-default:#8DA101;--shiki-dark:#E9F284",[959],{"type":27,"value":960},"\"",{"type":22,"tag":759,"props":962,"children":964},{"style":963},"--shiki-default:#8DA101;--shiki-dark:#F1FA8C",[965],{"type":27,"value":854},{"type":22,"tag":759,"props":967,"children":968},{"style":957},[969],{"type":27,"value":960},{"type":22,"tag":759,"props":971,"children":972},{"style":771},[973],{"type":27,"value":974},", ",{"type":22,"tag":759,"props":976,"children":977},{"style":874},[978],{"type":27,"value":979},"5",{"type":22,"tag":759,"props":981,"children":982},{"style":771},[983],{"type":27,"value":974},{"type":22,"tag":759,"props":985,"children":986},{"style":874},[987],{"type":27,"value":988},"567.89",{"type":22,"tag":759,"props":990,"children":991},{"style":771},[992],{"type":27,"value":993},");\n",{"type":22,"tag":23,"props":995,"children":996},{},[997],{"type":27,"value":998},"If all three of these properties are essential then this makes for a sensible constructor however many classes have a number of properties that are optional and class designers struggle to determine whether to make constructors that merely cut-down on typing and which of the various combinations of optional properties might make sense in having their own constructor.",{"type":22,"tag":23,"props":1000,"children":1001},{},[1002],{"type":27,"value":1003},"Invariably the combination you might want doesn’t exist and if it does the chances of being able to understand which properties are being set from one of a number of constructors that take parameters of similar types is low unless you go and take a peek with the IntelliSense.",{"type":22,"tag":23,"props":1005,"children":1006},{},[1007],{"type":27,"value":1008},"Using object initializers you can stick to creating constructors that reflect parameters necessary to ensure your object is in a valid state and forget about providing helpful ones for those optional parameters. In our example if we assume the ProductCode is essential and the others are optional we can write code like:",{"type":22,"tag":749,"props":1010,"children":1012},{"className":918,"code":1011,"language":920,"meta":10,"style":10},"MyClass myObj = new MyClass(\"ABC123\") { Quantity = 5, Cost = 567.89 };\n",[1013],{"type":22,"tag":272,"props":1014,"children":1015},{"__ignoreMap":10},[1016],{"type":22,"tag":759,"props":1017,"children":1018},{"class":761,"line":91},[1019,1023,1027,1031,1035,1039,1043,1047,1051,1055,1060,1064,1069,1074,1078,1083],{"type":22,"tag":759,"props":1020,"children":1021},{"style":783},[1022],{"type":27,"value":810},{"type":22,"tag":759,"props":1024,"children":1025},{"style":771},[1026],{"type":27,"value":774},{"type":22,"tag":759,"props":1028,"children":1029},{"style":777},[1030],{"type":27,"value":799},{"type":22,"tag":759,"props":1032,"children":1033},{"style":821},[1034],{"type":27,"value":944},{"type":22,"tag":759,"props":1036,"children":1037},{"style":783},[1038],{"type":27,"value":949},{"type":22,"tag":759,"props":1040,"children":1041},{"style":771},[1042],{"type":27,"value":954},{"type":22,"tag":759,"props":1044,"children":1045},{"style":957},[1046],{"type":27,"value":960},{"type":22,"tag":759,"props":1048,"children":1049},{"style":963},[1050],{"type":27,"value":854},{"type":22,"tag":759,"props":1052,"children":1053},{"style":957},[1054],{"type":27,"value":960},{"type":22,"tag":759,"props":1056,"children":1057},{"style":771},[1058],{"type":27,"value":1059},") { Quantity ",{"type":22,"tag":759,"props":1061,"children":1062},{"style":777},[1063],{"type":27,"value":799},{"type":22,"tag":759,"props":1065,"children":1066},{"style":874},[1067],{"type":27,"value":1068}," 5",{"type":22,"tag":759,"props":1070,"children":1071},{"style":771},[1072],{"type":27,"value":1073},", Cost ",{"type":22,"tag":759,"props":1075,"children":1076},{"style":777},[1077],{"type":27,"value":799},{"type":22,"tag":759,"props":1079,"children":1080},{"style":874},[1081],{"type":27,"value":1082}," 567.89",{"type":22,"tag":759,"props":1084,"children":1085},{"style":771},[1086],{"type":27,"value":1087}," };\n",{"type":22,"tag":23,"props":1089,"children":1090},{},[1091],{"type":27,"value":1092},"Which is both concise and easy to understand. It also requires no work on the part of the class designer and therefore works with all your existing classes. You can also nest them to set properties that require more complex types such as:",{"type":22,"tag":749,"props":1094,"children":1096},{"className":918,"code":1095,"language":920,"meta":10,"style":10},"MyClass myObj = new MyClass(\"ABC123\") {\n   Quantity = 5,\n   Cost = 567.89,\n   Category = new Category(\"A\") { Description = \"New machine\" }\n};\n",[1097],{"type":22,"tag":272,"props":1098,"children":1099},{"__ignoreMap":10},[1100,1144,1165,1185,1250],{"type":22,"tag":759,"props":1101,"children":1102},{"class":761,"line":91},[1103,1107,1111,1115,1119,1123,1127,1131,1135,1139],{"type":22,"tag":759,"props":1104,"children":1105},{"style":783},[1106],{"type":27,"value":810},{"type":22,"tag":759,"props":1108,"children":1109},{"style":771},[1110],{"type":27,"value":774},{"type":22,"tag":759,"props":1112,"children":1113},{"style":777},[1114],{"type":27,"value":799},{"type":22,"tag":759,"props":1116,"children":1117},{"style":821},[1118],{"type":27,"value":944},{"type":22,"tag":759,"props":1120,"children":1121},{"style":783},[1122],{"type":27,"value":949},{"type":22,"tag":759,"props":1124,"children":1125},{"style":771},[1126],{"type":27,"value":954},{"type":22,"tag":759,"props":1128,"children":1129},{"style":957},[1130],{"type":27,"value":960},{"type":22,"tag":759,"props":1132,"children":1133},{"style":963},[1134],{"type":27,"value":854},{"type":22,"tag":759,"props":1136,"children":1137},{"style":957},[1138],{"type":27,"value":960},{"type":22,"tag":759,"props":1140,"children":1141},{"style":771},[1142],{"type":27,"value":1143},") {\n",{"type":22,"tag":759,"props":1145,"children":1146},{"class":761,"line":81},[1147,1152,1156,1160],{"type":22,"tag":759,"props":1148,"children":1149},{"style":771},[1150],{"type":27,"value":1151},"   Quantity ",{"type":22,"tag":759,"props":1153,"children":1154},{"style":777},[1155],{"type":27,"value":799},{"type":22,"tag":759,"props":1157,"children":1158},{"style":874},[1159],{"type":27,"value":1068},{"type":22,"tag":759,"props":1161,"children":1162},{"style":771},[1163],{"type":27,"value":1164},",\n",{"type":22,"tag":759,"props":1166,"children":1167},{"class":761,"line":702},[1168,1173,1177,1181],{"type":22,"tag":759,"props":1169,"children":1170},{"style":771},[1171],{"type":27,"value":1172},"   Cost ",{"type":22,"tag":759,"props":1174,"children":1175},{"style":777},[1176],{"type":27,"value":799},{"type":22,"tag":759,"props":1178,"children":1179},{"style":874},[1180],{"type":27,"value":1082},{"type":22,"tag":759,"props":1182,"children":1183},{"style":771},[1184],{"type":27,"value":1164},{"type":22,"tag":759,"props":1186,"children":1187},{"class":761,"line":832},[1188,1193,1197,1201,1206,1210,1214,1219,1223,1228,1232,1236,1241,1245],{"type":22,"tag":759,"props":1189,"children":1190},{"style":771},[1191],{"type":27,"value":1192},"   Category ",{"type":22,"tag":759,"props":1194,"children":1195},{"style":777},[1196],{"type":27,"value":799},{"type":22,"tag":759,"props":1198,"children":1199},{"style":821},[1200],{"type":27,"value":944},{"type":22,"tag":759,"props":1202,"children":1203},{"style":783},[1204],{"type":27,"value":1205}," Category",{"type":22,"tag":759,"props":1207,"children":1208},{"style":771},[1209],{"type":27,"value":954},{"type":22,"tag":759,"props":1211,"children":1212},{"style":957},[1213],{"type":27,"value":960},{"type":22,"tag":759,"props":1215,"children":1216},{"style":963},[1217],{"type":27,"value":1218},"A",{"type":22,"tag":759,"props":1220,"children":1221},{"style":957},[1222],{"type":27,"value":960},{"type":22,"tag":759,"props":1224,"children":1225},{"style":771},[1226],{"type":27,"value":1227},") { Description ",{"type":22,"tag":759,"props":1229,"children":1230},{"style":777},[1231],{"type":27,"value":799},{"type":22,"tag":759,"props":1233,"children":1234},{"style":957},[1235],{"type":27,"value":848},{"type":22,"tag":759,"props":1237,"children":1238},{"style":963},[1239],{"type":27,"value":1240},"New machine",{"type":22,"tag":759,"props":1242,"children":1243},{"style":957},[1244],{"type":27,"value":960},{"type":22,"tag":759,"props":1246,"children":1247},{"style":771},[1248],{"type":27,"value":1249}," }\n",{"type":22,"tag":759,"props":1251,"children":1252},{"class":761,"line":718},[1253],{"type":22,"tag":759,"props":1254,"children":1255},{"style":771},[1256],{"type":27,"value":1257},"};\n",{"type":22,"tag":23,"props":1259,"children":1260},{},[1261],{"type":27,"value":1262},"This feature is no use if your objects are immutable in which case constructors are your only friend.",{"type":22,"tag":23,"props":1264,"children":1265},{},[1266],{"type":22,"tag":75,"props":1267,"children":1268},{},[1269],{"type":27,"value":79},{"type":22,"tag":482,"props":1271,"children":1272},{},[1273],{"type":27,"value":1274},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":10,"searchDepth":81,"depth":81,"links":1276},[],"content:blog:2007:2007-11-04-object-initializers-in-net-35.md","blog/2007/2007-11-04-object-initializers-in-net-35.md","blog/2007/2007-11-04-object-initializers-in-net-35","/blog/2007/11/04/object-initializers-in-net-35/",430,0,{"_path":1284,"_dir":1285,"_draft":7,"_partial":7,"_locale":8,"title":1286,"description":1287,"date":1288,"category":12,"tags":1289,"excerpt":1292,"body":1303,"_type":83,"_id":1410,"_source":85,"_file":1411,"_stem":1412,"_extension":88,"url":1413,"wordCount":1414,"minutes":81,"commentCount":1282},"/blog/2005/2005-09-13-visualstudio2003systemargumentexceptionindebugger","2005","Visual Studio 2003: System.ArgumentException in debugger","I recently ran into a problem while debugging inside Visual Studio 2003.Net. Google couldn’t find me an answer, only a few other people with the same problem. Here’s my solution in the hope it might save somebody else some time.","2005-09-13T12:06:00+00:00",[14,1290,1291,16],"debugging","Visual Studio",{"type":19,"children":1293},[1294,1298],{"type":22,"tag":23,"props":1295,"children":1296},{},[1297],{"type":27,"value":1287},{"type":22,"tag":23,"props":1299,"children":1300},{},[1301],{"type":27,"value":1302},"Whenever debugging a specific VB.NET application that used a C# class library I would receive the following error certain objects in the C# class library:",{"type":19,"children":1304,"toc":1405},[1305,1309,1315,1319,1327,1332,1337,1343,1348,1353,1367,1373,1388,1393,1398],{"type":22,"tag":23,"props":1306,"children":1307},{},[1308],{"type":27,"value":1287},{"type":22,"tag":242,"props":1310,"children":1312},{"id":1311},"symptoms",[1313],{"type":27,"value":1314},"Symptoms",{"type":22,"tag":23,"props":1316,"children":1317},{},[1318],{"type":27,"value":1302},{"type":22,"tag":749,"props":1320,"children":1322},{"code":1321},"\u003Cerror: an exception of type: {System.ArgumentException} occurred>\n",[1323],{"type":22,"tag":272,"props":1324,"children":1325},{"__ignoreMap":10},[1326],{"type":27,"value":1321},{"type":22,"tag":23,"props":1328,"children":1329},{},[1330],{"type":27,"value":1331},"Strangely only the ASP.NET application was affected, the WinForms application that also used this class library was unaffected. It also seemed to be localized to the debugger only, run-time behavior appeared to be just fine.",{"type":22,"tag":23,"props":1333,"children":1334},{},[1335],{"type":27,"value":1336},"A reboot, rebuild project or a clear down of the ASP temp directory had no effect and another developer on the project had exactly the same problem despite the fact we do not share any binaries…",{"type":22,"tag":242,"props":1338,"children":1340},{"id":1339},"solution",[1341],{"type":27,"value":1342},"Solution",{"type":22,"tag":23,"props":1344,"children":1345},{},[1346],{"type":27,"value":1347},"Close VS and delete all bin and obj directories from all projects in your solution if you want to be totally sure.",{"type":22,"tag":23,"props":1349,"children":1350},{},[1351],{"type":27,"value":1352},"If you really don’t want to do this, deleting the .pdb’s from the project where the messed up object lives may be enough.",{"type":22,"tag":23,"props":1354,"children":1355},{},[1356,1358,1366],{"type":27,"value":1357},"You may think forcing a rebuild with “rebuild solution” would achieve this, but it ",{"type":22,"tag":255,"props":1359,"children":1360},{},[1361],{"type":22,"tag":75,"props":1362,"children":1363},{},[1364],{"type":27,"value":1365},"does not",{"type":27,"value":112},{"type":22,"tag":242,"props":1368,"children":1370},{"id":1369},"cause-optional",[1371],{"type":27,"value":1372},"Cause (optional)",{"type":22,"tag":23,"props":1374,"children":1375},{},[1376,1378,1386],{"type":27,"value":1377},"While debugging I noticed that I couldn’t create the messed up object interactively either. It was complaining that no constructor took 4 arguments and that it took 3. Well, it ",{"type":22,"tag":255,"props":1379,"children":1380},{},[1381],{"type":22,"tag":75,"props":1382,"children":1383},{},[1384],{"type":27,"value":1385},"used to",{"type":27,"value":1387}," take 3, over a week ago.",{"type":22,"tag":23,"props":1389,"children":1390},{},[1391],{"type":27,"value":1392},"This leads me to believe that some combination of changing your C# source can make either VS or the C# compiler believe the debugging symbols are still up to date when they are not. This may possibly only affect VB.NET applications compiled against it.",{"type":22,"tag":23,"props":1394,"children":1395},{},[1396],{"type":27,"value":1397},"It must be a reproducible bug because it happened on two different machines.",{"type":22,"tag":23,"props":1399,"children":1400},{},[1401],{"type":22,"tag":75,"props":1402,"children":1403},{},[1404],{"type":27,"value":79},{"title":10,"searchDepth":81,"depth":81,"links":1406},[1407,1408,1409],{"id":1311,"depth":81,"text":1314},{"id":1339,"depth":81,"text":1342},{"id":1369,"depth":81,"text":1372},"content:blog:2005:2005-09-13-visualstudio2003systemargumentexceptionindebugger.md","blog/2005/2005-09-13-visualstudio2003systemargumentexceptionindebugger.md","blog/2005/2005-09-13-visualstudio2003systemargumentexceptionindebugger","/blog/2005/09/13/visualstudio2003systemargumentexceptionindebugger/",305,{"_path":1416,"_dir":1285,"_draft":7,"_partial":7,"_locale":8,"title":1417,"description":1418,"date":1419,"category":12,"tags":1420,"excerpt":1421,"body":1445,"_type":83,"_id":1905,"_source":85,"_file":1906,"_stem":1907,"_extension":88,"url":1908,"wordCount":1909,"minutes":702,"commentCount":1282},"/blog/2005/2005-08-08-vbnet-to-c-conversion","VB.NET to C# conversion","I recently converted some components on a project from VB.NET to C#, mainly for overloading and better tool support (such as ReSharper). Some of the existing code was generated from my own CodeSmith templates, so a small rewrite to generate C# handled most of that.","2005-08-08T02:00:00+00:00",[14,17,16],{"type":19,"children":1422},[1423,1436],{"type":22,"tag":23,"props":1424,"children":1425},{},[1426,1428,1430,1432,1434],{"type":27,"value":1427},"I recently converted some components on a project from VB.NET to C#, mainly for overloading and better tool support (such as ",{"type":27,"value":1429},"ReSharper",{"type":27,"value":1431},"). Some of the existing code was generated from my own ",{"type":27,"value":1433},"CodeSmith",{"type":27,"value":1435}," templates, so a small rewrite to generate C# handled most of that.",{"type":22,"tag":23,"props":1437,"children":1438},{},[1439,1441,1443],{"type":27,"value":1440},"The remaining code, while not extensive in size, is a rather complex affair and the prospect of debugging this code when hand-converted was a little daunting so I decided to give the demo version of VBConversion’s ",{"type":27,"value":1442},"VB.NET to C# Converter 1.31",{"type":27,"value":1444}," a spin.",{"type":19,"children":1446,"toc":1896},[1447,1465,1470,1481,1495,1501,1602,1607,1680,1685,1691,1696,1702,1707,1713,1718,1764,1769,1813,1819,1824,1885,1892],{"type":22,"tag":23,"props":1448,"children":1449},{},[1450,1451,1457,1458,1464],{"type":27,"value":1427},{"type":22,"tag":63,"props":1452,"children":1455},{"href":1453,"rel":1454},"https://www.jetbrains.com/resharper/",[301],[1456],{"type":27,"value":1429},{"type":27,"value":1431},{"type":22,"tag":63,"props":1459,"children":1462},{"href":1460,"rel":1461},"https://www.codesmithtools.com",[301],[1463],{"type":27,"value":1433},{"type":27,"value":1435},{"type":22,"tag":242,"props":1466,"children":1468},{"id":1467},"vbnet-to-c-converter-131",[1469],{"type":27,"value":1442},{"type":22,"tag":23,"props":1471,"children":1472},{},[1473,1474,1480],{"type":27,"value":1440},{"type":22,"tag":63,"props":1475,"children":1478},{"href":1476,"rel":1477},"https://www.vbconversions.com",[301],[1479],{"type":27,"value":1442},{"type":27,"value":1444},{"type":22,"tag":23,"props":1482,"children":1483},{},[1484,1486,1493],{"type":27,"value":1485},"I was disappointed it made some obvious and stupid mistakes especially when it had converted everything else so well; so much for the 99% accuracy claim. I thought it might just be our project be we adhere to many of ",{"type":22,"tag":63,"props":1487,"children":1490},{"href":1488,"rel":1489},"https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpgenref/html/cpconnetframeworkdesignguidelines.asp",[301],[1491],{"type":27,"value":1492},"Microsoft’s guidelines",{"type":27,"value":1494},"…",{"type":22,"tag":367,"props":1496,"children":1498},{"id":1497},"problem-1-ignores-all-instanceclass-variables-default-assignments-and-constructors",[1499],{"type":27,"value":1500},"Problem 1: Ignores all instance/class variables default assignments and constructors.",{"type":22,"tag":749,"props":1502,"children":1504},{"className":751,"code":1503,"language":753,"meta":10,"style":10},"Public Class Mine\n  Public myObj As MyClass = New MyClass()\n  Private myVar As MyEnumeration = MyEnumeration.MyDefault\nEnd Class\n",[1505],{"type":22,"tag":272,"props":1506,"children":1507},{"__ignoreMap":10},[1508,1526,1563,1594],{"type":22,"tag":759,"props":1509,"children":1510},{"class":761,"line":91},[1511,1516,1521],{"type":22,"tag":759,"props":1512,"children":1513},{"style":777},[1514],{"type":27,"value":1515},"Public",{"type":22,"tag":759,"props":1517,"children":1518},{"style":765},[1519],{"type":27,"value":1520}," Class ",{"type":22,"tag":759,"props":1522,"children":1523},{"style":771},[1524],{"type":27,"value":1525},"Mine\n",{"type":22,"tag":759,"props":1527,"children":1528},{"class":761,"line":81},[1529,1534,1538,1542,1546,1551,1555,1559],{"type":22,"tag":759,"props":1530,"children":1531},{"style":777},[1532],{"type":27,"value":1533},"  Public",{"type":22,"tag":759,"props":1535,"children":1536},{"style":771},[1537],{"type":27,"value":774},{"type":22,"tag":759,"props":1539,"children":1540},{"style":777},[1541],{"type":27,"value":780},{"type":22,"tag":759,"props":1543,"children":1544},{"style":783},[1545],{"type":27,"value":949},{"type":22,"tag":759,"props":1547,"children":1548},{"style":777},[1549],{"type":27,"value":1550}," =",{"type":22,"tag":759,"props":1552,"children":1553},{"style":765},[1554],{"type":27,"value":804},{"type":22,"tag":759,"props":1556,"children":1557},{"style":807},[1558],{"type":27,"value":810},{"type":22,"tag":759,"props":1560,"children":1561},{"style":771},[1562],{"type":27,"value":815},{"type":22,"tag":759,"props":1564,"children":1565},{"class":761,"line":702},[1566,1571,1576,1580,1585,1589],{"type":22,"tag":759,"props":1567,"children":1568},{"style":777},[1569],{"type":27,"value":1570},"  Private",{"type":22,"tag":759,"props":1572,"children":1573},{"style":771},[1574],{"type":27,"value":1575}," myVar ",{"type":22,"tag":759,"props":1577,"children":1578},{"style":777},[1579],{"type":27,"value":780},{"type":22,"tag":759,"props":1581,"children":1582},{"style":783},[1583],{"type":27,"value":1584}," MyEnumeration",{"type":22,"tag":759,"props":1586,"children":1587},{"style":777},[1588],{"type":27,"value":1550},{"type":22,"tag":759,"props":1590,"children":1591},{"style":771},[1592],{"type":27,"value":1593}," MyEnumeration.MyDefault\n",{"type":22,"tag":759,"props":1595,"children":1596},{"class":761,"line":832},[1597],{"type":22,"tag":759,"props":1598,"children":1599},{"style":765},[1600],{"type":27,"value":1601},"End Class\n",{"type":22,"tag":23,"props":1603,"children":1604},{},[1605],{"type":27,"value":1606},"Suddenly becomes;",{"type":22,"tag":749,"props":1608,"children":1610},{"className":918,"code":1609,"language":920,"meta":10,"style":10},"public class Mine {\n  public MyClass myObj;\n  private MyEnumeration myVar;\n}\n",[1611],{"type":22,"tag":272,"props":1612,"children":1613},{"__ignoreMap":10},[1614,1638,1655,1672],{"type":22,"tag":759,"props":1615,"children":1616},{"class":761,"line":91},[1617,1622,1627,1633],{"type":22,"tag":759,"props":1618,"children":1619},{"style":777},[1620],{"type":27,"value":1621},"public",{"type":22,"tag":759,"props":1623,"children":1624},{"style":821},[1625],{"type":27,"value":1626}," class",{"type":22,"tag":759,"props":1628,"children":1630},{"style":1629},"--shiki-default:#3A94C5;--shiki-dark:#8BE9FD",[1631],{"type":27,"value":1632}," Mine",{"type":22,"tag":759,"props":1634,"children":1635},{"style":771},[1636],{"type":27,"value":1637}," {\n",{"type":22,"tag":759,"props":1639,"children":1640},{"class":761,"line":81},[1641,1646,1650],{"type":22,"tag":759,"props":1642,"children":1643},{"style":777},[1644],{"type":27,"value":1645},"  public",{"type":22,"tag":759,"props":1647,"children":1648},{"style":783},[1649],{"type":27,"value":949},{"type":22,"tag":759,"props":1651,"children":1652},{"style":771},[1653],{"type":27,"value":1654}," myObj;\n",{"type":22,"tag":759,"props":1656,"children":1657},{"class":761,"line":702},[1658,1663,1667],{"type":22,"tag":759,"props":1659,"children":1660},{"style":777},[1661],{"type":27,"value":1662},"  private",{"type":22,"tag":759,"props":1664,"children":1665},{"style":783},[1666],{"type":27,"value":1584},{"type":22,"tag":759,"props":1668,"children":1669},{"style":771},[1670],{"type":27,"value":1671}," myVar;\n",{"type":22,"tag":759,"props":1673,"children":1674},{"class":761,"line":832},[1675],{"type":22,"tag":759,"props":1676,"children":1677},{"style":771},[1678],{"type":27,"value":1679},"}\n",{"type":22,"tag":23,"props":1681,"children":1682},{},[1683],{"type":27,"value":1684},"While the object not being created will soon throw an exception, the defaults for value types is a little harder to track down.",{"type":22,"tag":367,"props":1686,"children":1688},{"id":1687},"problem-2-gives-up-on-select-case-statements-after-the-first-case-commenting-out-the-others",[1689],{"type":27,"value":1690},"Problem 2: Gives up on Select Case statements after the first case, commenting out the others.",{"type":22,"tag":23,"props":1692,"children":1693},{},[1694],{"type":27,"value":1695},"In at least one case it got so confused it commented out substantially more code after the case statement too.",{"type":22,"tag":367,"props":1697,"children":1699},{"id":1698},"problem-3-declares-additional-unnecessary-name-spaces-including-the-one-for-microsoftvisualbasic-despite-not-needing-it",[1700],{"type":27,"value":1701},"Problem 3: Declares additional unnecessary name-spaces including the one for Microsoft.VisualBasic, despite not needing it.",{"type":22,"tag":23,"props":1703,"children":1704},{},[1705],{"type":27,"value":1706},"Either we hadn’t used the CDate/CInt/CLng functions or it had converted them)…",{"type":22,"tag":367,"props":1708,"children":1710},{"id":1709},"problem-4-all-with-statements-are-converted-to-be-variable-with1-even-when-the-old-with-clause-was-a-simple-case",[1711],{"type":27,"value":1712},"Problem 4: All with statements are converted to be variable with1, even when the old with clause was a simple case.",{"type":22,"tag":23,"props":1714,"children":1715},{},[1716],{"type":27,"value":1717},"For example:",{"type":22,"tag":749,"props":1719,"children":1721},{"className":751,"code":1720,"language":753,"meta":10,"style":10},"With myObj\n  .doThis()\nEnd With\n",[1722],{"type":22,"tag":272,"props":1723,"children":1724},{"__ignoreMap":10},[1725,1736,1753],{"type":22,"tag":759,"props":1726,"children":1727},{"class":761,"line":91},[1728,1732],{"type":22,"tag":759,"props":1729,"children":1730},{"style":821},[1731],{"type":27,"value":824},{"type":22,"tag":759,"props":1733,"children":1734},{"style":771},[1735],{"type":27,"value":829},{"type":22,"tag":759,"props":1737,"children":1738},{"class":761,"line":81},[1739,1744,1749],{"type":22,"tag":759,"props":1740,"children":1741},{"style":771},[1742],{"type":27,"value":1743},"  .",{"type":22,"tag":759,"props":1745,"children":1746},{"style":807},[1747],{"type":27,"value":1748},"doThis",{"type":22,"tag":759,"props":1750,"children":1751},{"style":771},[1752],{"type":27,"value":815},{"type":22,"tag":759,"props":1754,"children":1755},{"class":761,"line":702},[1756,1760],{"type":22,"tag":759,"props":1757,"children":1758},{"style":902},[1759],{"type":27,"value":905},{"type":22,"tag":759,"props":1761,"children":1762},{"style":821},[1763],{"type":27,"value":910},{"type":22,"tag":23,"props":1765,"children":1766},{},[1767],{"type":27,"value":1768},"becomes;",{"type":22,"tag":749,"props":1770,"children":1772},{"className":918,"code":1771,"language":920,"meta":10,"style":10},"MyClass with1 = myObj;\nwith1.doThis();\n",[1773],{"type":22,"tag":272,"props":1774,"children":1775},{"__ignoreMap":10},[1776,1796],{"type":22,"tag":759,"props":1777,"children":1778},{"class":761,"line":91},[1779,1783,1788,1792],{"type":22,"tag":759,"props":1780,"children":1781},{"style":783},[1782],{"type":27,"value":810},{"type":22,"tag":759,"props":1784,"children":1785},{"style":771},[1786],{"type":27,"value":1787}," with1 ",{"type":22,"tag":759,"props":1789,"children":1790},{"style":777},[1791],{"type":27,"value":799},{"type":22,"tag":759,"props":1793,"children":1794},{"style":771},[1795],{"type":27,"value":1654},{"type":22,"tag":759,"props":1797,"children":1798},{"class":761,"line":81},[1799,1804,1808],{"type":22,"tag":759,"props":1800,"children":1801},{"style":771},[1802],{"type":27,"value":1803},"with1.",{"type":22,"tag":759,"props":1805,"children":1806},{"style":807},[1807],{"type":27,"value":1748},{"type":22,"tag":759,"props":1809,"children":1810},{"style":771},[1811],{"type":27,"value":1812},"();\n",{"type":22,"tag":242,"props":1814,"children":1816},{"id":1815},"using-both-vbnet-and-c-together",[1817],{"type":27,"value":1818},"Using both VB.NET and C# together",{"type":22,"tag":23,"props":1820,"children":1821},{},[1822],{"type":27,"value":1823},"If you are going to use both in a project you can bring the syntax a little closer in format, here’s a few tips.",{"type":22,"tag":138,"props":1825,"children":1826},{},[1827,1832,1865,1870,1875,1880],{"type":22,"tag":142,"props":1828,"children":1829},{},[1830],{"type":27,"value":1831},"Don’t use the Visual Basic CInt/CDate/IsNumber etc functions. Use the .NET Framework equivalents such as Int.Parse, Date.Parse etc. which will work in both languages and are normally faster than these legacy functions.",{"type":22,"tag":142,"props":1833,"children":1834},{},[1835,1837],{"type":27,"value":1836},"Bring the source closer together by;\n",{"type":22,"tag":138,"props":1838,"children":1839},{},[1840,1845,1850,1855,1860],{"type":22,"tag":142,"props":1841,"children":1842},{},[1843],{"type":27,"value":1844},"Putting brackets round if conditions in VB.NET",{"type":22,"tag":142,"props":1846,"children":1847},{},[1848],{"type":27,"value":1849},"Putting quotes round region declarations in C#",{"type":22,"tag":142,"props":1851,"children":1852},{},[1853],{"type":27,"value":1854},"Putting attributes on separate lines in VB.NET with a _ suffix",{"type":22,"tag":142,"props":1856,"children":1857},{},[1858],{"type":27,"value":1859},"Dropping the implementation and interface declarations onto a new line in C#",{"type":22,"tag":142,"props":1861,"children":1862},{},[1863],{"type":27,"value":1864},"Dropping the base(whatever) onto a new line in C# constructors (good idea anyway)",{"type":22,"tag":142,"props":1866,"children":1867},{},[1868],{"type":27,"value":1869},"Check out the “Differences Between Visual Basic .NET and Visual C# .NET” white paper on MSDN",{"type":22,"tag":142,"props":1871,"children":1872},{},[1873],{"type":27,"value":1874},"Use VBCommenter to get C# style XML comment documentation.",{"type":22,"tag":142,"props":1876,"children":1877},{},[1878],{"type":27,"value":1879},"Use Monitor.Enter() instead of VB.NET's synclock and C#’s lock.",{"type":22,"tag":142,"props":1881,"children":1882},{},[1883],{"type":27,"value":1884},"Be aware of the differences regarding math! VB.NET defaults to floating point precision and rounding when converting with CInt/CLng etc.. C# normally uses integer division and casting truncates.",{"type":22,"tag":23,"props":1886,"children":1887},{},[1888],{"type":22,"tag":75,"props":1889,"children":1890},{},[1891],{"type":27,"value":79},{"type":22,"tag":482,"props":1893,"children":1894},{},[1895],{"type":27,"value":1274},{"title":10,"searchDepth":81,"depth":81,"links":1897},[1898,1904],{"id":1467,"depth":81,"text":1442,"children":1899},[1900,1901,1902,1903],{"id":1497,"depth":702,"text":1500},{"id":1687,"depth":702,"text":1690},{"id":1698,"depth":702,"text":1701},{"id":1709,"depth":702,"text":1712},{"id":1815,"depth":81,"text":1818},"content:blog:2005:2005-08-08-vbnet-to-c-conversion.md","blog/2005/2005-08-08-vbnet-to-c-conversion.md","blog/2005/2005-08-08-vbnet-to-c-conversion","/blog/2005/08/08/vbnet-to-c-conversion/",519,{"_path":1911,"_dir":1285,"_draft":7,"_partial":7,"_locale":8,"title":1912,"description":1913,"date":1914,"category":12,"tags":1915,"excerpt":1916,"body":1927,"_type":83,"_id":2582,"_source":85,"_file":2583,"_stem":2584,"_extension":88,"url":2585,"wordCount":2586,"minutes":832,"commentCount":91},"/blog/2005/2005-01-19-lapsedlistenersmemoryleaksinsubscriberpublisherscenarios","Lapsed-listeners: Memory leaks in subscriber-publisher scenarios","I’ve been promising something .NET related for a while… here’s something!","2005-01-19T11:32:00+00:00",[14,17,16],{"type":19,"children":1917},[1918,1922],{"type":22,"tag":23,"props":1919,"children":1920},{},[1921],{"type":27,"value":1913},{"type":22,"tag":23,"props":1923,"children":1924},{},[1925],{"type":27,"value":1926},"There exists a ‘gotcha’ in .NET (and other programming environment) whereby an object has subscribed to another objects published event will not be garbage collected when you expect because the environment itself holds a reference to the subscriber inside the event notification system.",{"type":19,"children":1928,"toc":2570},[1929,1933,1939,1943,1948,1954,1959,1965,1979,1993,2007,2013,2029,2035,2073,2078,2084,2114,2119,2140,2388,2394,2423,2428,2451,2456,2468,2474,2488,2522,2543,2559,2566],{"type":22,"tag":23,"props":1930,"children":1931},{},[1932],{"type":27,"value":1913},{"type":22,"tag":242,"props":1934,"children":1936},{"id":1935},"the-lapsed-listener-scenario",[1937],{"type":27,"value":1938},"The lapsed-listener scenario",{"type":22,"tag":23,"props":1940,"children":1941},{},[1942],{"type":27,"value":1926},{"type":22,"tag":23,"props":1944,"children":1945},{},[1946],{"type":27,"value":1947},"Let’s illustrate with an example:",{"type":22,"tag":367,"props":1949,"children":1951},{"id":1950},"mybusiness-class",[1952],{"type":27,"value":1953},"MyBusiness Class",{"type":22,"tag":23,"props":1955,"children":1956},{},[1957],{"type":27,"value":1958},"Encapsulates some aspect of business functionality and publishes a Changed event.",{"type":22,"tag":367,"props":1960,"children":1962},{"id":1961},"mytreenode-business-adapter-class",[1963],{"type":27,"value":1964},"MyTreeNode Business Adapter Class",{"type":22,"tag":23,"props":1966,"children":1967},{},[1968,1970,1977],{"type":27,"value":1969},"Inherits from ",{"type":22,"tag":63,"props":1971,"children":1974},{"href":1972,"rel":1973},"https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemWindowsFormsTreeNodeClassTopic.asp",[301],[1975],{"type":27,"value":1976},"TreeNode",{"type":27,"value":1978}," and takes an instance of MyBusiness in the constructor. The object signs up to the Changed event of the MyBusiness object so that it can always accurately show the correct Icon (via ImageIndex), Text and update any associated child nodes etc.",{"type":22,"tag":23,"props":1980,"children":1981},{},[1982,1984,1991],{"type":27,"value":1983},"Now you happily add instances of MyTreeNodeBusinessAdapter class to your ",{"type":22,"tag":63,"props":1985,"children":1988},{"href":1986,"rel":1987},"https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformstreeviewclasstopic.asp",[301],[1989],{"type":27,"value":1990},"TreeView",{"type":27,"value":1992},", each with the associated MyBusiness instance it should be reflecting.",{"type":22,"tag":23,"props":1994,"children":1995},{},[1996,1998,2005],{"type":27,"value":1997},"The problem now arises in that if the TreeNode is removed from the TreeView with either Nodes.Remove or Nodes.Clear then the ",{"type":22,"tag":63,"props":1999,"children":2002},{"href":2000,"rel":2001},"https://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetGCbasics.asp",[301],[2003],{"type":27,"value":2004},"garbage collector",{"type":27,"value":2006}," will not be able dispose of the MyTreeNodeBusinessAdapter objects because the MyBusiness objects hold a reference to them in their event notification queues. Even worse, an inactive TreeView hangs around because the not-yet-disposed TreeNode’s are still pointing to it.",{"type":22,"tag":242,"props":2008,"children":2010},{"id":2009},"possible-solutions",[2011],{"type":27,"value":2012},"Possible solutions…",{"type":22,"tag":54,"props":2014,"children":2015},{},[2016],{"type":22,"tag":23,"props":2017,"children":2018},{},[2019,2021,2027],{"type":27,"value":2020},".NET Framework 3.0 has a ",{"type":22,"tag":63,"props":2022,"children":2024},{"href":2023},"https://msdn2.microsoft.com/en-us/library/aa970850.aspx",[2025],{"type":27,"value":2026},"WeakEvent pattern",{"type":27,"value":2028}," to make things simple!",{"type":22,"tag":367,"props":2030,"children":2032},{"id":2031},"_1-mytreenodebusinessadapter-listens-in-to-treeview-events-and-un-subscribes-when-it-is-removed-rejected",[2033],{"type":27,"value":2034},"1. MyTreeNodeBusinessAdapter listens in to TreeView events and un-subscribes when it is removed (Rejected)",{"type":22,"tag":23,"props":2036,"children":2037},{},[2038,2040,2047,2049,2054,2056,2062,2064,2071],{"type":27,"value":2039},"TreeNode’s maintain their own internal array of child nodes which they publish under the “Nodes” property which uses the ",{"type":22,"tag":63,"props":2041,"children":2044},{"href":2042,"rel":2043},"https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemwindowsformstreenodecollectionclasstopic.asp",[301],[2045],{"type":27,"value":2046},"TreeNodeCollection",{"type":27,"value":2048}," class to just point straight back at itself calling internal methods. None are over-ridable and while they ",{"type":22,"tag":255,"props":2050,"children":2051},{},[2052],{"type":27,"value":2053},"do",{"type":27,"value":2055}," send the message ",{"type":22,"tag":272,"props":2057,"children":2059},{"className":2058},[],[2060],{"type":27,"value":2061},"TVM_DELETEITEM",{"type":27,"value":2063}," to their own TreeView control it does nothing with them nor does it have methods you could implement in a subclass. (",{"type":22,"tag":63,"props":2065,"children":2068},{"href":2066,"rel":2067},"https://www.aisto.com/roeder/dotnet/",[301],[2069],{"type":27,"value":2070},".NET Reflector",{"type":27,"value":2072}," is great for finding out what’s going on inside the WinForms assembly)",{"type":22,"tag":23,"props":2074,"children":2075},{},[2076],{"type":27,"value":2077},"This would also mean every UI control you adapt would need sub-classing to override even if you could hook into the remove events.",{"type":22,"tag":367,"props":2079,"children":2081},{"id":2080},"_2-weak-events",[2082],{"type":27,"value":2083},"2. Weak Events",{"type":22,"tag":23,"props":2085,"children":2086},{},[2087,2094,2096,2103,2105,2112],{"type":22,"tag":63,"props":2088,"children":2091},{"href":2089,"rel":2090},"https://wwww.seedindustries.com/blog/x/",[301],[2092],{"type":27,"value":2093},"Xavier Musy’s web log",{"type":27,"value":2095}," has an interesting C# class called ",{"type":22,"tag":63,"props":2097,"children":2100},{"href":2098,"rel":2099},"https://www.seedindustries.com/blog/x/2004_06_01_archive.html",[301],[2101],{"type":27,"value":2102},"WeakMulticastDelegate",{"type":27,"value":2104}," that works in a similar way to normal delegates but uses the ",{"type":22,"tag":63,"props":2106,"children":2109},{"href":2107,"rel":2108},"https://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemweakreferenceclasstopic.asp",[301],[2110],{"type":27,"value":2111},"WeakReference",{"type":27,"value":2113}," class in .NET to allow referencing an object that can be garbage collected.",{"type":22,"tag":23,"props":2115,"children":2116},{},[2117],{"type":27,"value":2118},"This solution also prevents subscribers from subscribing more than once and also from subscribers throwing an exception that stops other subscribers receiving this event (in .NET an exception will break the chain).",{"type":22,"tag":23,"props":2120,"children":2121},{},[2122,2124,2130,2132,2138],{"type":27,"value":2123},"Unfortunately it requires that you publish an event by writing ",{"type":22,"tag":272,"props":2125,"children":2127},{"className":2126},[],[2128],{"type":27,"value":2129},"Add",{"type":27,"value":2131}," and ",{"type":22,"tag":272,"props":2133,"children":2135},{"className":2134},[],[2136],{"type":27,"value":2137},"Remove",{"type":27,"value":2139}," methods for your event, not possible directly under VB.NET but possible with the following workaround:",{"type":22,"tag":527,"props":2141,"children":2142},{},[2143,2177,2197,2336,2341,2361],{"type":22,"tag":142,"props":2144,"children":2145},{},[2146,2148,2153,2155,2160,2162,2168,2169,2175],{"type":27,"value":2147},"Create a ",{"type":22,"tag":255,"props":2149,"children":2150},{},[2151],{"type":27,"value":2152},"C# Class Library",{"type":27,"value":2154}," project inside your solution and add the ",{"type":22,"tag":272,"props":2156,"children":2158},{"className":2157},[],[2159],{"type":27,"value":2102},{"type":27,"value":2161}," code to it (remember to put ",{"type":22,"tag":272,"props":2163,"children":2165},{"className":2164},[],[2166],{"type":27,"value":2167},"using System;",{"type":27,"value":2131},{"type":22,"tag":272,"props":2170,"children":2172},{"className":2171},[],[2173],{"type":27,"value":2174},"using System.Reflection;",{"type":27,"value":2176}," at the top of the file)",{"type":22,"tag":142,"props":2178,"children":2179},{},[2180,2182,2188,2189,2195],{"type":27,"value":2181},"Create a new MyPublisherWorkaround class in the C# project with the ",{"type":22,"tag":272,"props":2183,"children":2185},{"className":2184},[],[2186],{"type":27,"value":2187},"private WeakMulticastDelegate",{"type":27,"value":2131},{"type":22,"tag":272,"props":2190,"children":2192},{"className":2191},[],[2193],{"type":27,"value":2194},"public event EventHandler",{"type":27,"value":2196}," code blocks described in Xavier’s article.",{"type":22,"tag":142,"props":2198,"children":2199},{},[2200,2202],{"type":27,"value":2201},"In MyPublisherWorkaround create a method to fire your event (because .NET does not let you raise events defined in a parent class) e.g:\n",{"type":22,"tag":749,"props":2203,"children":2205},{"className":918,"code":2204,"language":920,"meta":10,"style":10},"protected internal void Changed(EventArgs e) {\n if (this.weakEventHandler != null) weakEventHandler.Invoke(new object[] { this, e } );\n}```\n",[2206],{"type":22,"tag":272,"props":2207,"children":2208},{"__ignoreMap":10},[2209,2251,2328],{"type":22,"tag":759,"props":2210,"children":2211},{"class":761,"line":91},[2212,2217,2222,2227,2232,2236,2241,2247],{"type":22,"tag":759,"props":2213,"children":2214},{"style":777},[2215],{"type":27,"value":2216},"protected",{"type":22,"tag":759,"props":2218,"children":2219},{"style":777},[2220],{"type":27,"value":2221}," internal",{"type":22,"tag":759,"props":2223,"children":2224},{"style":765},[2225],{"type":27,"value":2226}," void",{"type":22,"tag":759,"props":2228,"children":2229},{"style":807},[2230],{"type":27,"value":2231}," Changed",{"type":22,"tag":759,"props":2233,"children":2234},{"style":771},[2235],{"type":27,"value":954},{"type":22,"tag":759,"props":2237,"children":2238},{"style":783},[2239],{"type":27,"value":2240},"EventArgs",{"type":22,"tag":759,"props":2242,"children":2244},{"style":2243},"--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic",[2245],{"type":27,"value":2246}," e",{"type":22,"tag":759,"props":2248,"children":2249},{"style":771},[2250],{"type":27,"value":1143},{"type":22,"tag":759,"props":2252,"children":2253},{"class":761,"line":81},[2254,2259,2264,2270,2274,2280,2285,2290,2295,2300,2304,2309,2314,2319,2323],{"type":22,"tag":759,"props":2255,"children":2256},{"style":821},[2257],{"type":27,"value":2258}," if",{"type":22,"tag":759,"props":2260,"children":2261},{"style":771},[2262],{"type":27,"value":2263}," (",{"type":22,"tag":759,"props":2265,"children":2267},{"style":2266},"--shiki-default:#DF69BA;--shiki-default-font-style:inherit;--shiki-dark:#BD93F9;--shiki-dark-font-style:italic",[2268],{"type":27,"value":2269},"this",{"type":22,"tag":759,"props":2271,"children":2272},{"style":771},[2273],{"type":27,"value":112},{"type":22,"tag":759,"props":2275,"children":2277},{"style":2276},"--shiki-default:#35A77C;--shiki-dark:#F8F8F2",[2278],{"type":27,"value":2279},"weakEventHandler",{"type":22,"tag":759,"props":2281,"children":2282},{"style":777},[2283],{"type":27,"value":2284}," !=",{"type":22,"tag":759,"props":2286,"children":2287},{"style":874},[2288],{"type":27,"value":2289}," null",{"type":22,"tag":759,"props":2291,"children":2292},{"style":771},[2293],{"type":27,"value":2294},") weakEventHandler.",{"type":22,"tag":759,"props":2296,"children":2297},{"style":807},[2298],{"type":27,"value":2299},"Invoke",{"type":22,"tag":759,"props":2301,"children":2302},{"style":771},[2303],{"type":27,"value":954},{"type":22,"tag":759,"props":2305,"children":2306},{"style":821},[2307],{"type":27,"value":2308},"new",{"type":22,"tag":759,"props":2310,"children":2311},{"style":765},[2312],{"type":27,"value":2313}," object",{"type":22,"tag":759,"props":2315,"children":2316},{"style":771},[2317],{"type":27,"value":2318},"[] { ",{"type":22,"tag":759,"props":2320,"children":2321},{"style":2266},[2322],{"type":27,"value":2269},{"type":22,"tag":759,"props":2324,"children":2325},{"style":771},[2326],{"type":27,"value":2327},", e } );\n",{"type":22,"tag":759,"props":2329,"children":2330},{"class":761,"line":702},[2331],{"type":22,"tag":759,"props":2332,"children":2333},{"style":771},[2334],{"type":27,"value":2335},"}```\n",{"type":22,"tag":142,"props":2337,"children":2338},{},[2339],{"type":27,"value":2340},"Add the new C# project to your VB.NET project reference and appropriate Imports line at the top of your VB.NET project (obviously).",{"type":22,"tag":142,"props":2342,"children":2343},{},[2344,2346,2352,2354,2360],{"type":27,"value":2345},"Change your ",{"type":22,"tag":272,"props":2347,"children":2349},{"className":2348},[],[2350],{"type":27,"value":2351},"MyPublisher",{"type":27,"value":2353}," class to inherit from ",{"type":22,"tag":272,"props":2355,"children":2357},{"className":2356},[],[2358],{"type":27,"value":2359},"MyPublisherWorkaround",{"type":27,"value":112},{"type":22,"tag":142,"props":2362,"children":2363},{},[2364,2365,2370,2372,2378,2380,2386],{"type":27,"value":2345},{"type":22,"tag":272,"props":2366,"children":2368},{"className":2367},[],[2369],{"type":27,"value":2351},{"type":27,"value":2371}," class to call the ",{"type":22,"tag":272,"props":2373,"children":2375},{"className":2374},[],[2376],{"type":27,"value":2377},"Changed",{"type":27,"value":2379}," method (or whatever you named it) instead of executing ",{"type":22,"tag":272,"props":2381,"children":2383},{"className":2382},[],[2384],{"type":27,"value":2385},"RaiseEvent",{"type":27,"value":2387}," directly (because of aforementioned restrictions in the .NET framework).",{"type":22,"tag":367,"props":2389,"children":2391},{"id":2390},"_3-weak-delegates",[2392],{"type":27,"value":2393},"3. Weak Delegates",{"type":22,"tag":23,"props":2395,"children":2396},{},[2397,2404,2406,2413,2415,2422],{"type":22,"tag":63,"props":2398,"children":2401},{"href":2399,"rel":2400},"https://weblogs.asp.net/greg_schechter/",[301],[2402],{"type":27,"value":2403},"Greg Schechter",{"type":27,"value":2405}," on the Microsoft ",{"type":22,"tag":63,"props":2407,"children":2410},{"href":2408,"rel":2409},"https://msdn.microsoft.com/Longhorn/understanding/pillars/avalon/default.aspx",[301],[2411],{"type":27,"value":2412},"Avalon",{"type":27,"value":2414}," team has written an article about this very problem and his solution of ",{"type":22,"tag":63,"props":2416,"children":2419},{"href":2417,"rel":2418},"https://weblogs.asp.net/greg_schechter/archive/2004/05/27/143605.aspx",[301],[2420],{"type":27,"value":2421},"Weak Delegates",{"type":27,"value":112},{"type":22,"tag":23,"props":2424,"children":2425},{},[2426],{"type":27,"value":2427},"Our scenario would become:",{"type":22,"tag":527,"props":2429,"children":2430},{},[2431,2436,2441,2446],{"type":22,"tag":142,"props":2432,"children":2433},{},[2434],{"type":27,"value":2435},"MyPublisher instance receives request to add instance of MySubscriber to event.",{"type":22,"tag":142,"props":2437,"children":2438},{},[2439],{"type":27,"value":2440},"MyPublisher creates new instance of own internal class derived from WeakReference.",{"type":22,"tag":142,"props":2442,"children":2443},{},[2444],{"type":27,"value":2445},"This WeakReference instance actually contains the reference to the target object.",{"type":22,"tag":142,"props":2447,"children":2448},{},[2449],{"type":27,"value":2450},"The WeakReference instance’s event handler (with the same signature) is added to the event listener.",{"type":22,"tag":23,"props":2452,"children":2453},{},[2454],{"type":27,"value":2455},"While this works in VB.NET it has a problem in that the class derived from WeakReference requires all subscribers to either be the same class, derived from a common parent or support a specific interface that defines this event handler.",{"type":22,"tag":23,"props":2457,"children":2458},{},[2459,2461,2466],{"type":27,"value":2460},"This defeats much of the point of delegates although it ",{"type":22,"tag":255,"props":2462,"children":2463},{},[2464],{"type":27,"value":2465},"might",{"type":27,"value":2467}," be possible to rework his code so that WeakReference stores a weak reference to the given event handler and not the object implementing it.",{"type":22,"tag":242,"props":2469,"children":2471},{"id":2470},"stepping-back",[2472],{"type":27,"value":2473},"Stepping back",{"type":22,"tag":23,"props":2475,"children":2476},{},[2477,2479,2486],{"type":27,"value":2478},"WinForms is focused around the Win32 API itself and not what modern applications require from a UI when developing along ",{"type":22,"tag":63,"props":2480,"children":2483},{"href":2481,"rel":2482},"https://c2.com/cgi/wiki?ModelViewController",[301],[2484],{"type":27,"value":2485},"Model-View-Controller",{"type":27,"value":2487}," patterns.",{"type":22,"tag":23,"props":2489,"children":2490},{},[2491,2493,2499,2500,2506,2508,2513,2514,2520],{"type":27,"value":2492},"Compound controls such as TreeView and ListView should deal with interfaces such as ",{"type":22,"tag":272,"props":2494,"children":2496},{"className":2495},[],[2497],{"type":27,"value":2498},"ITreeNode",{"type":27,"value":2131},{"type":22,"tag":272,"props":2501,"children":2503},{"className":2502},[],[2504],{"type":27,"value":2505},"IListViewItem",{"type":27,"value":2507}," and then provide a concrete implementation for backwards compatibility/general use (",{"type":22,"tag":272,"props":2509,"children":2511},{"className":2510},[],[2512],{"type":27,"value":1976},{"type":27,"value":2131},{"type":22,"tag":272,"props":2515,"children":2517},{"className":2516},[],[2518],{"type":27,"value":2519},"ListViewItem",{"type":27,"value":2521},").",{"type":22,"tag":23,"props":2523,"children":2524},{},[2525,2527,2532,2534,2541],{"type":27,"value":2526},"This would solve ",{"type":22,"tag":255,"props":2528,"children":2529},{},[2530],{"type":27,"value":2531},"some",{"type":27,"value":2533}," issues. Check out Skybound’s ",{"type":22,"tag":63,"props":2535,"children":2538},{"href":2536,"rel":2537},"https://www.skybound.ca/developer/visualstyles/default.aspx",[301],[2539],{"type":27,"value":2540},"VisualStyles",{"type":27,"value":2542}," for a free and easy to use fix for the poor theme support.",{"type":22,"tag":54,"props":2544,"children":2545},{},[2546],{"type":22,"tag":23,"props":2547,"children":2548},{},[2549,2551,2557],{"type":27,"value":2550},"Check out the ",{"type":22,"tag":63,"props":2552,"children":2554},{"href":2553},"https://msdn2.microsoft.com/en-us/library/system.windows.weakeventmanager.aspx",[2555],{"type":27,"value":2556},"WeakEventManager in .NET 3.5",{"type":27,"value":2558}," for an event mechanism that is immune to this problem.",{"type":22,"tag":23,"props":2560,"children":2561},{},[2562],{"type":22,"tag":75,"props":2563,"children":2564},{},[2565],{"type":27,"value":79},{"type":22,"tag":482,"props":2567,"children":2568},{},[2569],{"type":27,"value":1274},{"title":10,"searchDepth":81,"depth":81,"links":2571},[2572,2576,2581],{"id":1935,"depth":81,"text":1938,"children":2573},[2574,2575],{"id":1950,"depth":702,"text":1953},{"id":1961,"depth":702,"text":1964},{"id":2009,"depth":81,"text":2012,"children":2577},[2578,2579,2580],{"id":2031,"depth":702,"text":2034},{"id":2080,"depth":702,"text":2083},{"id":2390,"depth":702,"text":2393},{"id":2470,"depth":81,"text":2473},"content:blog:2005:2005-01-19-lapsedlistenersmemoryleaksinsubscriberpublisherscenarios.md","blog/2005/2005-01-19-lapsedlistenersmemoryleaksinsubscriberpublisherscenarios.md","blog/2005/2005-01-19-lapsedlistenersmemoryleaksinsubscriberpublisherscenarios","/blog/2005/01/19/lapsedlistenersmemoryleaksinsubscriberpublisherscenarios/",881,1775115128730]