Tuesday, August 18, 2015

VB.Net Interface Implements Again

I thought maybe it was just me, but lots of sites out there with incorrect examples of the Implements Interface in VB.Net. And yet in the Microsoft documentation it goes all the way back to VS2003, so if it was correct at any stage, it was a very long time ago.

https://msdn.microsoft.com/en-us/library/f6x8ydw5%28v=vs.71%29.aspx

perhaps the authors are thinking back to VB6 and ignoring this article:
https://msdn.microsoft.com/en-us/library/4ab292ze%28v=vs.80%29.aspx

Monday, August 17, 2015

.Net Winform config gotcha

Been bitten by this a few times, and now been asked if I remembered it, so putting it up here

You might follow the excellent advice at
http://instinctcoder.com/how-to-read-app-config-file-in-vb-net/

and end up with something like this:


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<sectionGroup name="applicationSettings" type="System.Configuration.ApplicationSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b99a9c59999e099">
<section name="ReportClient.My.MySettings" type="System.Configuration.ClientSettingsSection,System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b99a9c59999e099" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
</startup>
<applicationSettings>
<ReportClient.My.MySettings>
<setting name="SqlServerName" serializeAs="String">
<value>test123</value>
</setting>
<setting name="MainDbName" serializeAs="String">
<value />
</setting>
<setting name="ReportDbName" serializeAs="String">
<value />
</setting>
</ReportClient.My.MySettings>
</applicationSettings>
</configuration>

           
    The trouble is that that the config files are so complicated, it is easy to lose track.

The best thing in this case is to add the following right after the end of  </configSections>
  <appSettings>
    <add key="SqlServerName" value="test123" />   
    <add key="MainDbName" value="" />     
    <add key=="ReportDbName"  value="" /> 
  </appSettings>


and when you are happy, remove the  <applicationSettings> section entirely

C# to VB.Net Gotcha 1: Interfaces

This doesn't happen if you write your interfaces and classes from scratch, but if you are translating from C#
either manually or using an online tool, you might end up with

 A_Service must implement Function XMLData () As IAService.XmlData(id as String)


Public Class A_Service : Implements IAService

Public Function XMLData (id as String) as String
'implementation here
End Function
'....
End Class

and you are thinking "I did, dammit"

Actually you need to explicitly add teh Implements keyword to each function as well. Unlike the C# versions where the implementation is implied by the fact that the class declaration says Implements. I think was how VB6 interfaces worked?

What you need is:

Public Function XMLData (id as String) as String Implments IAService.XMLData

There was a great posting on Stackoverflow for this:
http://stackoverflow.com/questions/666399/cant-see-must-implement-error