<?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>Jamie&#039;s Blog &#187; C#</title>
	<atom:link href="http://jamiei.com/blog/topic/development/win-dev/csharp/feed/" rel="self" type="application/rss+xml" />
	<link>http://jamiei.com/blog</link>
	<description>Delphi Programming, Web Development, General Technology and, of course, Midget Gems</description>
	<lastBuildDate>Mon, 19 Dec 2011 09:19:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
<cloud domain='jamiei.com' port='80' path='/blog/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Diary of a C# to Delphi Prism conversion</title>
		<link>http://jamiei.com/blog/2009/04/diary-of-a-c-to-delphi-prism-conversion/</link>
		<comments>http://jamiei.com/blog/2009/04/diary-of-a-c-to-delphi-prism-conversion/#comments</comments>
		<pubDate>Tue, 28 Apr 2009 14:15:25 +0000</pubDate>
		<dc:creator>jamiei</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Delphi]]></category>
		<category><![CDATA[Delphi Prism]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Podcast at Delphi]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[delphi prism]]></category>
		<category><![CDATA[project conversion]]></category>

		<guid isPermaLink="false">http://jamiei.com/blog/?p=271</guid>
		<description><![CDATA[I have in the past written up a few tips for converting a Delphi.NET project to Delphi Prism but this time I thought it might be useful for me to write up my experiences on converting this C# Project to Delphi Prism by initially using the C#ToPas Tool from RemObjects. Many of you who attended CodeRage [...]]]></description>
			<content:encoded><![CDATA[<p>I have in the past written up a few tips for <a href="http://jamiei.com/blog/2008/12/migrating-a-project-to-delphi-prism-from-delphinet/">converting a Delphi.NET project to Delphi Prism</a> but this time I thought it might be useful for me to write up my experiences on converting this C# Project to Delphi Prism by initially using the C#ToPas Tool from RemObjects. Many of you who attended <a href="http://conferences.codegear.com/coderage08">CodeRage III</a> (in a virtual manner) might remember <a href="http://www.delphi.org">Jim McKeeth</a>&#8216;s <a href="http://www.delphi.org/robot-rage/">Revenge of Delphi Robot rage</a> session. The aim of the session was to build a <a href="http://www.unrealtournament3.com/">Unreal Tournament 3</a> Bot to fight it out against other competitor&#8217;s bots in an Unreal Tournament 3 arena using the (then) newly recently <a href="http://www.codegear.com/products/delphi/prism">Delphi Prism</a> tool. For those who didn&#8217;t attend, I&#8217;ll spoil the ending and just tell you that somehow <a href="http://www.delphi.org/2008/12/18-coderage-iii-wrap-up/">my bot won</a>. <img src='http://jamiei.com/blog/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
<p>Jim is planning another session for the <a href="http://www.delphilive.com/">DelphiLive</a> conference (which I really wish I could go to, check it out if you haven&#8217;t already). However when running the last tournament we encountered a number of fatal bugs in the .NET UT3 Bot library which was originally put together by <a href="http://www.utbots.com/">Andy Sterland &amp; James Lissiak</a>. As a result Jim and I had been discussing the idea of porting the original library to Delphi Prism so that we could fix a few of the more serious bugs, it would also allow potential Prism Developers who were not familiar with C# to dig into the library itself. This is a project conversion diary which will outline the approaches I tried, any hurdles I encountered and a few tips for trying to avoid them when converting your own project.</p>
<p>I started by running the <a href="http://code.remobjects.com/p/csharptoxy/">C#ToOxygene</a> Tool on all of the source files within the project, It&#8217;s a very simple and easy to use tool that as its name implies aims to convert C# source files to Delphi Prism files. I was able to run the tool without issue on every C# source file except for 3 where for some reason it exited producing the error:  &#8221;<em><strong>Error during processing: Index was out of range</strong></em>&#8221; (Issue logged (rather clumsily) as #<a href="http://code.remobjects.com/p/csharptoxy/issues/37/">37</a>).</p>
<p>Having started on the basics of the translation using the conversion tool to do the grunt work I then dived into the code with the <a href="http://prismwiki.codegear.com/">Prism Wiki</a> and <a href="http://msdn.microsoft.com/en-us/library/618ayhy6(VS.80).aspx">MSDN C# documentation</a> open to make a start on correcting the <strong>600-odd errors</strong> that the resulting project produced upon an attempted Build.</p>
<p>At first glance I felt that the resultant code was a little bit visually &#8220;messy&#8221; in places and I felt it might be easier to prettify the code using a Code Formatter such as the <a href="http://jedicodeformat.sourceforge.net/">JEDI Code Format</a> tool to clean up the auto generated code and highlight any mismatched parenthesis or similar errors. However unfortunately the JEDI tool is not compatible with Prism and therefore refused to help me. If anyone knows of a Code Formatting tool which is compatible with Delphi Prism then please let me know in the comments below.</p>
<p>A few other issues which I encountered with the generated code or more generally are outlined below:</p>
<h3>Internal Keyword</h3>
<p><span style="font-weight: normal; font-size: 13px;">The C# <strong>internal</strong> (<a href="http://msdn.microsoft.com/en-us/library/7c5ka91b(VS.80).aspx">msdn doc</a>) keyword was not correctly translated &#8211; for this I took a trip to the Prism Wiki and looked up the <a href="http://prismwiki.codegear.com/en/Class_Member_Visibility_Levels">Class Member Visibility Levels</a> page to find that the equivalent level <strong>assembly</strong> is listed there (Issue #<a href="http://code.remobjects.com/p/csharptoxy/issues/42/">42</a>). Note that in Delphi Prism you can also have </span><span style="font-weight: normal; font-size: 13px;"><strong>assembly or protected</strong></span><span style="font-weight: normal; font-size: 13px;"> or </span><span style="font-weight: normal; font-size: 13px;"><strong>assembly and protected</strong></span><span style="font-weight: normal; font-size: 13px;"> depending upon whether you wish the class to be visible to descendants.</span></p>
<h3>The C# &#8220;this&#8221; Keyword</h3>
<p>The C# <strong>this.</strong> (<a href="http://msdn.microsoft.com/en-us/library/dk1507sz(VS.80).aspx">msdn doc</a>) keyword was not correctly translated to the Delphi Prism <strong>Self.</strong> equivalent that we&#8217;re all accustomed to. This isn&#8217;t hard to spot when going through the generated code and the issue had already been logged by Anton Kasyanov (Issue #<a href="http://code.remobjects.com/p/csharptoxy/issues/39/">39</a>) and should be fixed shortly.</p>
<h3>Float Types</h3>
<p>One class made extensive use of the <strong>float</strong> type (<a href="http://msdn.microsoft.com/en-us/library/b1e65aza(VS.80).aspx">msdn doc</a>) which doesn&#8217;t exist as is a built-in type for Delphi Prism so you&#8217;ll need to convert it to one of the <a href="http://prismwiki.codegear.com/en/Floats">two Float types</a> depending upon the precision you require.</p>
<h3>Operator Overloading</h3>
<p>Operator Overloading is not something that Delphi developers may be traditionally used to but Delphi Prism supports this nicely. However instead of using the sign operator that C# Developers use you&#8217;ll need to lookup the operator name and declaration on the <a href="http://prismwiki.codegear.com/en/Operator_Overloading">Operator Overloading Wiki Page</a>. eg. The Equals operator is declared as so:</p>
<pre class="brush: delphi; title: ; notranslate">class operator Equal(obj1, obj2: UTIdentifier): boolean;</pre>
<h3>Differences between return, Result and exit</h3>
<p>This should be obvious to any C# Developer but it could be easy to forget about. Don&#8217;t forget that the <strong>return</strong> C# method (<a href="http://msdn.microsoft.com/en-us/library/1h3swy84(VS.80).aspx">msdn doc</a>) terminates execution of the method in which it is being executed and returns to the calling method and therefore be wary of using <strong>Result</strong> blindly in an identical manner, particularly with boolean statements that rely upon this fact. Eg. Consider the following C# Code:</p>
<pre class="brush: csharp; title: ; notranslate">public override bool Equals(object obj)
{
	if (obj is UTVector)
	{
		if (this._x == ((UTVector)obj)._x &amp;&amp;
			this._y == ((UTVector)obj)._y &amp;&amp;
			this._z == ((UTVector)obj)._z)
		{
			return true;
		}
	}
	return false;
}</pre>
<p>Clearly, if you simply replace the <strong>return false;</strong> with <strong>Result := False;</strong> you will end up with a method that always returns False because the expression will always be executed no matter what Result is set to earlier in the method. In this case I simply moved the <strong>Result := false;</strong> to the top of the method. Delphi Prism also supports the <a href="http://prismwiki.codegear.com/en/Exit_(keyword)">Exit keyword</a> which also allows you to Return a result parameter. This method is a lot closer to the C# return keyword and usage but the <a href="http://prismwiki.codegear.com/en/Exit_(keyword)">Exit page</a> on the Prism Wiki warns against using too many exit keywords to avoid making the code harder to maintain.</p>
<h3>Constructors are not named</h3>
<p>This is a rare example of where the original C# code actually looks much closer to the end Delphi Prism code than a traditional Delphi programmer might think. In C# you might have a class and constructor that might look like this:</p>
<pre class="brush: csharp; title: ; notranslate">public class UTIdentifier
{
	internal UTIdentifier(string Id)
	{
	}
}</pre>
<p>In Prism, we have nameless <a href="http://prismwiki.codegear.com/en/Constructors">constructors</a> which means that the equivalent Delphi Prism constructor implementation might look like this:</p>
<pre class="brush: delphi; title: ; notranslate">  constructor UTIdentifier(id: String);
  begin
  end;</pre>
<p>It is important to note that in your class interface should adhere to this rule too. eg <strong>constructor(id: String);</strong></p>
<p>This was something that the C#ToPas tool also generated incorrectly. (Issue #<a href="http://code.remobjects.com/p/csharptoxy/issues/44/">44</a>)</p>
<h3>Short-Circuit Boolean Expressions</h3>
<p>I came across the C# <a href="http://msdn.microsoft.com/en-us/library/6373h346(VS.80).aspx"><strong>||</strong></a> operator which performs a short-circuited OR boolean evaluation of the operands. I wasn&#8217;t sure that the Delphi Prism <strong>or</strong> operator produces the same behaviour but if I read the <a href="http://prismwiki.codegear.com/en/Expression_Evaluation#Boolean_Short-Circuit_Evaluation">Expression Evaluation</a> page correctly then I believe it does (In Delphi 2009, at least, this behaviour is set through the <a href="http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/compdirsbooleanshortcircuitevaluation_xml.html">{$B+/-}</a> compiler flag) .</p>
<h3>Event Assignment</h3>
<p>The Event Assignment routine for multicast delegate type events is slightly different in Delphi Prism from what we&#8217;re used to in Delphi Win32. Looking at the <a href="http://prismwiki.codegear.com/en/Event_Assignment_Operators">Event Assignment</a> page in the wiki we can see that the actual method for assignment is much closer to the C# equivalent and uses the same C# <strong>+=</strong> and <strong>-=</strong> operators. The C#ToPas tool converted this code a little too literally converted this C# code:</p>
<pre class="brush: csharp; title: ; notranslate">this._connection.OnDataReceived += new EventHandler(DataReceived);
this._connection.OnErrorOccurred += new EventHandler(ErrorOccurred);</pre>
<p>to this:</p>
<pre class="brush: delphi; title: ; notranslate">Self._connection.OnDataReceived := Self._connection.OnDataReceived + new EventHandler(DataReceived);
Self._connection.OnErrorOccurred := Self._connection.OnErrorOccurred + new EventHandler(ErrorOccurred)</pre>
<p>Which produces a compiler Error that Events in Delphi Prism don&#8217;t allow access to the underlying field. You should use the += operator instead <a href="http://prismwiki.codegear.com/en/Event_Assignment_Operators">as seen on the Wiki</a>. (C#ToPas Issue logged as Issue #<a href="http://code.remobjects.com/p/csharptoxy/issues/45/">45</a>).</p>
<p><em>UPDATE: I&#8217;ve just noticed (after logging my own issue) that the event assignment issue has already been reported and has been marked as Won&#8217;t Fix (Issue #<a href="http://code.remobjects.com/p/csharptoxy/issues/27/">27</a>) so it looks like you&#8217;ll need to check _every_ event assignment in your project manually.</em></p>
<h3>Switch and Case Statements</h3>
<p>The C#ToPas tool did not appear to react well at all to use of the C# <a href="http://msdn.microsoft.com/en-us/library/06tc147t.aspx">switch</a> and case statements. I can&#8217;t give you any specific advice on how to fix these because each case statement was generated slightly differently in my converted code and therefore I can only advise that you check these manually or rewrite each of these methods by hand.</p>
<h3>Reserved Keywords</h3>
<p>Sadly this isn&#8217;t an issue that is related to the C#ToPas tool but more of an intrinsic difficulty when converting interoperable code between languages. I encountered at least one class that made use of a few reserved words in Delphi such as begin and end &#8211; I&#8217;m still not sure what to do with these for now.</p>
<h3>Overall Impression</h3>
<p>Despite a few early bugs the <a href="http://code.remobjects.com/p/csharptoxy/">C#ToPas</a> Tool is a good platform to begin converting your C# code to Delphi Prism although you may wish to ensure that you have some extensive Unit Tests for your code before the main code is converted so as to ensure that any bugs introduced can be picked up on quickly (which you have anyway right?). It is also worth noting that the <a href="http://prismwiki.codegear.com">Delphi Prism Wiki</a> (which now <a href="http://blogs.remobjects.com/blogs/mh/2009/04/27/p277">authenticates via your CDN login</a>) has <strong>excellent coverage</strong> and is an absolutely <strong>invaluable reference</strong> for anyone working with Delphi Prism.</p>
<p>By the time this entry is published I suspect that <a href="http://blogs.remobjects.com/blogs/ck/">Carlo</a> will be very close to fixing some or most of the above bugs in the C#ToPas tool.</p>
<p>The code is nowhere near complete yet and most of the files are still straight out of the Automatic conversion and therefore <strong>will not compile in any way</strong> but I&#8217;ve put what we have so far up on the rather smooth <a href="http://www.github.com">Github</a> site at the <a href="http://github.com/jamiei/PrismUT3RemoteBot">PrismUT3RemoteBot</a> project which anyone can fork and push changes back to. <a href="http://git-scm.com/">Git</a> is a little strange to work with at first because it&#8217;s a different mindset to SubVersion/CVS but it&#8217;s worth checking out if you haven&#8217;t already.</p>
<p>[<strong>UPDATE</strong>: The Prism UT3 Remote Bot conversion project has been completed since this original post and may be used during Jim McKeeth's session at <a href="http://www.delphilive.com/">DelphiLive! Germany Sept 28 - Oct 2 2009</a>.]</p>
]]></content:encoded>
			<wfw:commentRss>http://jamiei.com/blog/2009/04/diary-of-a-c-to-delphi-prism-conversion/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

