tag:blogger.com,1999:blog-87301952024-03-16T11:52:33.007-07:00Coding Power UnleashedProgramming blogs containing .NET, C#, LINQ, Objective-C and Delphi experiencesRoland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.comBlogger242125tag:blogger.com,1999:blog-8730195.post-69129065262068777062013-02-03T12:06:00.001-08:002013-02-03T12:10:46.528-08:00Use an image as your UIBarButtonItemUsing an image as your UIBarButtonItem in your navigationcontroller bar can only be achieved by using a common UIButton as the BarButtonItem's custom view.<br />
<div>
<br /></div>
<div>
This is an example on how you can achieve that. It takes a target, selector and image name and returns a UIBarButtonItem as an image.</div>
<div>
<br /></div>
<div>
<div style="font-family: Menlo; font-size: 11px;">
+(<span style="color: #7131a9;">UIBarButtonItem</span>*) getBarButton:(<span style="color: #b411a3;">id</span>)target withSelector:(<span style="color: #b411a3;">SEL</span>)selector andImage:(<span style="color: #7131a9;">NSString</span>*)imageName</div>
<div style="font-family: Menlo; font-size: 11px;">
{</div>
<div style="color: #1c8700; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>//Create image</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #7131a9;">UIImage</span> *image = [<span style="color: #7131a9;">UIImage</span> <span style="color: #410a81;">imageNamed</span>:imageName];</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="color: #1c8700; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>//Create UIButton with the imag</div>
<div style="color: #410a81; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span><span style="color: #7131a9;">UIButton</span><span style="color: black;"> *button = [</span><span style="color: #7131a9;">UIButton</span><span style="color: black;"> </span>buttonWithType<span style="color: black;">:</span>UIButtonTypeCustom<span style="color: black;">];</span></div>
<div style="font-family: Menlo; font-size: 11px;">
button.<span style="color: #7131a9;">bounds</span> = <span style="color: #410a81;">CGRectMake</span>(<span style="color: #3f00d8;">0</span>, <span style="color: #3f00d8;">0</span>, image.<span style="color: #7131a9;">size</span>.<span style="color: #7131a9;">width</span>, image.<span style="color: #7131a9;">size</span>.<span style="color: #7131a9;">height</span>);</div>
<div style="color: #410a81; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> [button </span>setImage<span style="color: black;">:image </span>forState<span style="color: black;">:</span>UIControlStateNormal<span style="color: black;">];</span></div>
<div style="color: #410a81; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> [button </span>addTarget<span style="color: black;">:target </span>action<span style="color: black;">:selector </span>forControlEvents<span style="color: black;">:</span>UIControlEventTouchUpInside<span style="color: black;">];</span></div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
<br /></div>
<div style="color: #1c8700; font-family: Menlo; font-size: 11px;">
<span style="color: black;"> </span>//Create UIBarButtonItem with customview button</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #7131a9;">UIBarButtonItem</span> *barButton = [[<span style="color: #7131a9;">UIBarButtonItem</span> <span style="color: #410a81;">alloc</span>] <span style="color: #410a81;">initWithCustomView</span>:button];</div>
<div style="font-family: Menlo; font-size: 11px; min-height: 13px;">
</div>
<div style="font-family: Menlo; font-size: 11px;">
<span style="color: #b411a3;">return</span> barButton;</div>
<div style="font-family: Menlo; font-size: 11px;">
}</div>
</div>
Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com84tag:blogger.com,1999:blog-8730195.post-60517200993018770382012-02-06T13:09:00.000-08:002012-02-21T12:14:04.421-08:00Xcode snippet #3: Injecting JavaScript in UIWebViewHow to get, for example, all the hyperlinks from a webpage, which is loaded in your iPhone app using the UIWebView control?<br />(Xcode's webbrowser counterpart)<br />
<br />
Doing this in JavaScript is an easy task. All we have to do is to inject a JavaScript function into the loaded webpage which will do the work for us.<br />
<br />
Injecting JavaScript into an UIWebView control can be achieved using the <b>stringByEvaluatingJavaScriptFromString</b> method, which returns the result of a given script. In other words you can run scripts with this method.<br />
<br />
In the below example all links from a webpage are extracted and returned as string formatted as href|text|title.<br />
<br />
<pre class="csharpcode">
-(<span class="kwrd">void</span>)getHyperlinks
{
[webView stringByEvaluatingJavaScriptFromString:<span class="str">@"var script = document.createElement('script');"</span>
<span class="str">"script.type = 'text/javascript';"</span>
<span class="str">"script.text = \"function getHyperlinks() { "</span>
<span class="str">"var links = document.getElementsByTagName('a');"</span>
<span class="str">"var allLinks = '';"</span>
<span class="str">"for (var i = 0; i<links.length; i++) {"</span>
<span class="str">"var link = links[i].href + '|' + links[i].text + '|' + links[i].title + ';';"</span>
<span class="str">"allLinks += link;"</span>
<span class="str">"} return allLinks;"</span>
<span class="str">"}\";"</span>
<span class="str">"document.getElementsByTagName('head')[0].appendChild(script);"</span>];
NSString *html = [webView stringByEvaluatingJavaScriptFromString:
<span class="str">@"getHyperlinks();"</span>];
textView.text = html;
}</pre>
In the first step the JavaScript function getHyperlinks() is injected in the HTML document. At last the function is executed and its result is returned into a textview.<br /><br />
That's all!Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com75tag:blogger.com,1999:blog-8730195.post-38393846536957869202011-09-18T12:35:00.000-07:002011-09-18T23:50:31.961-07:00Xcode snippet #2: Archiving objects with NSKeyedArchiverArchiving objects with <b>NSKeyedArchiver</b> is the process of saving your objects to a binary file. This process is called encoding. In this step-by-step tutorial we will persist a settings object to a file, and bring it back to life using <b>NSKeyedUnArchiver</b>. Archiving objects is very convenient way for saving small amounts of data like settings in your iPhone app.<br /><br />
<b>Step 1: Comply with the NSCoding protocol</b><br />
The first thing we have to do is to let our object comply with the NSCoding protocol. This protocol adds encoding and decoding methods to our object. Complying is easily done by adding <NSCoding> to the object like this:<br />
<pre>
@<font color="#2040a0">interface</font> <font color="#2040a0">Settings</font> <font color="4444FF">:</font> <font color="#2040a0">NSObject</font><font color="4444FF"><</font><font color="#2040a0">NSCoding</font><font color="4444FF">></font> <font color="4444FF"><strong>{</strong></font>
<font color="4444FF"><strong>}</strong></font>
</pre>
<b>Step 2: Implement the encodeWithCode and initWithCoder methods</b><br />
We have to tell the NSKeyedArchiver object how our object should be encoded. Simply said tell it what our properties are and what types they are etc.<br /><br />
This is done the <b>encodeWithCoder</b> method (as implemented by the NSCoding protocol), here under you see an example for an object, boolean and int property.<br />
<pre>
<font color="4444FF">-</font> <font color="4444FF">(</font><strong>void</strong><font color="4444FF">)</font><font color="#2040a0">encodeWithCoder</font><font color="4444FF">:</font><font color="4444FF">(</font><font color="#2040a0">NSCoder</font> <font color="4444FF">*</font><font color="4444FF">)</font><font color="#2040a0">coder</font> <font color="4444FF"><strong>{</strong></font>
<font color="4444FF">[</font><font color="#2040a0">coder</font> <font color="#2040a0">encodeObject</font><font color="4444FF">:</font><font color="#2040a0">self</font>.<font color="#2040a0">name</font> <font color="#2040a0">forKey</font><font color="4444FF">:</font>@<font color="#008000">"name"</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="4444FF">[</font><font color="#2040a0">coder</font> <font color="#2040a0">encodeBool</font><font color="4444FF">:</font><font color="#2040a0">self</font>.<font color="#2040a0">newsLetter</font> <font color="#2040a0">forKey</font><font color="4444FF">:</font>@<font color="#008000">"newsLetter"</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="4444FF">[</font><font color="#2040a0">coder</font> <font color="#2040a0">encodeInt32</font><font color="4444FF">:</font><font color="#2040a0">self</font>.<font color="#2040a0">count</font> <font color="#2040a0">forKey</font><font color="4444FF">:</font>@<font color="#008000">"count"</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font>
</pre>
<br />
To unarchive our object we have to implement the initWithCoder method: (Make sure that the keys in both the encodeWithCoder and initWithCoder are the same for each property)<br /><br />
<pre>
<font color="4444FF">-</font><font color="4444FF">(</font><font color="#2040a0">id</font><font color="4444FF">)</font><font color="#2040a0">initWithCoder</font><font color="4444FF">:</font><font color="4444FF">(</font><font color="#2040a0">NSCoder</font> <font color="4444FF">*</font><font color="4444FF">)</font><font color="#2040a0">coder</font> <font color="4444FF"><strong>{</strong></font>
<strong>if</strong> <font color="4444FF">(</font><font color="4444FF">(</font><font color="#2040a0">self</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">super</font> <font color="#2040a0">init</font><font color="4444FF">]</font><font color="4444FF">)</font><font color="4444FF">)</font>
<font color="4444FF"><strong>{</strong></font>
<font color="#2040a0">self</font>.<font color="#2040a0">name</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="4444FF">[</font><font color="#2040a0">coder</font> <font color="#2040a0">decodeObjectForKey</font><font color="4444FF">:</font>@<font color="#008000">"name"</font><font color="4444FF">]</font> <font color="#2040a0">retain</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="#2040a0">self</font>.<font color="#2040a0">newsLetter</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">coder</font> <font color="#2040a0">decodeBoolForKey</font><font color="4444FF">:</font>@<font color="#008000">"newsLetter"</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="#2040a0">self</font>.<font color="#2040a0">count</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">coder</font> <font color="#2040a0">decodeInt32ForKey</font><font color="4444FF">:</font>@<font color="#008000">"count"</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font>
<strong>return</strong> <font color="#2040a0">self</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font>
</pre>
That is pretty much all we have to do before we can archive and unarchive.
<br /><br />
<b>Step 3: Implement the archive and unarchive methods</b><br />
What is left is to make two methods for un- and archiving. The saveSetting methods has two parameters, namely settings, the object to archive and the name for the file we want to save it to. With help of the getFullFilePath method we get a full file path to the Document directory of our device. To bring our object back to live use the loadSettings method.
<br />
<pre>
<font color="4444FF">-</font><font color="4444FF">(</font><font color="#2040a0">NSString</font><font color="4444FF">*</font><font color="4444FF">)</font> <font color="#2040a0">getFullFilePath</font><font color="4444FF">:</font><font color="4444FF">(</font><font color="#2040a0">NSString</font><font color="4444FF">*</font><font color="4444FF">)</font><font color="#2040a0">name</font>
<font color="4444FF"><strong>{</strong></font>
<font color="#2040a0">NSArray</font> <font color="4444FF">*</font><font color="#2040a0">paths</font> <font color="4444FF">=</font> <font color="#2040a0">NSSearchPathForDirectoriesInDomains</font>
<font color="4444FF">(</font><font color="#2040a0">NSDocumentDirectory</font>, <font color="#2040a0">NSUserDomainMask</font>, <font color="#2040a0">YES</font><font color="4444FF">)</font><font color="4444FF">;</font>
<font color="#2040a0">NSString</font> <font color="4444FF">*</font><font color="#2040a0">savePath</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">paths</font> <font color="#2040a0">objectAtIndex</font><font color="4444FF">:</font><font color="#FF0000">0</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="#2040a0">NSString</font> <font color="4444FF">*</font><font color="#2040a0">theFileName</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">NSString</font> <font color="#2040a0">stringWithFormat</font><font color="4444FF">:</font>@<font color="#008000">"%@.setting"</font>, <font color="#2040a0">name</font><font color="4444FF">]</font><font color="4444FF">;</font>
<strong>return</strong> <font color="4444FF">[</font><font color="#2040a0">savePath</font> <font color="#2040a0">stringByAppendingPathComponent</font><font color="4444FF">:</font> <font color="#2040a0">theFileName</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font>
<font color="4444FF">-</font><font color="4444FF">(</font><strong>void</strong><font color="4444FF">)</font><font color="#2040a0">saveSetting</font><font color="4444FF">:</font><font color="4444FF">(</font><font color="#2040a0">Settings</font> <font color="4444FF">*</font><font color="4444FF">)</font><font color="#2040a0">settings</font> <font color="#2040a0">name</font><font color="4444FF">:</font> <font color="4444FF">(</font><font color="#2040a0">NSString</font> <font color="4444FF">*</font><font color="4444FF">)</font><font color="#2040a0">name</font>
<font color="4444FF"><strong>{</strong></font>
<font color="#2040a0">NSString</font> <font color="4444FF">*</font><font color="#2040a0">filePath</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">self</font> <font color="#2040a0">getFullFilePath</font><font color="4444FF">:</font><font color="#2040a0">name</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="#2040a0">NSData</font> <font color="4444FF">*</font><font color="#2040a0">theData</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">NSKeyedArchiver</font> <font color="#2040a0">archivedDataWithRootObject</font><font color="4444FF">:</font><font color="#2040a0">settings</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="4444FF">[</font><font color="#2040a0">NSKeyedArchiver</font> <font color="#2040a0">archiveRootObject</font><font color="4444FF">:</font><font color="#2040a0">theData</font> <font color="#2040a0">toFile</font><font color="4444FF">:</font><font color="#2040a0">filePath</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font>
<font color="4444FF">-</font><font color="4444FF">(</font><font color="#2040a0">Settings</font><font color="4444FF">*</font><font color="4444FF">)</font><font color="#2040a0">loadSetting</font><font color="4444FF">:</font><font color="4444FF">(</font><font color="#2040a0">NSString</font> <font color="4444FF">*</font><font color="4444FF">)</font><font color="#2040a0">name</font>
<font color="4444FF"><strong>{</strong></font>
<font color="#2040a0">NSString</font> <font color="4444FF">*</font><font color="#2040a0">filePath</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">self</font> <font color="#2040a0">getFullFilePath</font><font color="4444FF">:</font><font color="#2040a0">name</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="#2040a0">NSData</font> <font color="4444FF">*</font><font color="#2040a0">theData</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">NSKeyedUnarchiver</font> <font color="#2040a0">unarchiveObjectWithFile</font><font color="4444FF">:</font><font color="#2040a0">filePath</font><font color="4444FF">]</font><font color="4444FF">;</font>
<font color="#2040a0">Settings</font> <font color="4444FF">*</font><font color="#2040a0">settings</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">NSKeyedUnarchiver</font> <font color="#2040a0">unarchiveObjectWithData</font><font color="4444FF">:</font><font color="#2040a0">theData</font><font color="4444FF">]</font><font color="4444FF">;</font>
<strong>return</strong> <font color="#2040a0">settings</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font>
</pre>
<b>Saving arrays of objects</b><br />
It is also very easy to save lists of objects to file. Suppose we have a list with Settings and want to store them in one file. You can achieve this by archiving a NSMutableArray. For example:<br />
<pre>
// <font color="#2040a0">Archiving</font>
<font color="#2040a0">NSMutableArray</font> <font color="4444FF">*</font><font color="#2040a0">array</font> <font color="4444FF">=</font> <font color="#2040a0">self</font>.<font color="#2040a0">self</font>.<font color="#2040a0">settingsList</font><font color="4444FF">;</font>
<font color="4444FF">[</font><font color="#2040a0">NSKeyedArchiver</font> <font color="#2040a0">archiveRootObject</font><font color="4444FF">:</font><font color="#2040a0">array</font> <font color="#2040a0">toFile</font><font color="4444FF">:</font>
<font color="4444FF">[</font><font color="#2040a0">savePath</font> <font color="#2040a0">stringByAppendingPathComponent</font><font color="4444FF">:</font> <font color="#2040a0">theFileName</font><font color="4444FF">]</font><font color="4444FF">]</font><font color="4444FF">;</font>
//<font color="#2040a0">UnArchiving</font>
<font color="#2040a0">NSMutableArray</font> <font color="4444FF">*</font><font color="#2040a0">array</font> <font color="4444FF">=</font> <font color="4444FF">[</font><font color="#2040a0">NSKeyedUnarchiver</font> <font color="#2040a0">unarchiveObjectWithFile</font><font color="4444FF">:</font>
<font color="4444FF">[</font><font color="#2040a0">savePath</font> <font color="#2040a0">stringByAppendingPathComponent</font><font color="4444FF">:</font> <font color="#2040a0">theFileName</font><font color="4444FF">]</font><font color="4444FF">]</font><font color="4444FF">;</font>
<strong>if</strong> <font color="4444FF">(</font><font color="4444FF">[</font><font color="#2040a0">array</font> <font color="#2040a0">count</font><font color="4444FF">]</font> <font color="4444FF">></font> <font color="#FF0000">0</font><font color="4444FF">)</font> <font color="4444FF"><strong>{</strong></font>
<font color="#2040a0">self</font>.<font color="#2040a0">settingsList</font> <font color="4444FF">=</font> <font color="#2040a0">array</font><font color="4444FF">;</font>
<font color="4444FF"><strong>}</strong></font>
</pre><br />
Conclusion, the NSKeyedArchiver and NSKeyedUnarchiver are pretty easy and straight forwarded to use for persisting objects. It is very handy for saving small objects likes settings etc.<br />
More information:<br />
<a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSKeyedArchiver_Class/Reference/Reference.html">NSKeyedArchiver Class Reference</a><br />
<a href="http://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Protocols/NSCoding_Protocol/Reference/Reference.html">NSCoding protocol reference</a>
Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com7tag:blogger.com,1999:blog-8730195.post-36511628437328059372011-04-04T12:27:00.000-07:002011-04-04T12:44:52.610-07:00Xcode snippet #1: Reading the contents of a directoryBelow you will find a code snippet which reads the contents of a specific directory (in this case the documents directory) from your device. It looks for file with has a suffix .list, stripes the filename from it with the substringToIndex method and adds that name to another array.<br />
<br />
<pre>NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
for (NSString *dir in dirContents) {
if ([dir hasSuffix:@".list"]) {
NSRange range = [dir rangeOfString:@"."];
NSString *name = [dir substringToIndex:range.location];
if (![name isEqualToString:@""]) {
MyListItem *listItem = [[MyListItem alloc] init];
listItem.name = name;
[lists addObject:listItem];
[listItem release];
}
}
}
</pre>Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com2tag:blogger.com,1999:blog-8730195.post-75488373892492006992010-10-27T07:41:00.000-07:002010-10-27T07:41:38.550-07:00Developing for the iPadThe past months we have been busy with a new challenge: Developing for the iPad.<br />
I can tell you that developing for the iPad is a total different experience compared to Delphi and Visual Studio. Never the less it is great fun! Besides the fact that it is a fun platform to work on it is even more fun to learn a new platform, language and IDE. <br />
<br />
<b>The tools: XCode and Interface builder</b><br />
<a href="http://www.beensoft.nl">We</a> decided to go the hard way and develop with <a href="http://developer.apple.com/technologies/tools/xcode.html">Apples native IDE XCode</a>. As .NET developer an easier approach would be to choose for a Mono solution like <a href="http://monotouch.net/">MonoTouch</a>, however we felt that we had to know 'the metal' first. <br />
The main tools you use developing for iPad are XCode and Interface builder. XCode is a good to work with IDE, however compared to Visual Studio it lacks some features. (Must say I still find new features, even today). One annoying point of XCode is that it opens every file in a new window (when double clicking on a file) so after a while it can be hard to seperate the trees from the forest with all that open files. However if you click once you can keep them in one window but for some reason that is hard to persist. <br />
A big plus for XCode, in my opinion, is that it helps you to work in the <b>MVC design pattern</b> way which gives you clean code.<br />
XCode does not offer an integrated form designer like VS does instead you make your views (forms) with <a href="http://en.wikipedia.org/wiki/Interface_Builder">Interface builder</a>. <br />
<br />
<b>The language Objective-C</b><br />
The language, <a href="http://nl.wikipedia.org/wiki/Objective-C">Objective-C</a>, looks at first very weird with all that brackets but there are great resources online that helped us to get started.<br />
<br />
If you would like to start iPad development take a look at some resources we gathered along the way:<br />
<a href="http://developer.apple.com/">Apples developer website</a> (Get yourself a free account)<br />
<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewiTunesUCollection?id=384233225">Stanford University lectures on iTunes U</a> (Highly recommended!!!!)<br />
<a href="http://cocoadevcentral.com/d/learn_objectivec/">Cocoa Dev Central - Learn Objective-C</a><br />
<br />
It is likely that I will write some more blogposts about this new adventure in the future.Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com1tag:blogger.com,1999:blog-8730195.post-85188172052318956882010-08-17T01:50:00.001-07:002010-08-18T05:39:45.865-07:00Fooling around with TWebbrowser compilation post<p>In the statistics of my blog I find a lot of people searching for Delphi’s TWebbrowser solutions and examples. I wrote a few post about some TWebbrowser specific problems and possible solutions. <br />
Because they are in seperated, non related, posts I thought it would be nice to compilate all the knowledge in one blogpost. Well here it is. :) <br />
<br />
<a href="http://beensoft.blogspot.com/2006/01/fooling-around-with-twebbrowser.html">Fooling around with TWebbrowser #1</a> <br />
Describes how to use TWebbrowser for editing HTML</p><p><a href="http://beensoft.blogspot.com/2007/09/fooling-around-with-twebbrowser-2.html">Fooling around with TWebbrowser #2</a> <br />
Describes how to retain the browser session within you application</p><a href="http://beensoft.blogspot.com/2007/09/fooling-around-with-twebbrowser-3.html">Fooling around with TWebbrowser #3</a> <br />
Describes some TWebbrowser basics like how to get the right popwindowsize, how to fill up a combobox with visited websites, how to enable/disable navigate buttons etcetera. <p><a href="http://beensoft.blogspot.com/2007/10/fooling-around-with-twebbrowser-4.html">Fooling around with TWebbrowser #4</a> <br />
Describes two solutions for the Enter key problem. <br />
<br />
<a href="http://beensoft.blogspot.com/2006/02/posting-data-with-twebbrower.html">Posting data with TWebbrowser</a> <br />
Describes how to post data to websites using TWebbrowser</p><p><a href="http://beensoft.blogspot.com/2006/05/saving-twebbrowser-content-with.html">Saving TWebbrowser content with IPersistFile</a> <br />
Describes two ways to save the content of the current loaded document in the browser.</p><p><a href="http://beensoft.blogspot.com/2010/03/two-way-interaction-with-javascript-in.html">Two way interaction with Javascript in Winforms using Webbrowser</a> <br />
Although about Winforms C# this should be compatible with Delphi TWebbrowser.</p><p>Enjoy! </p>Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com3tag:blogger.com,1999:blog-8730195.post-30926424841143929862010-05-19T12:55:00.001-07:002010-05-20T00:26:58.669-07:00Using Extension methods on Lists to make it fluent<p>Since C# 3.0 you can write extension methods on any class that you want, even if you don’t have the source code of the class. In other words you can extend a class with your own methods.</p> <p><strong>Extension methods can only be declared in static classes as static methods.</strong></p> <p>Extensions on List<T> classes can be very handy and make your code more readable and fluent. <br />Suppose you have this mixed Animal list with Dogs and Cats from <a href="http://beensoft.blogspot.com/2009/11/casting-lists-using-linq-2.html" target="_blank">this blogpost</a>.</p> <pre class="code"><span style="color: #2b91af">List</span><<span style="color: #2b91af">Animal</span>> animalList = <span style="color: blue">new </span><span style="color: #2b91af">List</span><<span style="color: #2b91af">Animal</span>>(); <br />animalList.Add(<span style="color: blue">new </span><span style="color: #2b91af">Dog</span>(<span style="color: #a31515">"Dog1"</span>, <span style="color: #2b91af">ConsoleColor</span>.Red)); <br />animalList.Add(<span style="color: blue">new </span><span style="color: #2b91af">Dog</span>(<span style="color: #a31515">"Dog2"</span>, <span style="color: #2b91af">ConsoleColor</span>.Green)); <br />animalList.Add(<span style="color: blue">new </span><span style="color: #2b91af">Dog</span>(<span style="color: #a31515">"Dog3"</span>, <span style="color: #2b91af">ConsoleColor</span>.Red)); <br />animalList.Add(<span style="color: blue">new </span><span style="color: #2b91af">Cat</span>(<span style="color: #a31515">"Cat1"</span>, <span style="color: #2b91af">ConsoleColor</span>.Black)); <br />animalList.Add(<span style="color: blue">new </span><span style="color: #2b91af">Cat</span>(<span style="color: #a31515">"Cat2"</span>, <span style="color: #2b91af">ConsoleColor</span>.Black)); <br />animalList.Add(<span style="color: blue">new </span><span style="color: #2b91af">Cat</span>(<span style="color: #a31515">"Cat3"</span>, <span style="color: #2b91af">ConsoleColor</span>.Black));</pre><br />(Never mind the ConsoleColor for the animal ;-) )<br /><br /><p>Suppose we want all the red dogs from the animalList, we could write our code like:<br /><span style="color: green"><br /> <br />//Get the Red docs<br /> <br /></span><span style="color: #2b91af">List</span><<span style="color: #2b91af">Dog</span>> redDogList = animalList.OfType<<span style="color: #2b91af">Dog</span>>().Where(n => n.Color == <span style="color: #2b91af">ConsoleColor</span>.Red).ToList();<br /><br />This works, however everytime we want to get some colored animal we would get the same type of code. We would probably write some static methods to avoid this :</p><br /><p> <span style="color: #2b91af">List</span><<span style="color: #2b91af">Dog</span>> redDogList = ListUtils.GetTheDogs(animalList);</p><br /><p>Even better is to make this <strong>static extension methods</strong> so that the functionality looks to be encapsulated by the list its self. We can then also make the method names more readable, for instant not “GetTheDog”, but “WhichAreDog”.</p><br /><p>For example:<br /> <span style="color: blue">public static </span><span style="color: #2b91af">List</span><<span style="color: #2b91af">Animal</span>> WhichAreDog(<span style="color: blue">this </span><span style="color: #2b91af">List</span><<span style="color: #2b91af">Animal</span>> animalList)<br /> {<br />   <span style="color: blue">return</span> animalList.OfType<<span style="color: #2b91af">Dog</span>>().Cast<<span style="color: #2b91af">Animal</span>>().ToList();<br /> }<br /> <br /><span style="color: blue"><br /> <br />public static </span><span style="color: #2b91af">List</span><<span style="color: #2b91af">Animal</span>> HavingColor(<span style="color: blue">this </span><span style="color: #2b91af">List</span><<span style="color: #2b91af">Animal</span>> animalList, <span style="color: #2b91af">ConsoleColor </span>color)<br /> {<br />   <span style="color: blue">return</span> animalList.Where(n => n.Color == color).ToList();<br /> }<br /> <br />The “<span style="color: blue">this</span>” before the first parameter of the method indicates that this is an extension method on this parameter, in this case the list with animals.</p><br /><p>Now getting the red dogs looks like this:</p><br /><pre class="code"><span style="color: #2b91af">List</span><<span style="color: #2b91af">Animal</span>> allRedDogs = <br />animalList.WhichAreDog().HavingColor(<span style="color: #2b91af">ConsoleColor</span>.Red);</pre><br /><a href="http://11011.net/software/vspaste"></a>Much more readable and fluent!Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com5tag:blogger.com,1999:blog-8730195.post-26724898819424585832010-05-03T12:31:00.001-07:002010-05-03T12:37:56.741-07:00Exploring LINQ #3<p>LINQ keeps amazing me in its power and beauty. So much that for each problem I have to solve (coding problem that is)  I find myself wondering can this be done with LINQ, and how.</p> <p>I am working on a rewrite of an old application to C#. For some reason the original designers had put some ‘model’ data into a none relational database. Instead of making a good relational data model the data has been put as follows (simplified record from the database) : <br /> <br />Option1 = “1;2;3” <br />Option2 = “A;B” <br />Value = 1</p> <p>In other words foreach possible combination of option1 and option2 there is a value. (1-A-1, 1-B-1, 2-A-1 etc.) I would prefer a relational set up, but to honor the past this will have to stay that way. For use in our program it  is required to have all possible combinations into a single flat list which we then can query with LINQ. <br />Of course it easy to construct some code that would knock this down into a flat list though I don’t think any will be as elegant and as fast to produce as this LINQified solution. <br /> <br />Suppose we read the raw data from the database into a list with raw objects looking like this:</p> <pre class="code"><span style="color: #2b91af">List</span><<span style="color: #2b91af">Raw</span>> rawList = <span style="color: blue">new </span><span style="color: #2b91af">List</span><<span style="color: #2b91af">Raw</span>>();<br />rawList.Add(<span style="color: blue">new </span><span style="color: #2b91af">Raw</span>() { Option1 = <span style="color: #a31515">"1;2;3"</span>, <br /> Option2 = <span style="color: #a31515">"A"</span>, Value = 1 });<br />rawList.Add(<span style="color: blue">new </span><span style="color: #2b91af">Raw</span>() { Option1 = <span style="color: #a31515">"4;5"</span>, <br /> Option2 = <span style="color: #a31515">"A;B"</span>, Value = 2 });<br />rawList.Add(<span style="color: blue">new </span><span style="color: #2b91af">Raw</span>() { Option1 = <span style="color: #a31515">"1;2;3"</span>, <br /> Option2 = <span style="color: #a31515">"C"</span>, Value = 3 });</pre><br /><p>Using LINQ it is easy to combine the original raw list with the “;” splitted list (arrays in fact) from the fields Option1 and Option2. From that a flat list can be constructed which holds all the combinations:</p><br /><pre class="code"><span style="color: #2b91af">List</span><<span style="color: #2b91af">Raw</span>> flatList = (<span style="color: blue">from </span>n <span style="color: blue">in </span>rawList<br /> <span style="color: blue">from </span>opt1 <span style="color: blue">in </span>n.Option1.Split(<span style="color: #a31515">';'</span>)<br /> <span style="color: blue">from </span>opt2 <span style="color: blue">in </span>n.Option2.Split(<span style="color: #a31515">';'</span>)<br /> <span style="color: blue">select new </span><span style="color: #2b91af">Raw<br /> </span>{<br /> Option1 = opt1,<br /> Option2 = opt2,<br /> Value = n.Value<br /> }).ToList();<br />flatList.ForEach((n) => <span style="color: #2b91af">Console</span>.WriteLine(n.Option1 + <br /> <span style="color: #a31515">"-" </span>+ n.Option2 + <br /> <span style="color: #a31515">"-" </span>+ n.Value.ToString()));</pre><br />Resulting in a ‘flat’ list:<br /><p><br />1-A-1<br />2-A-1<br />3-A-1<br />4-A-2<br />4-B-2<br />5-A-2<br />5-B-2<br />1-C-3<br />2-C-3<br />3-C-3<br /></p><br /><p>Querying this list is now child’s play:<br /><br /><span style="color: blue">int </span>Result = (<span style="color: blue">from </span>n <span style="color: blue">in </span>flatList<br />               <span style="color: blue">where </span>(n.Option1 == <span style="color: #a31515">"4"</span>) && (n.Option2 == <span style="color: #a31515">"B"</span>)<br />               <span style="color: blue">select </span>n.Value).FirstOrDefault();Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com0tag:blogger.com,1999:blog-8730195.post-76904036725663728762010-03-09T13:06:00.001-08:002010-03-10T00:13:51.994-08:00Two way interaction with JavaScript in Winforms using Webbrowser<p>In a recent project I had to integrate Google maps into a Winforms application. To use Google Maps from a desktop client you must be able to call the Javascript in webpage, which can be done with the Webbrowser component. <br /> <br /><strong>Calling JavaScript from C# <br /></strong>Calling JavaScript in a Webbrowser component is very easy. JavaScript is exposed through the <a href="http://msdn.microsoft.com/en-us/library/aa741505%28VS.85%29.aspx" target="_blank">IHTMLWindow2 interface</a> which can be referenced from the <a href="http://msdn.microsoft.com/en-us/library/aa752574%28VS.85%29.aspx" target="_blank">IHTMLDocument2 interface</a> through the parentWindow property. </p> <pre class="code">doc = webBrowser1.Document.<br /> DomDocument <span style="color: blue">as </span><span style="color: #2b91af">IHTMLDocument2</span>;<br />doc.parentWindow.execScript(<span style="color: #a31515">"createMapMarker<br /> </span>(<span style="color: #a31515">"52.3738007, 4.8909347"</span>, 1);<span style="color: #a31515">", "</span>JavaScript<span style="color: #a31515">");<br /></span></pre><br /><strong>Calling C# from JavaScript<br /></strong>Calling C# code from JavaScript can be done using the <strong>ObjectForScripting</strong> property of the Webbrowser component. The object can be called in JavaScript using window.external. The communication is established through COM interop so the class should be visible for COM.<br /><pre class="code"><span style="color: green">//Class example<br /></span>[ComVisible(<span style="color: blue">true</span>)]<br /><span style="color: blue">public class </span><span style="color: #2b91af">ExternalApplication<br /></span>{<br /> <span style="color: blue">public string </span>GetApplicationName()<br /> {<br /> <span style="color: blue">return </span><span style="color: #a31515">"The application"</span>;<br /> }<br />}<br /><span style="color: green">//Connect it to the webbrowser<br /></span>webbrowser1.ObjectForScripting <br /> = <span style="color: blue">new </span>ExternalApplication();</pre><br />From JavaScript call the external application, our C# app, like this:<br /> <br />(Google maps click event example)<br /><pre class="code"><span style="color: blue">function </span>createMapMarker(lat, lng, html) {<br /><span style="color: blue">var </span>point = <span style="color: blue">new </span>GLatLng(parseFloat(lat),parseFloat(lng));<br /><span style="color: blue">var </span>marker = <span style="color: blue">new </span>GMarker(point);<br />GEvent.addListener(marker, <span style="color: #a31515">"click"</span>, <span style="color: blue">function</span>() {<br /><span style="color: blue"> var </span>appname = window.external.GetApplicationName();<br /> alert(appname);<br /> });<br /> map.addOverlay(marker);<br /> <span style="color: blue">return </span>marker;<br />}</pre><br />For Delphi equivalent code take a look at Steve Trefethen blog serie about Google Maps / Delphi integration <a href="http://www.stevetrefethen.com/blog/UsingGoogleMapsFromAWindowsClientApplication.aspx">here</a>.Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com7tag:blogger.com,1999:blog-8730195.post-69597633187634588522010-01-20T12:20:00.001-08:002010-01-20T12:21:47.201-08:00Connecting (struggling) to Oracle database<p>This week I have been working (read struggling) on connecting a C# .NET application to Oracle. In the end I was trapped between <strong>32 bits vs 64 bits</strong>, .NET Oracle providers and 32 bit OLE DB providers.</p> <p>First of all I challenged this task, not bothered by any knowledge about Oracle. I did know that if you want to connect to an Oracle database you have to install the <strong>Oracle Client</strong> on the client machine. <a href="http://www.oracle.com/technology/software/index.html" target="_blank">This only 1,7 Gb (!) download</a> (Oracle 11g) happily told me that my Windows 7 version (real version 6.1, 64 bits) was not certified for the job. Only Windows 6.0 was. <br /> <br />Huh, now what? </p> <p>After some googling found <a href="http://fulopmihaly.wordpress.com/2009/09/17/how-to-install-oracle-client-11g-on-windows-7/" target="_blank">this blogpost</a> about how to trick the setup to install on Window 7 (6.1) anyway. <br />After that it installed gracefully (some warnings…), time to startup VS to give it a spin! <br /> <br />Well not. That app that I am writing connects besides <strong>Oracle</strong> and <strong>SQL Server</strong> to an <strong>Access database</strong>. Because the Jet OLEDB driver for Access is 32 bits only I am doomed to target the x86 platform for now. No problem, however the 64 bits Oracle client can not connect targeting the x86 platform. Getting a BadImageFormatException:</p> <p><em>[InvalidOperationException: Attempt to load Oracle client libraries threw BadImageFormatException. This problem will occur when running in 64 bit mode with the 32 bit Oracle client components installed.]</em> </p> <p>(Still having the feeling it is the other way around, because I have the 64 bit Oracle client components installed)</p> <p>Huh, now what?</p> <p>Because the ADO.NET provider for Oracle from Microsoft suits well in the other technologies I tried to google more information. <a href="http://blogs.msdn.com/adonet/archive/2009/06/15/system-data-oracleclient-update.aspx" target="_blank">As it seems this driver is by the coming of .NET 4.0 deprecated</a>. So on second thoughts not a good choice after all. Using the .NET provider from Oracle (ODP) then? Can I install 32 bits Oracle client on 64 bits machine? Who knows.</p> <p>Fortunately Twitter came to rescue someone pointed out that there are third party solutions, which can connect to an Oracle database <strong>even without the Oracle Client</strong>.</p> <p>So I tried <a href="http://www.devart.com/dotconnect/oracle/" target="_blank">Devarts dotConnect</a>, which indeed can connect without the Oracle Client, isn’t that just great! Within 5 minutes I was connected (either 32 or 64 bits). <br /> <br />So if you want to connect to Oracle on .NET, keep some sparetime afterwards, I would suggest using third party components for it!</p>Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com6tag:blogger.com,1999:blog-8730195.post-41703513452676036482009-11-06T00:13:00.001-08:002009-11-09T09:52:57.135-08:00Casting lists using LINQ #2<p>In <a href="http://beensoft.blogspot.com/2009/11/casting-lists-using-linq.html">my previous post</a> I showed the LINQ way to cast List<SomeDeriveType> to List<SomeBaseObject>.</p> <div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:e5aa0a4c-a01e-4b7d-b6bd-7c1f2f520f1d" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br />--><span style="color: #000000;">IEnumerable</span><span style="color: #000000;"><</span><span style="color: #000000;">BaseObject</span><span style="color: #000000;">></span><span style="color: #000000;"> baseObjects <br /> </span><span style="color: #000000;">=</span><span style="color: #000000;"> DerivedList.Cast</span><span style="color: #000000;"><</span><span style="color: #000000;">BaseObject</span><span style="color: #000000;">></span><span style="color: #000000;">;</span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div>Great stuff!<br /><br />As <a href="http://twitter.com/jamiei" target="_blank">@jamiei</a> <a href="http://beensoft.blogspot.com/2009/11/casting-lists-using-linq.html#comments">commented</a> this will raise an exception if the cast fails. <strong>OfType<T></strong> will return only the elements of type T despite the fact that you have different derived types in one list. So suppose you have an <strong>Animal </strong>class and a <strong>Cat</strong> and <strong>Dog</strong> class that derive from Animal you could do something like this:<br /><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:0965f3e9-8652-4cc6-9e0b-5d71bcae5d04" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">List</span><span style="color: #000000;"><</span><span style="color: #000000;">Animal</span><span style="color: #000000;">></span><span style="color: #000000;"> animalList </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;"><</span><span style="color: #000000;">Animal</span><span style="color: #000000;">></span><span style="color: #000000;">();<br />animalList.Add(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Dog(</span><span style="color: #800000;">"</span><span style="color: #800000;">Dog1</span><span style="color: #800000;">"</span><span style="color: #000000;">));<br />animalList.Add(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Dog(</span><span style="color: #800000;">"</span><span style="color: #800000;">Dog2</span><span style="color: #800000;">"</span><span style="color: #000000;">));<br />animalList.Add(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Dog(</span><span style="color: #800000;">"</span><span style="color: #800000;">Dog3</span><span style="color: #800000;">"</span><span style="color: #000000;">));<br />animalList.Add(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Cat(</span><span style="color: #800000;">"</span><span style="color: #800000;">Cat1</span><span style="color: #800000;">"</span><span style="color: #000000;">));<br />animalList.Add(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Cat(</span><span style="color: #800000;">"</span><span style="color: #800000;">Cat2</span><span style="color: #800000;">"</span><span style="color: #000000;">));<br />animalList.Add(</span><span style="color: #0000FF;">new</span><span style="color: #000000;"> Cat(</span><span style="color: #800000;">"</span><span style="color: #800000;">Cat3</span><span style="color: #800000;">"</span><span style="color: #000000;">));<br /></span><span style="color: #008000;">//</span><span style="color: #008000;">Get the dogs</span><span style="color: #008000;"><br /></span><span style="color: #000000;">IEnumerable</span><span style="color: #000000;"><</span><span style="color: #000000;">Dog</span><span style="color: #000000;">></span><span style="color: #000000;"> dogList </span><span style="color: #000000;">=</span><span style="color: #000000;"> animalList.OfType</span><span style="color: #000000;"><</span><span style="color: #000000;">Dog</span><span style="color: #000000;">></span><span style="color: #000000;">();<br /></span><span style="color: #008000;">//</span><span style="color: #008000;">Get the cats</span><span style="color: #008000;"><br /></span><span style="color: #000000;">IEnumerable</span><span style="color: #000000;"><</span><span style="color: #000000;">Cat</span><span style="color: #000000;">></span><span style="color: #000000;"> catList </span><span style="color: #000000;">=</span><span style="color: #000000;"> animalList.OfType</span><span style="color: #000000;"><</span><span style="color: #000000;">Cat</span><span style="color: #000000;">></span><span style="color: #000000;">();</span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div><br />LINQ makes it very easy to seperate the Dogs from the Cats!Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com3tag:blogger.com,1999:blog-8730195.post-594791355998454462009-11-04T00:05:00.001-08:002009-11-06T12:00:21.413-08:00Casting lists using LINQ #1I have been doing lately much work on a .NET 3.5 application using LINQ. LINQ is a great way to manipulate data. I still consider myself a 'LINQ rookie' so I discover everyday something new.</p> <p>One thing that floated around my head a while was the problem that you can't cast a List<SomeDerivedObject> to List<SomeBaseObject>. <br />A poorman's solution to this was to loop the objects from the one list into the other list. Not an elegant solution though.....</p> <p>I discovered that you can do this easily with LINQ. When you use a <strong>IEnumerable<T></strong> you can do this like this:</p> <p> <div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:3d2ed4fb-5987-4339-9565-2f69418449ae" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">IEnumerable</span><span style="color: #000000;"><</span><span style="color: #000000;">BaseObject</span><span style="color: #000000;">></span><span style="color: #000000;"> baseObjects <br /> </span><span style="color: #000000;">=</span><span style="color: #000000;"> DerivedList.Cast</span><span style="color: #000000;"><</span><span style="color: #000000;">BaseObject</span><span style="color: #000000;">></span><span style="color: #000000;">;</span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div><br />Where DerivedList holds objects that inherit from BaseObject.<br />With a <strong>List<T></strong> you could do something like this(copy):<br /> <div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:516d3fb7-54d8-459c-b337-7499bf3ac312" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">List</span><span style="color: #000000;"><</span><span style="color: #000000;">BaseObject</span><span style="color: #000000;">></span><span style="color: #000000;"> baseObjects </span><span style="color: #000000;">=</span><span style="color: #000000;"> <br /> </span><span style="color: #0000FF;">new</span><span style="color: #000000;"> List</span><span style="color: #000000;"><</span><span style="color: #000000;">BaseObject</span><span style="color: #000000;">></span><span style="color: #000000;">(DerivedList.Cast</span><span style="color: #000000;"><</span><span style="color: #000000;">BaseObject</span><span style="color: #000000;">></span><span style="color: #000000;">());</span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div><br />A great LINQ resource is <a href="http://msdn.microsoft.com/en-gb/vcsharp/aa336746.aspx" target="_blank">101 LINQ samples</a> on MSDN.Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com1tag:blogger.com,1999:blog-8730195.post-11067879918483858702009-08-05T00:43:00.001-07:002010-08-22T12:05:04.478-07:00Go see Delphi 2010<p>The first Delphi 2010 sneak peek video's showed up on the Internet. <a href="http://www.embarcadero.com/rad-studio-2010/" target="_blank">The first sneak peek</a> shows some new IDE features like <strong>IDE insight</strong>, an everywhere context sensitive IDE navigation system. <br />
<br />
Anders Ohlsson has a nice sum up of <a href="http://blogs.embarcadero.com/ao/2009/08/04/39017" target="_blank">all the related Delphi 2010 blogposts</a>.</p>Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com0tag:blogger.com,1999:blog-8730195.post-27587979119107475712009-06-30T00:30:00.001-07:002009-06-30T00:31:54.562-07:00CodeRush for free, not for Delphi Prism<p>As <a href="http://beensoft.blogspot.com/2009/04/coderush-when-code-smells.html" target="_blank">blogged before</a> you know that I think DevExpress CodeRush totally rocks! <br />Did you know that you can get a lot of this functionality for free? <br /> <br />Mark Miller has got <a href="http://community.devexpress.com/blogs/markmiller/archive/2009/06/25/coderush-xpress-for-c-and-visual-basic-2008.aspx" target="_blank">a very nice list</a> of features that are available in <a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/CodeRushX/index.xml" target="_blank">CodeRush Xpress</a> for<strong> C#</strong> and <strong>Visual Basic</strong> inside <strong>Visual Studio 2008</strong>. All for free! <br />In the full product you will get even more, check out the differences in this article: <a href="http://community.devexpress.com/blogs/markmiller/archive/2008/12/19/differences-between-coderush-xpress-and-coderush.aspx" target="_blank">Moving up from CodeRus Xpress to CodeRush</a></p> <p><strong>Delphi Prism</strong> <br />The downside of all this good stuff is that <a href="http://www.codegear.com/products/delphi/prism" target="_blank">Delphi Prism</a> is not supported. The fact that the original CodeRush was a pure Delphi IDE product, I can not believe that it will not be supported in the future. </p>Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com2tag:blogger.com,1999:blog-8730195.post-83263909075569279412009-06-29T00:39:00.001-07:002009-06-29T00:39:59.998-07:00The future of Delphi<p>I haven't had much time lately to follow everything on the Delphi side of the fence, but there is a lot going on. <br />Stefaan Lesage has a nice write up on this future, <a href="http://www.devia.be/news/article/the-future-of-delphi-looks-brighter-than-ever-before/" target="_blank">The future of Delphi looks brighter than ever before</a>. <br /> <br />A very nice read, sums up everything that is cooking in the labs, including the roadmap. </p> Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com0tag:blogger.com,1999:blog-8730195.post-88363248586153346922009-05-18T01:06:00.001-07:002009-05-18T01:33:21.726-07:00Applying Domain-Driven Design - Book review<p>Before this weekend I got my copy of <a href="http://www.amazon.com/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202" target="_blank">Applying Domain-Driven Design and Patterns</a> by Jimmy Nilsson. <br /><a href="http://www.amazon.com/Applying-Domain-Driven-Design-Patterns-Examples/dp/0321268202" target="_blank"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="148" alt="adddp" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhQoRjf1pK5Liv4xQnu0l_FWbIIsnjAMH4Tc-j7W5wNnEq2AxN7u6rPppMiPCTJavAuts89ZadVOb9Lhe_GUP7kWX9Jw4qQcDWHqvuha8UH_eGRI37EwRPKbUNFy3XdJ4OJKNetCw/?imgmax=800" width="115" border="0" /></a> <br />Althought the title, in my opinion should be "<strong>Applying Domain-Driven Design, Test Driven Design and Patterns"</strong>, because beside DDD it covers a great deal of TDD also. <br />Design patterns for me were always a bit fuzzy, something from far-far-away land. However lately I am having more and more interest in <a href="http://beensoft.blogspot.com/2009/04/exploring-test-driven-development.html" target="_blank">Test Driven Devleopment</a> and that software design method drives you towards better, testable, design, and <a href="http://en.wikipedia.org/wiki/Domain-driven_design" target="_blank">Domain Driven Design</a> seems to being just doing that. <br /> <br /><strong>Back to book</strong> <br />When you order a book about Design Patterns you expect it to be more a reference then a complete read, because the stuff looks to be boring ('dry stuff' as we say here), without trying it out. <br />In this book however the author takes you on a journey of <strong>software architecture</strong>, <strong>design</strong> and <strong>decisions</strong>. The book gives lots of "<strong>Aha!</strong>", "<strong>Huh?</strong>" and "<strong>Done that</strong>" moments which, I guess, keeps you awake. <br />It is loaded with lots of solutions for particular (recognizable) software problems, not just pushing Design Patterns, but making you think more about the right solutions, which <strong>always depends</strong>....</p> <p>In just three days I have read half of the book already! For me that is rare. <br /> <br />So if you are interested in better software design I can highly recommend this book, it reads like a breeze!</p>Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com1tag:blogger.com,1999:blog-8730195.post-17876925797402733182009-04-24T12:39:00.001-07:002009-04-24T12:45:43.882-07:00Exploring Test Driven Development<p>A few weeks ago I attended a software event organized by the dutch <a href="http://www.sdn.nl" target="_blank">Software Developer Network</a> SDN. This time I followed a few sessions about <a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank">Test Driven Development</a> presented by (among others) <a href="http://www.alexthissen.nl/blogs/main/" target="_blank">Alex Thissen</a>, <a href="http://blogs.oosterkamp.nl/blogs/thomas/" target="_blank">Thomas Huijer</a> and <a href="http://www.dennisdoomen.net" target="_blank">Dennis Doomen</a>.</p> <blockquote> <p>I must admit that before this sessions I was convinced that automated unit testing was a plus, but it would take just too much time (at least that is what I thought) so that I never actually used it. Triggered by this sessions I started to explore TDD and slowly but surely I am convinced by its pros.</p> </blockquote> <p><strong>So what is it?</strong> <br />TDD is not strictly about testing, but it is a <a href="http://en.wikipedia.org/wiki/Software_engineering" target="_blank">software design method</a> build upon testing. Check the <a href="http://en.wikipedia.org/wiki/Test-driven_development" target="_blank">TDD wiki</a> for all the details, but basically it drills down to the following: <br /> <br />Every new feature in an application <strong>starts with building a test</strong> around one unit of code (a method or property), because of the fact that the real feature is not yet written that test will fail. After making the test you write the actual method which correctness can be checked by running the test. <br /><strong>Kind of strange huh?</strong> First test, then code. But when you think about it a little more it is a great way to improve your code quality.</p> <p>Consider the following: <br />- By building a test you are triggered to think more about the <strong>specifications</strong>, heck by building the test you 'persist' the specifications in your code. (If specification changes, the test changes) <br />- It doesn't matter who writes the actual method, as long as the test fails he/she did not comply to the specifications. <br />- New features and specifications, which leads to refactoring your code, will be checked against your tests. If the test passes you will be fine. <br />In fact in TDD you will first change/refactor your test before refactoring the real code. <br />- You will try to keep test as small as possible, thus making smaller methods in your applications, thus making it less complex. <br />Less complex means more maintainable in the future. (Less time/costs)</p> <blockquote> <p><em>“I don't care how good you think your design is. If I can't walk in and write a test for an arbitrary method of yours in five minutes its not as good as you think it is, and whether you know it or not, you're paying a price for it.” <br /><a href="http://www.michaelfeathers.com/" target="_blank">Michael Feathers</a></em></p> </blockquote> <p><strong>Testing in Visual Studio</strong> <br />Visual Studio 2008 now offers unit testing. You can add a test project in your solution that refers your application/code library.</p> <p><strong>Making a first TDD application</strong>  <br />Suppose we have some class Calculator with methods like Add, Divide etcetera. <br /> <br /><strong>Step 1</strong> <br />Create two test methods for the Add and Divide methods. A test method's result is stated by either an Assert, or an expected Exception:</p> <div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ca91995b-5382-4a34-bf31-811e79bd7c87" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;">[TestMethod]<br /> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Add()<br /> {<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Int1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">2</span><span style="color: #000000;">;<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Int2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">3</span><span style="color: #000000;">;<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Result </span><span style="color: #000000;">=</span><span style="color: #000000;"> calculator.Add(Int1, Int2);<br /> Assert.AreEqual(</span><span style="color: #800080;">5</span><span style="color: #000000;">, Result); <br /> }<br /><br /> [TestMethod]<br /> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> Divide()<br /> {<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Int1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">6</span><span style="color: #000000;">;<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Int2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">3</span><span style="color: #000000;">;<br /> </span><span style="color: #0000FF;">double</span><span style="color: #000000;"> Result </span><span style="color: #000000;">=</span><span style="color: #000000;"> calculator.Divide(Int1, Int2);<br /> Assert.AreEqual(</span><span style="color: #800080;">2</span><span style="color: #000000;">, Result); <br /> }<br /><br /> [TestMethod]<br /> [ExpectedException(</span><span style="color: #0000FF;">typeof</span><span style="color: #000000;">(DivideByZeroException))]<br /> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">void</span><span style="color: #000000;"> DivideByZero()<br /> {<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Int1 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">6</span><span style="color: #000000;">;<br /> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Int2 </span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #0000FF;">double</span><span style="color: #000000;"> Result </span><span style="color: #000000;">=</span><span style="color: #000000;"> calculator.Divide(Int1, Int2);<br /> }<br /></span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div><br />In the test unit we created our Calculator class which we will test. Of course without the actual code this will not compile, so we create the initial method stubs, which will throw a not implemented exception.<br /><p><strong>Step 2</strong> <br />Run the tests. No supprise they will fail. <br /> <br /><strong>Step 3</strong> <br />Write the real methods: <br /> </p><br /><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:5c4a7003-9271-4c95-a4d4-8e1e1fbe41bf" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br />--><span style="color: #000000;"> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Add(</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Int1, </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Int2)<br /> {<br /> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> Int1 </span><span style="color: #000000;">+</span><span style="color: #000000;"> Int2;<br /> }<br /><br /> </span><span style="color: #0000FF;">public</span><span style="color: #000000;"> </span><span style="color: #0000FF;">double</span><span style="color: #000000;"> Divide(</span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Int1, </span><span style="color: #0000FF;">int</span><span style="color: #000000;"> Int2)<br /> {<br /> </span><span style="color: #0000FF;">return</span><span style="color: #000000;"> Int1 </span><span style="color: #000000;">/</span><span style="color: #000000;"> Int2;<br /> }<br /> }</span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div><br /><strong>Step 4 <br /></strong>Run the test. They succeed! Because the not so well choosen parameter names you could easily mismatch them in the method Divide, but your test will check for that! <br /><p><strong>What is next? <br /></strong>This example is rather simple. Complex codes, with for example database interaction will require some more coding for which you will write <strong>fakes and mocks</strong>. I will explore them in another post. <br /> <br /><strong>Conclusion <br /></strong>Although you might think that Test Driven Development takes a lot time in the process I am convinced (now) that it will pay back further on in the development process. Beside that it will boost the quality of your code and give you a base to safely refactor it in the future. <br /> <br />The examples where build in C#. I am not sure if this works in Delphi Prism as well, but I expect that it does. </p>Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com3tag:blogger.com,1999:blog-8730195.post-87795414251596526282009-04-08T04:28:00.001-07:002009-04-08T04:28:11.561-07:00CodeRush, when code smells...<p>At the beginning of this month <a href="http://www.devexpress.com/" target="_blank">Devexpress</a> released <a href="http://www.devexpress.com/Products/NET/DXperience/WhatsNew2009v1/" target="_blank">the new version</a> of their DXPerience products for .NET (Version 2009.1) <br />With the release of this components they also released a new version of their coding assisting tool <a href="http://www.devexpress.com/Products/NET/DXperience/WhatsNew2009v1/index.xml?page=7#autolist16" target="_blank">CodeRush and Refactor Pro</a>. <br />We use CodeRush for a couple of years now in Visual Studio for our C# work. Strange enough this new version somehow managed to trigger the "<strong>wow</strong>" factor once again. <br />I will not explain every new feature here, (You will find some resources at the end of this post) but one that is <strong>awesome</strong> in particular is <strong>Code Issues</strong>. </p> <p>The <strong>Code Issues feature</strong> analyses your source code while your are working in your code. It gives you hints for <strong>Unused namespace references</strong>, <strong>unused methods</strong>, <strong>undisposed local vars</strong> etc, etc. <br /> <br /><strong>How does it work?</strong> <br />Hints appear in a bar near the scrollbar of the editor when you hover the mouse over the colored lines (each color is a hint category): <br /><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj6fKQ4ljwclHfDPr7MOsce5tc-vXKAMyzuyfN11zOkaDkYMExDjlCfIpU4ndgOLsVZGn9-LDMwXUq3ZbypFOJT4q53TwSKlYDJeV9LsEwfrz6eXiwcBMXWB7NvgnDX-7Y2yk9Yfg/s1600-h/cr2%5B3%5D.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="157" alt="Code Smell" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg1hf3Tyo5qMGin0jCqRPP5vq_jhrWJpx_A-Symt0ZC3JVp3mWlwXcqKTn-LxEvlAN_T8VmCAd0812rU0PDA2hJPxt1AC11H5HSIVAvnhO4EWroYAVPSnJ0Hzt7G1a3nhaL7pU0OQ/?imgmax=800" width="302" border="0" /></a>   <br />In this particular example, which seems to be extracted from some farmer application ;-), Coderush hints that a line of code where two strings are concatenated, could be refactored using string.format. <br /> <br />When you hover the code line, it shows you what you could do to improve your code: (or make it smell better)<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi8wZcfPCePHzmdchYcJ9HAB_jBM68DzmU-tA01JNJnqF5BW8Q9MDB9p2nTOQp11p6laC4NWBKDwwIINBZVZZKz8iEPMrFsAqSgRg7rE6Jje0tRDQHDgzllbVMbrW0vnZ93RZbEog/s1600-h/cr1%5B3%5D.jpg"><img style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="139" alt="cr1" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEirAPVrxwy-Macia_1UOzoy5d56rTdE2RbjYI70YbaQYh3WFJmION1fc0xyNyFP3KcER4cJOoyER5aOfZODBxeQ1oSESypmNoVho6HZnsqxOjtYFhsK-cKi1n5UkwbrhMYvilJrkA/?imgmax=800" width="402" border="0" /></a> <br />This is really <strong>an impressive cool feature</strong>, that will boost the quality of your code. <br /> <br /><strong>Delphi Prism <br /></strong>Unfortunately CodeRush only supports <strong>C#</strong> and <strong>VB.NET</strong> at this moment. It would be very nice if CodeRush <a href="http://beensoft.blogspot.com/2008/11/devexpress-will-support-delphi-prism.html" target="_blank">would support Delphi Prism</a> as well. <br /> <br /><strong>Conclusion</strong> <br />Beside the fact that CodeRush helps you to boost your productivity with lots of code templates, it also helps you to track code smells and refactor them out of your software which will eventualy increase the quality of it. <br /> <br />You can find more resources on CodeRush here: <br /><a href="http://www.devexpress.com/Products/Visual_Studio_Add-in/Coding_Assistance/" target="_blank">The Devexpress website</a> <br />Mark Miller blog post "<a href="http://community.devexpress.com/blogs/markmiller/archive/2009/04/01/what-s-new-in-coderush-amp-refactor-pro-9-1.aspx" target="_blank">What's new in CodeRush & Refactor Pro</a>" <br /><a href="http://rorybecker.blogspot.com/2009/03/coderush-and-refactor-910-beta.html" target="_blank">This blogpost by Rory Becker</a></p> Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com9tag:blogger.com,1999:blog-8730195.post-86669572758022346202009-03-12T00:59:00.001-07:002009-03-12T04:03:48.298-07:00New release for Delphi Prism<p>There is a new release of Delphi Prism available, according <a href="http://edn.embarcadero.com/article/39408" target="_blank">this edn article</a>. The <strong>Delphi Prism February 2009 release</strong> (Build 3.0.17.591) as it is called (Wasn't it already March? ;-) ) contains the following <a href="http://prismwiki.codegear.com/en/Changes_and_Known_Issues#Changes_and_Resolved__Issues_in_Build_3.0.17.591%22" target="_blank">changes</a>. </p> Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com1tag:blogger.com,1999:blog-8730195.post-31870988084574534122009-01-07T02:33:00.001-08:002009-01-07T02:37:13.616-08:00TIOBE language of the year not Delphi<p>In october Jim Mckeeth <a href="http://www.delphi.org/2008/10/delphi-language-of-the-year-2008/" target="_blank">reported</a> that Delphi could well be the language of the year in the <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html" target="_blank">TIOBE Community language index</a>. <br />Unfortunately this has not happened despite the efforts of the Delphi community putting "Delphi programming" on webpages. <br /> <br />In <a href="http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html" target="_blank">january</a> Delphi is fallen slightly one place back to position 10. (Nothing to worry about). Remarkable is that Pascal went up 3 places compared with last year, now holding the 15th position. I predict that Pascal will become the language of the year 2009! ;-)</p> <p>Language of the year 2008: C</p> <p>For what it is worth........ <br /> <br />What is your prediction?</p> Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com4tag:blogger.com,1999:blog-8730195.post-40590086982806080682009-01-06T12:41:00.001-08:002009-01-06T12:42:15.162-08:00LINQ with Delphi Prism #2 : Deferred Execution<p>As said in <a href="http://beensoft.blogspot.com/2009/01/linq-with-delphi-prism-1-sequences.html" target="_blank">this previous post</a> Delphi Prism supports LINQ all the way so all the LINQ feature just work in Delphi Prism. <br /> <br /><strong>Deferred Execution <br /></strong>One of the key features of LINQ is <strong>Deferred Execution</strong>. That means that a LINQ query does not execute until data is demanded, for except by an iteration through a list. This makes it possible to make a complex query without execution each part of the query seperately. <br /> <br />However, there is one exceptions to the rule, a LINQ query <strong>executes immediately</strong> when you use a function that must iterate over the list for its result like, for example <strong>Sum(), Count(), ToList()</strong> etc. <br /> <br /></p> <div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:691b3a09-5fed-441d-a173-7049265a41cf" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;">Example of deferred execution or lazy execution<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">-------------------------------------------</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> </span><span style="color: #0000FF;">var</span><span style="color: #000000;"> Numbers :</span><span style="color: #000000;">=</span><span style="color: #000000;"> new List</span><span style="color: #000000;"><</span><span style="color: #000000;">integer</span><span style="color: #000000;">></span><span style="color: #000000;">;<br /> Numbers.Add(</span><span style="color: #800080;">1</span><span style="color: #000000;">);<br /> </span><span style="color: #0000FF;">var</span><span style="color: #000000;"> DoubledList : sequence </span><span style="color: #0000FF;">of</span><span style="color: #000000;"> integer <br /> :</span><span style="color: #000000;">=</span><span style="color: #000000;"> Numbers.Select(n</span><span style="color: #000000;">-></span><span style="color: #000000;">n</span><span style="color: #000000;">*</span><span style="color: #800080;">2</span><span style="color: #000000;">);<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Query not yet executed!<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;"> Add another number to the list</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> Numbers.Add(</span><span style="color: #800080;">2</span><span style="color: #000000;">);<br /> <br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">At this point query is executed<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">Result "2--4"</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> </span><span style="color: #0000FF;">for</span><span style="color: #000000;"> i : Integer </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> DoubledList </span><span style="color: #0000FF;">do</span><span style="color: #000000;"> </span><span style="color: #0000FF;">begin</span><span style="color: #000000;"><br /> Result :</span><span style="color: #000000;">=</span><span style="color: #000000;"> Result </span><span style="color: #000000;">+</span><span style="color: #000000;"> i.ToString </span><span style="color: #000000;">+</span><span style="color: #000000;"> </span><span style="color: #800000;">'</span><span style="color: #800000;">--</span><span style="color: #800000;">'</span><span style="color: #000000;">;<br /> </span><span style="color: #0000FF;">end</span><span style="color: #000000;">;<br /> MessageBox.Show(Result);<br /><br /><br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">Example of direct execution<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">-------------------------------------------</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> </span><span style="color: #0000FF;">var</span><span style="color: #000000;"> Numbers :</span><span style="color: #000000;">=</span><span style="color: #000000;"> new List</span><span style="color: #000000;"><</span><span style="color: #000000;">integer</span><span style="color: #000000;">></span><span style="color: #000000;">;<br /> Numbers.Add(</span><span style="color: #800080;">1</span><span style="color: #000000;">);<br /> Numbers.Add(</span><span style="color: #800080;">2</span><span style="color: #000000;">);<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">Query executed here</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> </span><span style="color: #0000FF;">var</span><span style="color: #000000;"> Sum :</span><span style="color: #000000;">=</span><span style="color: #000000;"> (Numbers.Select(n</span><span style="color: #000000;">-></span><span style="color: #000000;">n</span><span style="color: #000000;">*</span><span style="color: #800080;">2</span><span style="color: #000000;">)).Sum();<br /> Numbers.Add(</span><span style="color: #800080;">3</span><span style="color: #000000;">);<br /> Numbers.Add(</span><span style="color: #800080;">4</span><span style="color: #000000;">);<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">Would expect 20, but result is only 6</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> MessageBox.Show(Sum.ToString); <br /></span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div> Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com0tag:blogger.com,1999:blog-8730195.post-69326884023842216322009-01-05T12:40:00.001-08:002009-01-05T12:56:30.120-08:00LINQ with Delphi Prism #1 : Sequences<p>I have been digging into LINQ this christmas. LINQ is a cool way to query data, whether it is an <strong>array</strong>, a <strong>list</strong> or a <strong>remote datasource</strong>. <br />LINQ is a new feature of <strong>C# 3.0</strong> that comes with Visual Studio 2008, but guess what, <a href="http://www.codegear.com/products/delphi/prism" target="_blank">Delphi Prism</a>, also supports LINQ all the way. </p> <blockquote> <p>If you want to play/test LINQ queries syntax you can download and install <a href="http://www.linqpad.net/">LinqPad</a>, a free tool to use LINQ querys against a SQL Server database. (You could drop Management Studio for that). </p> </blockquote> <p>The only disadvantage I found (IMO) on LINQ so far is that quering remote datasources is bound to Microsoft SQL Server database. (Maybe this will be extended in the future) <br />That involves, the so called "<strong>LINQ to SQL</strong>" methods, which is, I believe, allready deprecated. </p> <p>As said before <strong>Delphi Prism completely supports LINQ</strong>. Starting a 3.5 framework application will give you a reference to <strong>System.Linq</strong>, the namespace, where LINQ lives.</p> <p>In this series I will explore LINQ using Delphi Prism: </p> <p><strong>1. Using LINQ on a sequence</strong> <br />LINQ, in Delphi Prism, works on so called <strong>sequences</strong>, which is a new type described in the <a href="http://prismwiki.codegear.com/">Delphi Prism Wiki</a> as: </p> <blockquote> <p>Sequences are a special type in the language and can be thought of as a collection of elements, similar to an array. </p> </blockquote> <p>Declare a sequence like this: </p> <div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:ea535058-4b9e-4e7c-a499-99eb8a282a13" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; width: 437px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #0000FF;">var</span><span style="color: #000000;"><br /> Names : sequence </span><span style="color: #0000FF;">of</span><span style="color: #000000;"> String :</span><span style="color: #000000;">=</span><span style="color: #000000;"> <br /> [</span><span style="color: #800000;">'</span><span style="color: #800000;">Mickey</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">Dick</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">Roland</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">Delphi</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">Harry</span><span style="color: #800000;">'</span><span style="color: #000000;">];<br /></span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div><br /><p>With LINQ it is very easy to query a collection, like this sequence. You can do this in two ways, namely: <br />1.<strong> Using Lambda expressions</strong> <br />2. <strong>Using query comprehension syntax</strong> (Query Syntax)</p><br /><p>With <a href="http://prismwiki.codegear.com/en/Lambda_Expressions" target="_blank">Lambda expressions</a> you can write rather small expressions to query the data, with the <a href="http://prismwiki.codegear.com/en/Query_Expressions" target="_blank">query syntax</a> they become more readable (read less magic ;-)). <br />By the way the compiler (at least in C#) will translate the query syntax into lambda expressions. Both techniques are complementary. (I think the Oxygene compiler also does this....)</p> <br /><p>Suppose we want to have all the names from our sequence which have more then 4 characters, containing an "a" and sorted in upper case. </p><br /><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:bc14fc14-f145-48df-98ef-fc5d929aea21" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;"> </span><span style="color: #008000;">//</span><span style="color: #008000;">Using Lambda syntax</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> </span><span style="color: #0000FF;">var</span><span style="color: #000000;"> FilteredNames :</span><span style="color: #000000;">=</span><span style="color: #000000;"> Names<br /> .Where (n </span><span style="color: #000000;">-></span><span style="color: #000000;"> (n.Length </span><span style="color: #000000;">>=</span><span style="color: #000000;"> </span><span style="color: #800080;">4</span><span style="color: #000000;">) <br /> </span><span style="color: #0000FF;">and</span><span style="color: #000000;"> n.Contains(</span><span style="color: #800000;">'</span><span style="color: #800000;">a</span><span style="color: #800000;">'</span><span style="color: #000000;">))<br /> .OrderBy (n </span><span style="color: #000000;">-></span><span style="color: #000000;"> n)<br /> .Select (n </span><span style="color: #000000;">-></span><span style="color: #000000;"> n.ToUpper());<br /> <br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">Using query comprehension syntax</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> </span><span style="color: #0000FF;">var</span><span style="color: #000000;"> FilteredNames :</span><span style="color: #000000;">=</span><span style="color: #000000;"> from n </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> Names<br /> where ((n.Length </span><span style="color: #000000;">>=</span><span style="color: #000000;"> </span><span style="color: #800080;">4</span><span style="color: #000000;">) <br /> </span><span style="color: #0000FF;">and</span><span style="color: #000000;"> n.Contains(</span><span style="color: #800000;">'</span><span style="color: #800000;">a</span><span style="color: #800000;">'</span><span style="color: #000000;">))<br /> order by n<br /> select n.ToUpper;<br /><br /></span><span style="color: #008000;">//</span><span style="color: #008000;">Note we don't have to declare FilteredNames</span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div><br /><p>Iterate through the filterednames to show them in a messagebox:<br /> <div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:1775656e-5886-41e6-8c6d-2f60f60d7562" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;"> </span><span style="color: #0000FF;">for</span><span style="color: #000000;"> s : string </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> FilteredNames </span><span style="color: #0000FF;">do</span><span style="color: #000000;"> </span><span style="color: #0000FF;">begin</span><span style="color: #000000;"><br /> FilteredOnes :</span><span style="color: #000000;">=</span><span style="color: #000000;"> FilteredOnes </span><span style="color: #000000;">+</span><span style="color: #000000;"> s </span><span style="color: #000000;">+</span><span style="color: #000000;"> "</span><span style="color: #000000;">--</span><span style="color: #000000;">" <br /> </span><span style="color: #0000FF;">end</span><span style="color: #000000;">;<br /><br /> MessageBox.Show(</span><span style="color: #800000;">'</span><span style="color: #800000;">FilteredOnes: </span><span style="color: #800000;">'</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> FilteredOnes) </span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div><br /></p><br /><p>Note that the original collection, Names in this case, is still holding all the elements.</br><br />Conclusion LINQ is a, in basic, simple way to query collections the SQL way. Delphi Prism supports them complete. Choosing Lambda expression or Query syntax will be mostly a personal preference.</p>Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com2tag:blogger.com,1999:blog-8730195.post-75732163363645593102008-11-20T05:24:00.001-08:002008-11-20T05:24:43.946-08:00DevExpress will support Delphi Prism<p>Now that Delphi for .NET (now Delphi Prism) has become a Visual Studio plugin it is possible to use all the .NET components out there. <br /> <br /><a href="http://www.devexpress.com" target="_blank">DevExpress</a>, which btw started as a Delphi shop, has a great component set called <a href="http://www.devexpress.com/Products/NET/DXperience/" target="_blank">DXperience</a> for ASP.NET and Winforms development. Until now they only supported the Visual Studio IDE's. <br />You could use this components also in Delphi for .NET however they were not officialy supported. (No installation and technical support)</p> <p>According to <a href="http://community.devexpress.com/blogs/ctodx/archive/2008/11/19/dxperience-and-delphi-prism.aspx" target="_blank">this blogpost</a> by Julian Bucknall the CTO of DevExpress, it looks like they will <strong>support Delphi Prism in the official sense of the word</strong>.</p> <blockquote> <p>The story is that we shall be supporting Delphi Prism with DXperience and we're evaluating what we need to do to make that work. As it happens: not very much -- it installs just fine and, on first blush, seems to work just fine. There are more exhaustive tests to complete, obviously.</p> </blockquote> <p>Of course this is not an official statement......</p> <p>We use the DXPerience components daily in our C# projects, and they are just great. We feel that Visual Studio with C#/VB.NET/Delphi Prism + DXPerience gives the developer the same power as Delphi for Win32 as we know it for so long. (For Winforms developement).</p> <p> In my opinion this is great step for Delphi Prism in the .NET world. </p> Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com0tag:blogger.com,1999:blog-8730195.post-60120613520857456822008-10-27T12:14:00.000-07:002008-10-27T14:12:36.565-07:00Delphi Prism, some first thoughtsDelphi Prism is announced in <a href="http://www.codegear.com/products/delphi/prism">this Embarcadero press release</a>. Delphi Prism is based on Oxygene from <a href="http://www.remobjects.com">Remobjects</a>, so I think you could say <a href="http://www.codegear.com/products/delphi/prism">Delphi Prism </a>is Remobject's <a href="http://www.remobjects.com/oxygene.aspx">Oxygene</a>. Read <a href="http://www.remobjects.com/oxygene.aspx">Remobjects press release here</a>.<br /><br />Added: <a href="http://www.bitwisemag.com/2/Delphi-Prism-Visual-Studio-Pascal">Interview with marc hoffman</a>, Chief Architect at Remobjects on Bitwise Magazine.<br /><br />After five years of several solutions and directions, it looks like Delphi for .NET has finally found a good home.<br /><br />Although Delphi .NET only exists for only about four years, there have been a lot of changes in the strategy executed by Borland, Borland/CodeGear and now Embarcadero. So let's take a brief look at the Delphi for .NET history:<br /><br /><strong>2004 - Delphi 8 (Borland)</strong><br />Delphi for .NET is born for the .NET Framework version 1.1. Delphi for .NET is positioned as a <strong>first class .NET citizen</strong>. It offers .NET 1.1 development for Winforms, ASP.NET and VCL.NET, which is the .NET variant of the VCL, offering a highly compatible framework based on .NET.<br /><br /><strong>2005 - Delphi 2005 (Borland)</strong><br />Delphi 2005 offers the same as Delphi 8 but Delphi 2005 has a lot improvements in quality.<br /><br /><strong>2006 - Delphi 2006 (Borland)</strong><br />Delphi 2006 still has support for the .NET Framework 1.1 (Winforms, ASP.NET and VCL.NET) Delphi 2006 offers a stable IDE for this developments. The fact that it does not support .NET 2.0 shows the trouble that Borland had to keep up with Microsoft.<br /><br /><strong>2007 - Delphi 2007 (CodeGear/Borland)</strong><br />Delphi 2007 finally supports .NET 2.0, but unfortunately <a href="http://beensoft.blogspot.com/2007/06/codegear-to-drop-winforms-designer-in.html">the Winforms support is dropped</a>. (Only ASP.NET and VCL.NET is supported)<br /><br /><strong>2008 - Delphi 2008 (Embarcadero)</strong><br />Delphi 2009 has become a Visual Studio Plugin based on Oxygene (formely known as Chrome) offering support for all available Microsoft .NET technology's (2.0, 3.0, 3.5))<br />Unfortunately it looks like VCL.NET is dropped.<br /><br />It is not likely that the Delphi for .NET product strategy will be nominated for best executed product strategy ever. <g><br /><br />However this is a <strong>very strong sign </strong>of the new spirit that Embarcadero is bringing to Delphi. It proofs that <strong>the buyout by Embarcadero is the best thing </strong>that happened to Delphi (and of course CodeGear) in the last five years.<br /><br />Back to Delphi Prism:<br /><br /><strong>Delphi Prism the pros</strong><br />1. Instant support for all Microsoft .NET technologies.<br />2. The Delphi language is now at same level as C#, VB.NET, so the choice for the Delphi language can be made much easier. (Hack it is the same IDE)<br />3. Delphi for Win32 can focus on Win32 again, will not be hold back by .NET technologies.<br /><br /><strong>Delphi Prism the cons</strong><br />1. Less compatibilty with Delphi Win32 technologies, due to pure .NET and languages changes/additions. (Compared to VCL-VCL.NET, VCL-Winforms)<br />2. After Winforms, now VCL.NET dropped, and that will not please every body.<br /><br />All with all the pros weigh more than the cons in my opinion.<br /><br /><strong>Why use Delphi Prism?</strong><br />This question is not answered easily. I think a lot of Delphi developers went for C# in the last four years. For example we decided in 2007 to standarize our .NET development on Visual Studio C# due to the dropping of Winforms in Delphi 2007 (Basicaly due to the mixed/confusing messages by Borland). <br /><br /><strong>Conclusion</strong><br />Although we don't know all the ins and outs yet (sure will hear a lot more the coming week) this is by far the best thing ever that happened to Delphi for .NET.Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com6tag:blogger.com,1999:blog-8730195.post-73229274545636940772008-10-22T00:00:00.001-07:002008-10-22T03:14:48.699-07:00Exploring anonymous methods<p>New in Delphi 2009 are anonymous methods. Anonymous methods are methods without a name, which are able to do something on local variables where they are implemented. In fact they are <strong>a reference to a method</strong>. <br />Anonymous methods are a <strong>bit magic</strong>, at least they were to me, and hard to explain. <br />A nice place to use them is together with generics types. For instance the Sort method of a <a href="http://beensoft.blogspot.com/2008/09/simple-generic-list-tlist.html">TList<T></a> is <strong>a natural place</strong> to use them: </p><p><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E7:e2de5690-92c7-4bf1-a7f3-fb5d9bde112a" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #000000;"> PersonsList.Sort(TComparer</span><span style="color: #000000;"><</span><span style="color: #000000;">TPerson</span><span style="color: #000000;">></span><span style="color: #000000;">.Construct(<br /> </span><span style="color: #0000FF;">function</span><span style="color: #000000;">(</span><span style="color: #0000FF;">const</span><span style="color: #000000;"> Item1, Item2: TPerson): Integer<br /> </span><span style="color: #0000FF;">begin</span><span style="color: #000000;"><br /> Result :</span><span style="color: #000000;">=</span><span style="color: #000000;"> CompareText(Item1.LastName, <br /> Item2.LastName);<br /> </span><span style="color: #0000FF;">end</span><span style="color: #000000;">));</span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div><br />A cool thing about anonymous methods is that <strong>it can execute your code (incl. local vars) in another place</strong>. This can be <strong>another unit</strong> or even <strong>another thread</strong>.<br /><br />Implementing them in your daily code however is another thing. How to do that? Well here are <strong>my first explorations</strong>: Let's say we have this simple application that has a TStringList where we want to do some searching on the strings, based on the users input. I came up with this:</p><br /><div class="wlWriterSmartContent" id="scid:57F11A72-B0E5-49c7-9094-E3A15BD5B5E6:70517719-f929-4c70-a9d6-fd0c6fe497af" style="padding-right: 0px; display: inline; padding-left: 0px; float: none; padding-bottom: 0px; margin: 0px; padding-top: 0px"><pre style="background-color:White;;overflow: auto;"><div><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><span style="color: #008000;">//</span><span style="color: #008000;">Declaration of the anon reference <br /></span><span style="color: #008000;">//</span><span style="color: #008000;">The passed string will be searched on</span><span style="color: #008000;"><br /></span><span style="color: #0000FF;">type</span><span style="color: #000000;"><br /> TAnonStrProc </span><span style="color: #000000;">=</span><span style="color: #000000;"> <br /> reference </span><span style="color: #0000FF;">to</span><span style="color: #000000;"> </span><span style="color: #0000FF;">function</span><span style="color: #000000;">(s : string) : Integer;<br /><br /></span><span style="color: #008000;">//</span><span style="color: #008000;">Implement a method to execute <br /></span><span style="color: #008000;">//</span><span style="color: #008000;">1. Ask a string to search<br /></span><span style="color: #008000;">//</span><span style="color: #008000;">2. Execute the referenced method "proc"</span><span style="color: #008000;"><br /></span><span style="color: #0000FF;">function</span><span style="color: #000000;"> TForm4.FindString(proc: TAnonStrProc)<br /> : Integer;<br /></span><span style="color: #0000FF;">var</span><span style="color: #000000;"><br /> SearchStr : string;<br /></span><span style="color: #0000FF;">begin</span><span style="color: #000000;"><br /> Result :</span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #000000;">-</span><span style="color: #800080;">1</span><span style="color: #000000;">;<br /> </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> InputQuery(</span><span style="color: #800000;">'</span><span style="color: #800000;">Search</span><span style="color: #800000;">'</span><span style="color: #000000;">, </span><span style="color: #800000;">'</span><span style="color: #800000;">Search a string</span><span style="color: #800000;">'</span><span style="color: #000000;">, <br /> SearchStr) </span><span style="color: #0000FF;">then</span><span style="color: #000000;"> </span><span style="color: #0000FF;">begin</span><span style="color: #000000;"><br /> Result :</span><span style="color: #000000;">=</span><span style="color: #000000;"> proc(SearchStr);<br /> </span><span style="color: #0000FF;">end</span><span style="color: #000000;">;<br /></span><span style="color: #0000FF;">end</span><span style="color: #000000;">;<br /> <br /></span><span style="color: #008000;">//</span><span style="color: #008000;">Create a string list</span><span style="color: #008000;"><br /></span><span style="color: #0000FF;">procedure</span><span style="color: #000000;"> TForm4.Button1Click(Sender: TObject);<br /></span><span style="color: #0000FF;">var</span><span style="color: #000000;"><br /> sla : TStrings;<br /> i,j : Integer;<br /> Index, Count : integer;<br /></span><span style="color: #0000FF;">begin</span><span style="color: #000000;"><br /> sla :</span><span style="color: #000000;">=</span><span style="color: #000000;"> TStringList.Create;<br /> </span><span style="color: #0000FF;">try</span><span style="color: #000000;"><br /> sla.Add(</span><span style="color: #800000;">'</span><span style="color: #800000;">One</span><span style="color: #800000;">'</span><span style="color: #000000;">);<br /> sla.Add(</span><span style="color: #800000;">'</span><span style="color: #800000;">Two</span><span style="color: #800000;">'</span><span style="color: #000000;">);<br /> sla.Add(</span><span style="color: #800000;">'</span><span style="color: #800000;">Three</span><span style="color: #800000;">'</span><span style="color: #000000;">);<br /> sla.Add(</span><span style="color: #800000;">'</span><span style="color: #800000;">Two</span><span style="color: #800000;">'</span><span style="color: #000000;">);<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">Call the FindString method with anon method<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">to get the index of string in the list<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">Note that it does someting with our local<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">variable sla</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> Index :</span><span style="color: #000000;">=</span><span style="color: #000000;"> FindString(</span><span style="color: #0000FF;">function</span><span style="color: #000000;">(s : string) : Integer<br /> </span><span style="color: #0000FF;">begin</span><span style="color: #000000;"><br /> Result :</span><span style="color: #000000;">=</span><span style="color: #000000;"> sla.IndexOf(s);<br /> </span><span style="color: #0000FF;">end</span><span style="color: #000000;">);<br /> ShowMessage(</span><span style="color: #800000;">'</span><span style="color: #800000;">Index: </span><span style="color: #800000;">'</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> IntToStr(Index));<br /> <br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">Call the FindString method with anon method<br /> </span><span style="color: #008000;">//</span><span style="color: #008000;">to get the count of a searched string</span><span style="color: #008000;"><br /></span><span style="color: #000000;"> Count :</span><span style="color: #000000;">=</span><span style="color: #000000;"> FindString(</span><span style="color: #0000FF;">function</span><span style="color: #000000;">(s : string) : Integer<br /> </span><span style="color: #0000FF;">var</span><span style="color: #000000;"><br /> str : string;<br /> i : integer;<br /> </span><span style="color: #0000FF;">begin</span><span style="color: #000000;"><br /> Result :</span><span style="color: #000000;">=</span><span style="color: #000000;"> </span><span style="color: #800080;">0</span><span style="color: #000000;">;<br /> </span><span style="color: #0000FF;">for</span><span style="color: #000000;"> str </span><span style="color: #0000FF;">in</span><span style="color: #000000;"> sla </span><span style="color: #0000FF;">do</span><span style="color: #000000;"> </span><span style="color: #0000FF;">begin</span><span style="color: #000000;"><br /> </span><span style="color: #0000FF;">if</span><span style="color: #000000;"> str </span><span style="color: #000000;">=</span><span style="color: #000000;"> s </span><span style="color: #0000FF;">then</span><span style="color: #000000;"><br /> Inc(Result);<br /> </span><span style="color: #0000FF;">end</span><span style="color: #000000;">;<br /> </span><span style="color: #0000FF;">end</span><span style="color: #000000;">);<br /> ShowMessage(</span><span style="color: #800000;">'</span><span style="color: #800000;">Count: </span><span style="color: #800000;">'</span><span style="color: #000000;"> </span><span style="color: #000000;">+</span><span style="color: #000000;"> IntToStr(Count));<br /> </span><span style="color: #0000FF;">finally</span><span style="color: #000000;"><br /> sla.Free;<br /> </span><span style="color: #0000FF;">end</span><span style="color: #000000;">;<br /></span><span style="color: #0000FF;">end</span><span style="color: #000000;">;</span></div></pre><!-- Code inserted with Steve Dunn's Windows Live Writer Code Formatter Plugin. http://dunnhq.com --></div><br /><p>Please note that this example probably <strong>won't make it in the daily code</strong>, but it shows in a simple manner how they work. Because of the fact that our anonymouse method can take any method as long that it has a string as parameter you can call one method FindString with different implementations by passing different methods.</p><br /><p>Well I think anonymous methods are cool. Implement them in your daily code wisely is not that simple. The working however, which seems magic at first, is after some exploring not that hard to follow.</p>Roland Beenhakkerhttp://www.blogger.com/profile/05387682563668003098noreply@blogger.com0