<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MoebiusIT</title>
	<atom:link href="http://moebiusit.com/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://moebiusit.com/blog</link>
	<description></description>
	<lastBuildDate>Tue, 29 Nov 2011 17:31:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Custom type conversion</title>
		<link>http://moebiusit.com/blog/2010/11/27/custom-type-conversion/</link>
		<comments>http://moebiusit.com/blog/2010/11/27/custom-type-conversion/#comments</comments>
		<pubDate>Sat, 27 Nov 2010 07:47:05 +0000</pubDate>
		<dc:creator>Alejandro Mehring</dc:creator>
				<category><![CDATA[MoebiusIT]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[type conversion]]></category>

		<guid isPermaLink="false">http://moebiusit.com/blog/?p=5</guid>
		<description><![CDATA[It&#8217;s always useful to redefine operators on your custom types, for they make it easier to follow and mantain the code you&#8217;re writting. So why not do the same with casting operators? Intro Sometimes you create a new type and &#8230; <a href="http://moebiusit.com/blog/2010/11/27/custom-type-conversion/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s always useful to redefine operators on your custom types, for they make it easier to follow and mantain the code you&#8217;re writting.<br />
So why not do the same with casting operators?<span id="more-6"></span></p>
<h2>Intro</h2>
<p>Sometimes you create a new type and need to redefine certain operators in order to make your life easier. For instance, matrix or complex numbers operations. It&#8217;s way more comfortable (and understandable) to write a simple plus symbol than calling a method to perform an addition.</p>
<p>It&#8217;s pretty straightforward: you define a static method with the keyword <em>operator </em>such as this:</p>
<p>[sourcecode language='csharp']</p>
<p>//Complex addition<br />
public static Complex operator +(Complex a, Complex b)<br />
{<br />
a.Re = a.Re + b.Re;<br />
a.Im = a.Im + b.Im;<br />
return a;<br />
}<br />
[/sourcecode]</p>
<p>And then you just forget about that and simply use it! So&#8230; why not also forget about type conversion?</p>
<h2>Your custom type conversion</h2>
<p>In certain cases you need to create a constructor that receives an object and &#8220;converts&#8221; it to another, or a method to change it backwards.</p>
<p>Supose you have Rectangular and Polar types. You could have the following code to perform the conversion between each of them:</p>
<p>[sourcecode language='csharp']</p>
<p>//Rectangular type<br />
public Rectangular(Polar polar)<br />
{<br />
X = polar.R * Math.Cos(polar.Theta);<br />
Y = polar.R * Math.Sin(polar.Theta);<br />
}</p>
<p>public Polar ToPolar()<br />
{<br />
Polar polar = new Polar();<br />
polar.R = Math.Sqrt(X * X + Y * Y);<br />
polar.Theta = Math.Atan2(X, Y);<br />
return polar;<br />
}</p>
<p>//Polar Type<br />
public Polar(Rectangular rectangular)<br />
{<br />
R = Math.Sqrt(rectangular.X * rectangular.X + rectangular.Y * rectangular.Y);<br />
Theta = Math.Atan2(rectangular.X, rectangular.Y);<br />
}</p>
<p>public Rectangular ToRectangular()<br />
{<br />
Rectangular rectangular = new Rectangular();<br />
rectangular.X = R * Math.Cos(Theta);<br />
rectangular.Y = R * Math.Sin(Theta);<br />
return rectangular;<br />
}</p>
<p>//Usage example<br />
Rectangular r = new Rectangular(3,1);<br />
Polar p = new Polar(r);<br />
Polar p2 = r.ToPolar();<br />
Rectangular r2 = new Rectangular(p);</p>
<p>[/sourcecode]</p>
<p>Instead of that, you could define your own type conversion as follows:</p>
<p>[sourcecode language='csharp']</p>
<p>//Polar type<br />
public static explicit operator Polar (Rectangular r) {<br />
Polar p;<br />
p.R = Math.Sqrt(r.X * r.X + r.Y * r.Y);<br />
p.Theta = Math.Atan2(r.X, r.Y);<br />
return p;<br />
}</p>
<p>//Rectangular type<br />
public static explicit operator Rectangular (Polar p) {<br />
Rectangular r= new Rectangular();<br />
r.X = p.R * Math.Cos(p.Theta);<br />
r.Y = p.R * Math.Sin(p.Theta);<br />
return r;<br />
}</p>
<p>[/sourcecode]</p>
<p>And then use it like usual</p>
<p>[sourcecode language='csharp']</p>
<p>Rectangular r = new Rectangular(3,2);<br />
Polar p = (Polar)r;</p>
<p>[/sourcecode]</p>
<h2>A word on method declaration</h2>
<p>The method declared must be <em>static </em>for conversion to work without creating an instance of target type first. It should also be visible in other scopes, so it should be <em>public </em>or at least <em>internal</em>.</p>
<p>The <em>explicit operator</em> expression shows that the conversion must be explicit. You could also define it as <em>implicit</em>, in which case the assignment in our example would only be <em>Polar p = r;</em> I personally dislike this option since it can lead to unwanted results.</p>
<p>The name of the method must be the name of the target type of conversion.</p>
<p>The parameter is the instance to be converted.</p>
<address> </address>
<h2>References</h2>
<p><a title="Casting in c#" href="http://msdn.microsoft.com/en-us/library/ms173105(VS.80).aspx" target="_blank">Casting</a></p>
<p><a title="Explicit keyword" href="http://msdn.microsoft.com/en-us/library/xhbhezf4(VS.80).aspx" target="_blank">Explicit Keyword</a></p>
<p><a title="Implicit Keyword" href="http://msdn.microsoft.com/en-us/library/z5z9kes2(VS.80).aspx" target="_blank">Implicit Keyword</a></p>
]]></content:encoded>
			<wfw:commentRss>http://moebiusit.com/blog/2010/11/27/custom-type-conversion/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Changing dll settings without re-compiling</title>
		<link>http://moebiusit.com/blog/2010/07/13/changing-dll-settings-without-re-compiling/</link>
		<comments>http://moebiusit.com/blog/2010/07/13/changing-dll-settings-without-re-compiling/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 13:48:15 +0000</pubDate>
		<dc:creator>Pablo Romano</dc:creator>
				<category><![CDATA[MoebiusIT]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[dll]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[runtime]]></category>
		<category><![CDATA[settings]]></category>

		<guid isPermaLink="false">http://moebiusit.com/blog/pabloromano/2009/07/13/changing-dll-settings-without-compiling/</guid>
		<description><![CDATA[C# settings are a wonderful thing; they provide an easy and powerful way to customize compiled applications by changing data stored in external files. They work great when they are part of the startup project but &#8211; there&#8217;s always a &#8230; <a href="http://moebiusit.com/blog/2010/07/13/changing-dll-settings-without-re-compiling/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>C# settings are a wonderful thing; they provide an easy and powerful way to customize compiled applications by changing data stored in external files.</p>
<p>They work great when they are part of the startup project but &#8211; there&#8217;s always a but that ends up in a post &#8211; when you use them on a referenced project they always take their default value.</p>
<p>After three days of poring over blogs, framework documentation and other resources I concluded that no solution or work-around was readily available, even though the problem seems to be well known.</p>
<p>I tried several approaches, this post will describe the one I consider the best. You can skip the discussion and download the code directly <a href="http://moebiusit.com/blog/wp-content/SettingsInDllTest.rar">here</a> or <span id="more-136"></span>keep on reading to find out how it&#8217;s done and download it later.</p>
<h2>Scenario</h2>
<p>I stumbled on this problem while testing the new <a href="http://moebiusit.com/site/det_alto_palermo.php">&#8220;multi-function terminal&#8221; </a> for the <a title="Dot Baires Shopping" href="http://www.dotbaires.com.ar/" target="_blank">Dot Baires Shopping</a>. The application is deployed in many terminals which communicate with external services providing their identity, for example &#8216;abc123&#8242;, also each machine has its own timeout parameters, for example time to go back to the home screen. <strong>These parameters may vary from one machine to the next.</strong></p>
<p>&#8220;Hardcoding&#8221; the values was obviously not an option, the sole idea of one compilation per station was disturbing. I could, of course, have written a parser and stored these values in an XML, but I already had the Settings feature provided by the framework and I wanted to use it.</p>
<h2>How Settings Work</h2>
<p>When you add a &#8220;Settings&#8221; to a project, (I will name it &#8220;MySettings&#8221; in these example), Visual Studio automatically adds an app.config file, it also keeps synchronized the MySettings.settings file with MySettings.Designer.cs and the app.config, as you can see <strong>the default value of the setting is stored in three places.</strong></p>
<p>When the project is compiled a file named ProjectName.exe/dll.config is generated, the .exe.config file has no mystery, you have compiled a solution and the settings are used in the startup project, <strong>change this file and you will see the changes in the application.</strong></p>
<h2>Settings in a DLL</h2>
<p>Well, settings were beautiful so far, but here they get a little tricky &#8211; a nice word for buggy.</p>
<p>Let&#8217;s imagine you are building a solution that has a Persister, a Credit Card Manager and UI interaction, you want to reuse your Credit Card code in other projects so you put it in a project of its own with its own configuration values in the Settings, probably a Store Id for the transactions and other things . Now you compile the Credit Card project&#8230;, great!!<strong> the compiler generated a CreditCard.dll.config file which holds the Store Id as we expected!</strong></p>
<p><strong>Well, let&#8217;s compile the solution then!</strong> Mmm&#8230;, but where is the CreditCard.dll.config now??, to make a long story short<strong> </strong>it&#8217;s not there, there is one in the CreditCard project folder bindebug, but if you copy it to the directory the solution was build into <strong>you will find out the values will still be the default no matter what you do.</strong> The reason is the .config files are only used in the startup project.</p>
<h2>My approach</h2>
<p>After three days reading I couldn&#8217;t find a solution I liked.</p>
<p>I like Settings, <strong>they provide a good way to save data in files without having to write a parser, also the data are strongly typed and have default values</strong> in case the .config is not found.</p>
<p>After all the Settings are a main gear in the .Net Framework, so I started thinking of a work-around.</p>
<p>My approach was simple, if the .dll.config file wasn&#8217;t being read <strong>I had to add a file of my own</strong>, so I thought of the configSource attribute that can be applied to the userSettings, if you want to read more go to <a href="http://aspalliance.com/820">http://aspalliance.com/820</a>.</p>
<h2>Test solution</h2>
<h3>Components of the solution</h3>
<ol>
<li>Winform project &#8220;Test&#8221; with a form &#8220;TestForm&#8221;</li>
<li>Dll project &#8220;DllWithSettings&#8221; with Settings &#8220;MySettings.settings&#8221;</li>
<li>I added a class &#8220;SettingsConsumerClass&#8221; in the &#8220;DllWithSettings&#8221; project to access settings dependent values from the winform project &#8220;Test&#8221;</li>
</ol>
<p>TestForm.cs</p>
<p>[sourcecode language='csharp']<br />
using System.Windows.Forms;</p>
<p>namespace Test<br />
{<br />
public partial class TestForm : Form<br />
{<br />
public TestForm()<br />
{<br />
InitializeComponent();<br />
MessageBox.Show(&#8220;String Value: &#8221; + DllWithSettings.SettingsConsumerClass.StringValue + &#8220;nInt Value: &#8221; + DllWithSettings.SettingsConsumerClass.IntValue);<br />
Focus();<br />
}<br />
}<br />
}<br />
[/sourcecode]<br />
SettingsConsumerClass.cs<br />
[sourcecode language='csharp']<br />
namespace DllWithSettings<br />
{<br />
public static class SettingsConsumerClass<br />
{<br />
private static readonly int _intValue = MySettings.Default.TestValue;<br />
private static readonly string _stringValue = MySettings.Default.AnotherTestValue;</p>
<p>public static int IntValue<br />
{<br />
get<br />
{<br />
return _intValue;<br />
}<br />
}</p>
<p>public static string StringValue<br />
{<br />
get<br />
{<br />
return _stringValue;<br />
}<br />
}<br />
}<br />
}<br />
[/sourcecode]</p>
<p>If you try what we have written so far you&#8217;ll see that it compiles, and it also shows the settings, but if you look for the output directory you will see <strong>there&#8217;s no .config file and if you copy the one generated in the &#8220;DllWithSettings&#8221; output directory and change it nothing will happen.</strong></p>
<h2>Making it work</h2>
<p>First I extracted the settings from the app.config and put them in DllWithSettings.config, this means changing the app.config in the &#8220;DllWithSettings&#8221; project from</p>
<p>[sourcecode language='csharp']</p>
<p><!--?xml version="1.0" encoding="utf-8" ?--></p>
<section name="DllWithSettings.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowexedefinition="MachineToLocalUser" requirepermission="false"></section>
<p>123</p>
<p>Compiled</p>
<p>[/sourcecode]</p>
<p>to</p>
<p>[sourcecode language='csharp']</p>
<p><!--?xml version="1.0" encoding="utf-8" ?--></p>
<section name="DllWithSettings.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowexedefinition="MachineToLocalUser" requirepermission="false"></section>
<p>[/sourcecode]</p>
<p>DllWithSettings.config<br />
[sourcecode language='csharp']</p>
<p><!--?xml version="1.0" encoding="utf-8" ?--></p>
<p>123</p>
<p>Compiled<br />
[/sourcecode]</p>
<blockquote><p><strong>The &#8220;Copy to output directory&#8221; of this file must be set to &#8220;Copy if newer&#8221; so it&#8217;s available at runtime.</strong></p></blockquote>
<p>The second and last thing to do is to <strong>reference this file in the app.config of the startup project </strong>too &#8211; if you don&#8217;t have an app.config add one.</p>
<p>Because the settings defined in the app.config of the startup project always override others, referencing MySettings.config and making this settings belong to the correct namespace overrides the defaults of &#8220;DllWithSettings&#8221;, allowing to change them without recompiling.</p>
<p>The app.config in &#8220;Test&#8221; changes from</p>
<p>[sourcecode language='csharp']</p>
<p><!--?xml version="1.0" encoding="utf-8" ?--><br />
[/sourcecode]</p>
<p>to</p>
<p>[sourcecode language='csharp']</p>
<p><!--?xml version="1.0" encoding="utf-8" ?--></p>
<section name="DllWithSettings.MySettings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowexedefinition="MachineToLocalUser" requirepermission="false"></section>
<p>[/sourcecode]</p>
<p>You can download the source code <a href="http://moebiusit.com/blog/wp-content/SettingsInDllTest.rar">here</a>.</p>
<p><strong>Well, that&#8217;s all, hope you learned something useful, please post if you have any doubts or comments!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://moebiusit.com/blog/2010/07/13/changing-dll-settings-without-re-compiling/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>VB6 autonomous DBF file manager</title>
		<link>http://moebiusit.com/blog/2010/07/01/vb6-autonomous-dbf-file-manager/</link>
		<comments>http://moebiusit.com/blog/2010/07/01/vb6-autonomous-dbf-file-manager/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 18:36:46 +0000</pubDate>
		<dc:creator>Diego Jäger</dc:creator>
				<category><![CDATA[MoebiusIT]]></category>
		<category><![CDATA[DBF]]></category>
		<category><![CDATA[legacy apps]]></category>
		<category><![CDATA[VB6]]></category>

		<guid isPermaLink="false">http://moebiusit.com/blog/?p=103</guid>
		<description><![CDATA[Motivation If you ever had to deal with legacy apps, you must have come across data stored in DBF files; and  if you are  like me, you find it frustrating  that support for  DBFs will depend on factors such as &#8230; <a href="http://moebiusit.com/blog/2010/07/01/vb6-autonomous-dbf-file-manager/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><strong>Motivation</strong></p>
<p>If you ever had to deal with legacy apps, you must have come across data stored in DBF files; and  if you are  like me, you find it frustrating  that support for  DBFs will depend on factors such as which service packs are installed wherever it is your program must run &#8211; and this is often out of your  control, since the &#8220;old&#8221; system must remain  operative, at least until you migrate the data&#8230;or maybe you have to exchange data with such an app on a regular basis, since your client only wants to move part of his business to a new platform.</p>
<p>After one too many such frustrations, I decided to write a small utility that would kill this problem for me once and for all &#8211; well, at least as far as clients using VB6 go. No rocket science, but it gets the job done, and I can forget about dll-activex-SP swamps and concentrate on doing my job.</p>
<p>Even though I wrote this for myself, the code is pretty clear (I was planning on reusing it, and I did), so you  can simply download it from <a href="http://moebiusit.com/blog/wp-content/dbfManager.rar" target="_self">here</a>, add it to your VB6 project as an existing module and you&#8217;re ready to go.</p>
<p><span id="more-103"></span></p>
<p><strong>DBF files  &#8211; a brief note on an old technology</strong></p>
<p>A <em>DBF</em> file is essentially a self-contained database table, complete with both field definitions and data.</p>
<p>Each field definition is persisted in a structure (type dbfFieldDef in the code) where the main features are field name, type (I only consider Character (string), Numeric, Date and Logical (Boolean)), size, and number of decimals. Most other properties, interesting though they may be, are not used in my code and if you don&#8217;t plan on changing it you may basically ignore them.</p>
<p>As opposed to modern relational databases, DBFS have a &#8220;record number&#8221; property (that may change if you delete and expunge some other  record), which is simply a pointer to the physical position of the record in the table.</p>
<p>There is also a global header that takes up the first 32 bytes in the file (type dbfHeader).  Field cIsDbf must be 0&#215;03 if we are talking about a DBF 3 file, the kind used by CA-Clipper.  ymlLUpdate  holds the date the file was last updated. The rest of the fields are both useful (e.g.to compute the fieldCount property when you open a new file) and self-explanatory. Note that lRecCount is actually redundant, you could compute it from iHeaderSize , iRecordSize and the physical file size&#8230;(or you could figure one of the other parameters reading lRecCount), so you can easily &#8220;fix&#8221; a DBF file if the record count is lost &#8211; I saw this happen quite a few times.</p>
<p><strong>So what does this code do?</strong></p>
<p>As you would expect it lets you:</p>
<p><strong>Open and close the DBF</strong><br />
Public Function dbfOpen(ByVal fn As String, mgr As dbfmanager) As Integer<br />
Public Function dbfClose(mgr As dbfmanager) As Integer</p>
<p><strong>Move around the table</strong> (<em>when you &#8216;go&#8217; to a record, you also read it and<br />
you can get its field using dbField()</em>)<br />
Public Function dbGoTo(i As Integer) As Integer  &#8216;Go to record nr I<br />
Public Function dbGoTop() As Integer             &#8216;Go to 1st record<br />
Public Function dbfMoveFirst()                   &#8216;actually an alias for dbGoTop()<br />
Public Function dbfMoveNext()                    &#8216;move to next record, (test EOF)<br />
Public Function dbGoBottom() As Integer          &#8216;go to last record</p>
<p><strong>Write data</strong><br />
Public Function dbAppendBlank() As Integer &#8216;append a blank record at the bottom,<br />
&#8216;it becomes the current record<br />
Public Function dbReplace(ByVal fieldName As String, v As Variant) As Integer<br />
&#8216;set field value for current record<br />
&#8216;the record will not be written until you commit()<br />
Public Function dbCommit() As Integer      &#8216;write changes to disk</p>
<p><strong>Read  data</strong><br />
Property Get dbField(ByVal fieldName As String) As Variant  &#8216;read a field (column) value<br />
Property Get EOF() As Boolean<br />
Property Get MyBookmark() As Integer                        &#8216;bookmark a record<br />
Property Let MyBookmark(i As Integer)                       &#8216;go to bookmarked record<strong><br />
</strong></p>
<p><strong>Utility funcions</strong><br />
Function ZFormat(nNro, nIntegers, nDecimals)                &#8216;I find this formatting function more user friendly than VBs strings<br />
Public Function stripnulls(ByVal s As String) As String     &#8216;deal with c like null terminators</p>
<p><em>A note on implementation</em></p>
<p>The code includes a 2nd class module &#8216;dbfmanager.cls&#8217;  &#8211; The idea is that you may want to manage a collection of dbfs as a single object, so I provided this class to do things like closing a bunch of dbf&#8217;s in a single command, etc. I never got around to doing it&#8230;</p>
]]></content:encoded>
			<wfw:commentRss>http://moebiusit.com/blog/2010/07/01/vb6-autonomous-dbf-file-manager/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

