[{"data":1,"prerenderedAt":371},["ShallowReactive",2],{"blog:2005:vbnet-to-c-conversion":3,"blogMore-Development":357,"comments-vbnet-to-c-conversion":370},{"id":4,"title":5,"body":6,"category":337,"commentCount":338,"date":339,"description":340,"excerpt":341,"extension":342,"filenames":343,"hidden":344,"image":343,"meta":345,"minutes":112,"navigation":346,"path":347,"seo":348,"showCategory":343,"stem":349,"tags":350,"updated":343,"url":354,"wordCount":355,"__hash__":356},"content\u002Fblog\u002F2005\u002Fvbnet-to-c-conversion.md","VB.NET to C# conversion",{"type":7,"value":8,"toc":328},"minimark",[9,27,32,40,49,54,135,138,186,189,193,196,200,203,207,210,242,245,272,276,279,318,324],[10,11,12,13,20,21,26],"p",{},"I recently converted some components on a project from VB.NET to C#, mainly for overloading and better tool support (such as ",[14,15,19],"a",{"href":16,"rel":17},"https:\u002F\u002Fwww.jetbrains.com\u002Fresharper\u002F",[18],"nofollow","ReSharper","). Some of the existing code was generated from my own ",[14,22,25],{"href":23,"rel":24},"https:\u002F\u002Fwww.codesmithtools.com",[18],"CodeSmith"," templates, so a small rewrite to generate C# handled most of that.",[28,29,31],"h2",{"id":30},"vbnet-to-c-converter-131","VB.NET to C# Converter 1.31",[10,33,34,35,39],{},"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 ",[14,36,31],{"href":37,"rel":38},"https:\u002F\u002Fwww.vbconversions.com",[18]," a spin.",[10,41,42,43,48],{},"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 ",[14,44,47],{"href":45,"rel":46},"https:\u002F\u002Fmsdn.microsoft.com\u002Flibrary\u002Fdefault.asp?url=\u002Flibrary\u002Fen-us\u002Fcpgenref\u002Fhtml\u002Fcpconnetframeworkdesignguidelines.asp",[18],"Microsoft’s guidelines","…",[50,51,53],"h3",{"id":52},"problem-1-ignores-all-instanceclass-variables-default-assignments-and-constructors","Problem 1: Ignores all instance\u002Fclass variables default assignments and constructors.",[55,56,61],"pre",{"className":57,"code":58,"language":59,"meta":60,"style":60},"language-vb shiki shiki-themes everforest-light dracula","Public Class Mine\n  Public myObj As MyClass = New MyClass()\n  Private myVar As MyEnumeration = MyEnumeration.MyDefault\nEnd Class\n","vb","",[62,63,64,81,110,129],"code",{"__ignoreMap":60},[65,66,69,73,77],"span",{"class":67,"line":68},"line",1,[65,70,72],{"class":71},"s9HRq","Public",[65,74,76],{"class":75},"sXAHl"," Class ",[65,78,80],{"class":79},"s6Vpi","Mine\n",[65,82,84,87,90,93,97,100,103,107],{"class":67,"line":83},2,[65,85,86],{"class":71},"  Public",[65,88,89],{"class":79}," myObj ",[65,91,92],{"class":71},"As",[65,94,96],{"class":95},"snuxY"," MyClass",[65,98,99],{"class":71}," =",[65,101,102],{"class":75}," New ",[65,104,106],{"class":105},"sS4Kt","MyClass",[65,108,109],{"class":79},"()\n",[65,111,113,116,119,121,124,126],{"class":67,"line":112},3,[65,114,115],{"class":71},"  Private",[65,117,118],{"class":79}," myVar ",[65,120,92],{"class":71},[65,122,123],{"class":95}," MyEnumeration",[65,125,99],{"class":71},[65,127,128],{"class":79}," MyEnumeration.MyDefault\n",[65,130,132],{"class":67,"line":131},4,[65,133,134],{"class":75},"End Class\n",[10,136,137],{},"Suddenly becomes;",[55,139,143],{"className":140,"code":141,"language":142,"meta":60,"style":60},"language-csharp shiki shiki-themes everforest-light dracula","public class Mine {\n  public MyClass myObj;\n  private MyEnumeration myVar;\n}\n","csharp",[62,144,145,161,171,181],{"__ignoreMap":60},[65,146,147,150,154,158],{"class":67,"line":68},[65,148,149],{"class":71},"public",[65,151,153],{"class":152},"smiwp"," class",[65,155,157],{"class":156},"sPLAf"," Mine",[65,159,160],{"class":79}," {\n",[65,162,163,166,168],{"class":67,"line":83},[65,164,165],{"class":71},"  public",[65,167,96],{"class":95},[65,169,170],{"class":79}," myObj;\n",[65,172,173,176,178],{"class":67,"line":112},[65,174,175],{"class":71},"  private",[65,177,123],{"class":95},[65,179,180],{"class":79}," myVar;\n",[65,182,183],{"class":67,"line":131},[65,184,185],{"class":79},"}\n",[10,187,188],{},"While the object not being created will soon throw an exception, the defaults for value types is a little harder to track down.",[50,190,192],{"id":191},"problem-2-gives-up-on-select-case-statements-after-the-first-case-commenting-out-the-others","Problem 2: Gives up on Select Case statements after the first case, commenting out the others.",[10,194,195],{},"In at least one case it got so confused it commented out substantially more code after the case statement too.",[50,197,199],{"id":198},"problem-3-declares-additional-unnecessary-name-spaces-including-the-one-for-microsoftvisualbasic-despite-not-needing-it","Problem 3: Declares additional unnecessary name-spaces including the one for Microsoft.VisualBasic, despite not needing it.",[10,201,202],{},"Either we hadn’t used the CDate\u002FCInt\u002FCLng functions or it had converted them)…",[50,204,206],{"id":205},"problem-4-all-with-statements-are-converted-to-be-variable-with1-even-when-the-old-with-clause-was-a-simple-case","Problem 4: All with statements are converted to be variable with1, even when the old with clause was a simple case.",[10,208,209],{},"For example:",[55,211,213],{"className":57,"code":212,"language":59,"meta":60,"style":60},"With myObj\n  .doThis()\nEnd With\n",[62,214,215,223,233],{"__ignoreMap":60},[65,216,217,220],{"class":67,"line":68},[65,218,219],{"class":152},"With",[65,221,222],{"class":79}," myObj\n",[65,224,225,228,231],{"class":67,"line":83},[65,226,227],{"class":79},"  .",[65,229,230],{"class":105},"doThis",[65,232,109],{"class":79},[65,234,235,239],{"class":67,"line":112},[65,236,238],{"class":237},"sf-jo","End",[65,240,241],{"class":152}," With\n",[10,243,244],{},"becomes;",[55,246,248],{"className":140,"code":247,"language":142,"meta":60,"style":60},"MyClass with1 = myObj;\nwith1.doThis();\n",[62,249,250,262],{"__ignoreMap":60},[65,251,252,254,257,260],{"class":67,"line":68},[65,253,106],{"class":95},[65,255,256],{"class":79}," with1 ",[65,258,259],{"class":71},"=",[65,261,170],{"class":79},[65,263,264,267,269],{"class":67,"line":83},[65,265,266],{"class":79},"with1.",[65,268,230],{"class":105},[65,270,271],{"class":79},"();\n",[28,273,275],{"id":274},"using-both-vbnet-and-c-together","Using both VB.NET and C# together",[10,277,278],{},"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.",[280,281,282,286,306,309,312,315],"ul",{},[283,284,285],"li",{},"Don’t use the Visual Basic CInt\u002FCDate\u002FIsNumber 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.",[283,287,288,289],{},"Bring the source closer together by;\n",[280,290,291,294,297,300,303],{},[283,292,293],{},"Putting brackets round if conditions in VB.NET",[283,295,296],{},"Putting quotes round region declarations in C#",[283,298,299],{},"Putting attributes on separate lines in VB.NET with a _ suffix",[283,301,302],{},"Dropping the implementation and interface declarations onto a new line in C#",[283,304,305],{},"Dropping the base(whatever) onto a new line in C# constructors (good idea anyway)",[283,307,308],{},"Check out the “Differences Between Visual Basic .NET and Visual C# .NET” white paper on MSDN",[283,310,311],{},"Use VBCommenter to get C# style XML comment documentation.",[283,313,314],{},"Use Monitor.Enter() instead of VB.NET's synclock and C#’s lock.",[283,316,317],{},"Be aware of the differences regarding math! VB.NET defaults to floating point precision and rounding when converting with CInt\u002FCLng etc.. C# normally uses integer division and casting truncates.",[10,319,320],{},[321,322,323],"em",{},"[)amien",[325,326,327],"style",{},"html pre.shiki code .s9HRq, html code.shiki .s9HRq{--shiki-default:#F57D26;--shiki-dark:#FF79C6}html pre.shiki code .smiwp, html code.shiki .smiwp{--shiki-default:#F85552;--shiki-dark:#FF79C6}html pre.shiki code .sPLAf, html code.shiki .sPLAf{--shiki-default:#3A94C5;--shiki-dark:#8BE9FD}html pre.shiki code .s6Vpi, html code.shiki .s6Vpi{--shiki-default:#5C6A72;--shiki-dark:#F8F8F2}html pre.shiki code .snuxY, html code.shiki .snuxY{--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic}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);}html pre.shiki code .sS4Kt, html code.shiki .sS4Kt{--shiki-default:#8DA101;--shiki-dark:#50FA7B}html pre.shiki code .sXAHl, html code.shiki .sXAHl{--shiki-default:#3A94C5;--shiki-dark:#FF79C6}html pre.shiki code .sf-jo, html code.shiki .sf-jo{--shiki-default:#8DA101;--shiki-dark:#8BE9FD}",{"title":60,"searchDepth":83,"depth":83,"links":329},[330,336],{"id":30,"depth":83,"text":31,"children":331},[332,333,334,335],{"id":52,"depth":112,"text":53},{"id":191,"depth":112,"text":192},{"id":198,"depth":112,"text":199},{"id":205,"depth":112,"text":206},{"id":274,"depth":83,"text":275},"Development",0,"2005-08-08T02:00:00+00:00","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.","[object Object]","md",null,false,{},true,"\u002Fblog\u002F2005\u002Fvbnet-to-c-conversion",{"title":5,"description":340},"blog\u002F2005\u002Fvbnet-to-c-conversion",[351,352,353],".NET","C#","VB.NET","\u002Fblog\u002F2005\u002Fvbnet-to-c-conversion\u002F",567,"pa_vsiZbju3D8Bs6mWWjceLvg1uAN_d_7X84n5j9d4A",[358,362,366],{"title":359,"date":360,"url":361},"Transactions in the MongoDB EF Core Provider","2025-10-25","\u002Fblog\u002F2025\u002Fmongodb-explicit-transactions\u002F",{"title":363,"date":364,"url":365},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","\u002Fblog\u002F2025\u002Fmongodb-queryable-encryption\u002F",{"title":367,"date":368,"url":369},"Lazy Loading with EF Core Proxies","2025-04-02","\u002Fblog\u002F2025\u002Fef-proxies\u002F",[],1780900534132]