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

<channel>
	<title>Coding Friends &#187; mono</title>
	<atom:link href="http://www.codingfriends.com/index.php/tag/mono/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP SOAP server and .NET Client</title>
		<link>http://www.codingfriends.com/index.php/2010/04/19/php-soap-server-and-net-client/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=php-soap-server-and-net-client</link>
		<comments>http://www.codingfriends.com/index.php/2010/04/19/php-soap-server-and-net-client/#comments</comments>
		<pubDate>Mon, 19 Apr 2010 19:30:49 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[Client]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[Server]]></category>
		<category><![CDATA[SOAP]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=893</guid>
		<description><![CDATA[On a previous post, it was the other way around a .NET SOAP server and a PHP client and this one is a PHP SOAP server talking to a .NET client. I am using a similar PHP SOAP Server output as before, but having to alter the return type to a complexType instead of a [...]]]></description>
			<content:encoded><![CDATA[<p>On a previous post, it was the other way around a <a href="http://www.codingfriends.com/index.php/2010/04/16/soap-client-calling-net-web-service/">.NET SOAP server and a PHP client</a> and this one is a PHP SOAP server talking to a .NET client.  I am using a similar <a href="http://www.codingfriends.com/index.php/2010/04/12/soap-server/">PHP SOAP Server</a> output as before, but having to alter the return type to a complexType instead of a normal PHP SOAP server type.</p>
<p>The basic WSDL generation is very similar to the previous SOAP post, apart from parameter passed the Zend_Soap_AutoDiscover which is &#8220;Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex&#8221;, which creates the start of the newer return complexTypes.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'wsdl'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$autodiscover</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Soap_AutoDiscover<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$autodiscover</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'QuoteOfTheDay'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$autodiscover</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>here is the new return type for the function to return, it is a class that has public variable that is a string (you define as &#8220;&#8221; which sets up a string in PHP)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// the return class type</span>
<span style="color: #000000; font-weight: bold;">class</span> theQuote <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// have a return value of type string</span>
<span style="color: #009933; font-style: italic;">/** @var string */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$getTheQuote</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and then just alter the phpDoc notation for the auto discovery with the Zend SOAP to create a return of theQuote class as above and alter the return variable to the class., below I have included the new WSDL output generated.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">/* phpdoc notation to return a complex type (a class) */</span>
  <span style="color: #009933; font-style: italic;">/**
  * @return theQuote
  */</span>
 <span style="color: #000000; font-weight: bold;">function</span> getQuote<span style="color: #009900;">&#40;</span><span style="color: #000088;">$quote</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$theQuoteR</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> theQuote<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* just encase the string is in uppercase*/</span>
    <span style="color: #000088;">$symbol</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$quote</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* if there is a quote for the day requested */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quotes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$quote</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$theQuoteR</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTheQuote</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quotes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$quote</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$theQuoteR</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">/* else error with default response*/</span>
      <span style="color: #000088;">$theQuoteR</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTheQuote</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quotes</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;monday&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$theQuoteR</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>here is the full source code for the PHP SOAP server</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">/* setup the including path for the zend library framework */</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'include_path'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'/usr/share/php/libzend-framework-php/'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//****************************************************</span>
<span style="color: #666666; font-style: italic;">// Zend Framework 1.8</span>
<span style="color: #b1b100;">include_once</span> <span style="color: #0000ff;">'Zend/Loader/Autoloader.php'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;Zend/Soap/Server.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">require_once</span> <span style="color: #0000ff;">&quot;Zend/Soap/AutoDiscover.php&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$loader</span> <span style="color: #339933;">=</span> Zend_Loader_Autoloader<span style="color: #339933;">::</span><span style="color: #004000;">getInstance</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$loader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setFallbackAutoloader</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$loader</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">suppressNotFoundWarnings</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">//****************************************************</span>
&nbsp;
<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'wsdl'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$autodiscover</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Soap_AutoDiscover<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$autodiscover</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'QuoteOfTheDay'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$autodiscover</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$soap</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Soap_Server<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://localhost/projects/webservice/zend_soap_server_net.php?wsdl&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// this current file here</span>
    <span style="color: #000088;">$soap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setClass</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'QuoteOfTheDay'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$soap</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">handle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// the return class type</span>
<span style="color: #000000; font-weight: bold;">class</span> theQuote <span style="color: #009900;">&#123;</span>
<span style="color: #666666; font-style: italic;">// have a return value of type string</span>
<span style="color: #009933; font-style: italic;">/** @var string */</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$getTheQuote</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> QuoteOfTheDay <span style="color: #009900;">&#123;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">/* the quotes to be used from within the function getQuote */</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$quotes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;monday&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Monday's child is fair of face&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;tuesday&quot;</span><span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Tuesday's child is full of grace&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;wednesday&quot;</span> <span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Wednesday's child is full of woe&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">&quot;thursday&quot;</span> <span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Thursday's child has far to go&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;friday&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;Friday's child is loving and giving&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;saturday&quot;</span> <span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;Saturday's child works hard for a living&quot;</span><span style="color: #339933;">,</span>
  <span style="color: #0000ff;">&quot;sunday&quot;</span> <span style="color: #339933;">=&gt;</span><span style="color: #0000ff;">&quot;But the child who is born on the Sabbath Day - Is bonny and blithe and good and gay&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
  <span style="color: #666666; font-style: italic;">/* phpdoc notation to return a complex type (a class) */</span>
  <span style="color: #009933; font-style: italic;">/**
  * @return theQuote
  */</span>
 <span style="color: #000000; font-weight: bold;">function</span> getQuote<span style="color: #009900;">&#40;</span><span style="color: #000088;">$quote</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$theQuoteR</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> theQuote<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* just encase the string is in uppercase*/</span>
    <span style="color: #000088;">$symbol</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$quote</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* if there is a quote for the day requested */</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quotes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$quote</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000088;">$theQuoteR</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTheQuote</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quotes</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$quote</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$theQuoteR</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">/* else error with default response*/</span>
      <span style="color: #000088;">$theQuoteR</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getTheQuote</span><span style="color: #339933;">=</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">quotes</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;monday&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">return</span> <span style="color: #000088;">$theQuoteR</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Here is the newer WSDL output (snipped) from the new PHP SOAP server that creates a complexType return type for the .NET environment to be able to use.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;types<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
−
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:schema</span> <span style="color: #000066;">targetNamespace</span>=<span style="color: #ff0000;">&quot;http://localhost/projects/webservice/zend_soap_server_net.php&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
−
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:complexType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;theQuote&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
−
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:all<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xsd:element</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;getTheQuote&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;xsd:string&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsd:all<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsd:complexType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xsd:schema<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/types<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
−
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;portType</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;QuoteOfTheDayPort&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
−
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;operation</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;getQuote&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;documentation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>@return theQuote<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/documentation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;input</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;tns:getQuoteIn&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;output</span> <span style="color: #000066;">message</span>=<span style="color: #ff0000;">&quot;tns:getQuoteOut&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/operation<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/portType<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p><strong>Here is the .NET client</strong></p>
<p>Since we have a new WSDL generation, will need to generate a newer DLL that will allow the .NET to use the class structure.  Please note I am using mono as my .NET environment, so if you are using .NET in Windows then you could either include the WSDL within the Visual Studio or use very similar commands as below.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">wsdl2 http:<span style="color: #000000; font-weight: bold;">//</span>localhost<span style="color: #000000; font-weight: bold;">/</span>projects<span style="color: #000000; font-weight: bold;">/</span>webservice<span style="color: #000000; font-weight: bold;">/</span>zend_soap_server_net.php?wsdl
gmcs QuoteOfTheDayService.cs <span style="color: #000000; font-weight: bold;">/</span>t:library <span style="color: #000000; font-weight: bold;">/</span>r:System.Web.Services</pre></div></div>

<p>which will generate the QuoteOfTheDayService.dll and with the code below</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> codingfriendssoap
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">class</span> Test_Php_Soap
    <span style="color: #008000;">&#123;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
  QuoteOfTheDayService quoteService <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> QuoteOfTheDayService<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
  Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;mondays quote &quot;</span> <span style="color: #008000;">+</span> quoteService<span style="color: #008000;">.</span><span style="color: #0000FF;">getQuote</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;monday&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">getTheQuote</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>to compile that you once again I am using mono so these are the commands that I am using, but with .NET framework in Windows the commands may be different to compile the program e.g. (mcc)</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">gmcs web_service_client_php.cs <span style="color: #000000; font-weight: bold;">/</span>r:QuoteOfTheDayService.dll</pre></div></div>

<p>the /r includes the dll generated of the above generated PHP SOAP server this is the output</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mondays quote Monday<span style="color: #ff0000;">'s child is fair of face</span></pre></div></div>

<p>here is the XML response from the PHP SOAP server, which includes getTheQuote return wrapped around the theQuote class.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;SOAP-ENV:Body<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;ns1:getQuoteResponse<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;return</span> <span style="color: #000066;">xsi:type</span>=<span style="color: #ff0000;">&quot;ns1:theQuote&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span><span style="color: #000000; font-weight: bold;">&lt;getTheQuote</span> <span style="color: #000066;">xsi:type</span>=<span style="color: #ff0000;">&quot;xsd:string&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>Monday's child is fair of face<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/getTheQuote<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/return<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/ns1:getQuoteResponse<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/SOAP-ENV:Body<span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #000000; font-weight: bold;">&lt;/SOAP-ENV:Envelope<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/04/19/php-soap-server-and-net-client/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Mono &#8211; web development on Linux</title>
		<link>http://www.codingfriends.com/index.php/2010/04/14/mono-web-development-on-linux/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=mono-web-development-on-linux</link>
		<comments>http://www.codingfriends.com/index.php/2010/04/14/mono-web-development-on-linux/#comments</comments>
		<pubDate>Wed, 14 Apr 2010 09:38:45 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[Linux install and configurations]]></category>
		<category><![CDATA[Apache2]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[web server]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=873</guid>
		<description><![CDATA[It is really easy to get some web c# (csharp) (asp.net) development within a Linux setup, since I use kubuntu which is derived from ubuntu. All you need to do is to this installs the mono module for the apache2 aptitude install libapache2-mod-mono to enable the module to work within apache2 you have to update [...]]]></description>
			<content:encoded><![CDATA[<p>It is really easy to get some web c# (csharp) (asp.net) development within a Linux setup, since I use kubuntu which is derived from ubuntu.  All you need to do is to</p>
<p>this installs the mono module for the apache2</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #c20cb9; font-weight: bold;">aptitude</span> <span style="color: #c20cb9; font-weight: bold;">install</span> libapache2-mod-mono</pre></div></div>

<p>to enable the module to work within apache2 you have to update the mods-enabled directory and to do this, just use the</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">a2enmod mod_mono_auto</pre></div></div>

<p>which stands for apache2 (a2) enable (en) mod (module) and then the module name, you will need to restart apache2 for it to work</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">/</span>etc<span style="color: #000000; font-weight: bold;">/</span>init.d<span style="color: #000000; font-weight: bold;">/</span>apache2 restart</pre></div></div>

<p>that is it., then if you look within the /etc/apache2/mods-enabled you will notice the</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">mod_mono_auto.conf
mod_mono_auto.load</pre></div></div>

<p>of course you could symlink them yourself, but it is just easier to use the a2enmod script.</p>
<p>Within the .conf file it tells apache what extensions to &#8220;listen&#8221; to direct to the module mono instead of either php module or just a static html web page.</p>
<p>To test the setup, just create a directory within your hosting directory this is normally /var/www/ or just place a file in that base directory /var/www it is up to you, but within that file you could create a basic test file like.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008000;">&lt;%</span>@ Page Language<span style="color: #008000;">=</span><span style="color: #666666;">&quot;C#&quot;</span> <span style="color: #008000;">%&gt;</span>
<span style="color: #008000;">&lt;</span>html<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>head<span style="color: #008000;">&gt;</span>
    <span style="color: #008000;">&lt;</span>title<span style="color: #008000;">&gt;</span>Apache2 Mod mono test page<span style="color: #008000;">&lt;/</span>title<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;/</span>head<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;</span>body<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;</span>form id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;form1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>
          <span style="color: #008000;">&lt;</span>asp<span style="color: #008000;">:</span>label id<span style="color: #008000;">=</span><span style="color: #666666;">&quot;lbl1&quot;</span> runat<span style="color: #008000;">=</span><span style="color: #666666;">&quot;server&quot;</span><span style="color: #008000;">&gt;</span>Apache2 Mod mono test page<span style="color: #008000;">&lt;/</span>asp<span style="color: #008000;">:</span>label<span style="color: #008000;">&gt;</span>
        <span style="color: #008000;">&lt;/</span>form<span style="color: #008000;">&gt;</span>
  <span style="color: #008000;">&lt;/</span>body<span style="color: #008000;">&gt;</span>
<span style="color: #008000;">&lt;/</span>html<span style="color: #008000;">&gt;</span></pre></div></div>

<p>the asp:label will be the main test, since that part of the csharp library as such. you should see something like</p>
<p>Apache2 Mod mono test page</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/04/14/mono-web-development-on-linux/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ DLL Objects accessed from C#</title>
		<link>http://www.codingfriends.com/index.php/2009/07/24/c-dll-objects-accessed-from-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=c-dll-objects-accessed-from-c</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/24/c-dll-objects-accessed-from-c/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 21:41:09 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C / C++]]></category>
		<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[gcc]]></category>
		<category><![CDATA[gnu]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=181</guid>
		<description><![CDATA[Because there is a few things that c# cannot do compared to c++, e.g. cpu/memory management. And also there is probably allot of dll&#8217;s out there are still required for some events, c# is able to communicate with these files and use there functions within the c# language. To create an dll within Visual Studio [...]]]></description>
			<content:encoded><![CDATA[<p>Because there is a few things that c# cannot do compared to c++, e.g. cpu/memory management.  And also there is probably allot of dll&#8217;s out there are still required for some events, c# is able to communicate with these files and use there functions within the c# language.</p>
<p>To create an dll within Visual Studio 2005 within the language c++, if you do the following</p>
<p>1. New project -> c++ -> empty project<br />
2. Enter project name (e.g. hellocdll)<br />
3. Right click source files ( in the solution explorer) add-> new item<br />
enter an cpp filename.<br />
4, Right click on the main project heading in the solution explorer -> properties<br />
configuration properties->general inner screen project defaults -> configuration type, alter to dynamic library (.dll)<br />
5. Copy and paste code and then compile.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;stdio.h&gt;</span>
&nbsp;
<span style="color: #0000ff;">extern</span> <span style="color: #FF0000;">&quot;C&quot;</span>
<span style="color: #008000;">&#123;</span>
  __declspec<span style="color: #008000;">&#40;</span>dllexport<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">void</span> DisplayMessageFromDLL<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
              <span style="color: #0000dd;">printf</span> <span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Hi From the C DLL!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
&nbsp;
  __declspec<span style="color: #008000;">&#40;</span>dllexport<span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">int</span> DisplayValueAndReturn<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> i<span style="color: #008000;">&#41;</span>
  <span style="color: #008000;">&#123;</span>
         <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;Value %i<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, i<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
         <span style="color: #0000ff;">return</span> i<span style="color: #000040;">+</span><span style="color: #0000dd;">2</span><span style="color: #008080;">;</span>
  <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>The extern &#8220;C&#8221; means that the references within the code are going to be available externally and marco __declsepc(dllexport) is for the MS compile to inform that functions are to be available within an dll file. </p>
<p>To create an c# project to communicate with the above dll</p>
<p>1. New project -> c# -> empty project<br />
2. Enter project name (e.g. hellodlltest)<br />
3. Right click on the project name ( in the solution explorer) add-> new item<br />
select class and enter a filename<br />
4. Copy and paste the code and then compile. (you may need to change the Dllimport to the dll file name that has been created from the c dll project)</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Runtime.InteropServices</span><span style="color: #008000;">;</span>     <span style="color: #008080; font-style: italic;">// DLL support</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> hellodlltest
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">class</span> hellodllC
    <span style="color: #008000;">&#123;</span>
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;hellocdll.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">void</span> DisplayMessageFromDLL<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008000;">&#91;</span>DllImport<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;hellocdll.dll&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>
        <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #0600FF; font-weight: bold;">extern</span> <span style="color: #6666cc; font-weight: bold;">int</span> DisplayValueAndReturn<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> i<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main <span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
           <span style="color: #008000;">&#123;</span>
                  Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span> <span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;C# program 'talking' to an C DLL&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            DisplayMessageFromDLL<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>DisplayValueAndReturn<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
           Console<span style="color: #008000;">.</span><span style="color: #0000FF;">ReadLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
           <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>5. Copy the dll file from the debug directory of the c++ created dll project and place into the created bin/debug directory for this c# project</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/24/c-dll-objects-accessed-from-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Operator</title>
		<link>http://www.codingfriends.com/index.php/2009/07/24/operator/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=operator</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/24/operator/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 21:37:31 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=179</guid>
		<description><![CDATA[The standard class function are not able to utilize the standard operators within the c# language, for example the multiple, add, subtraction and divide in a mathematics example. To overcome this problem, there is a &#8216;operator&#8217; special syntax that allows for this to take place. The operator can work with any of the c# language [...]]]></description>
			<content:encoded><![CDATA[<p>The standard class function are not able to utilize the standard operators within the c# language, for example the multiple, add, subtraction and divide in a mathematics example.</p>
<p>To overcome this problem, there is a &#8216;operator&#8217; special syntax that allows for this to take place.  The operator can work with any of the c# language standard functions of which you are able to program there functional aspects within the class.  The syntax for this is</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #008000;">&lt;</span><span style="color: #0600FF; font-weight: bold;">return</span> type<span style="color: #008000;">&gt;</span> <span style="color: #0600FF; font-weight: bold;">operator</span> <span style="color: #008000;">&lt;</span><span style="color: #0600FF; font-weight: bold;">operator</span> type<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&lt;</span>parameter list of passed variables<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>for example if there was an return type of int and two integer values passed in the parameter list and using the addition operator.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">int</span> <span style="color: #0600FF; font-weight: bold;">operator</span> <span style="color: #008000;">+</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> a, <span style="color: #6666cc; font-weight: bold;">int</span> b<span style="color: #008000;">&#41;</span></pre></div></div>

<p>Below is some code that will demonstrate this further within a general code development.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> operatorBase
<span style="color: #008000;">&#123;</span>
       <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> i<span style="color: #008000;">;</span>       <span style="color: #008080; font-style: italic;">// private member of the class</span>
&nbsp;
       <span style="color: #0600FF; font-weight: bold;">public</span> operatorBase<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
       <span style="color: #008000;">&#123;</span>
              i <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>       
       <span style="color: #008000;">&#125;</span>
&nbsp;
       <span style="color: #0600FF; font-weight: bold;">public</span> operatorBase<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> init<span style="color: #008000;">&#41;</span>  
       <span style="color: #008000;">&#123;</span>
              <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">i</span> <span style="color: #008000;">=</span> init<span style="color: #008000;">;</span> 
       <span style="color: #008000;">&#125;</span>
&nbsp;
       <span style="color: #008080; font-style: italic;">// get and set the value for the private member i</span>
       <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">int</span> Value
       <span style="color: #008000;">&#123;</span>
              get <span style="color: #008000;">&#123;</span> <span style="color: #0600FF; font-weight: bold;">return</span> i<span style="color: #008000;">;</span><span style="color: #008000;">&#125;</span>
              set <span style="color: #008000;">&#123;</span> i <span style="color: #008000;">=</span> value<span style="color: #008000;">;</span><span style="color: #008000;">&#125;</span>
       <span style="color: #008000;">&#125;</span>
&nbsp;
       <span style="color: #008080; font-style: italic;">// the operator +, parameters are the two values that you want to add, can be overloaded with different values</span>
       <span style="color: #008080; font-style: italic;">// e.g. (int i2, int i3) for example.</span>
       <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> operatorBase <span style="color: #0600FF; font-weight: bold;">operator</span> <span style="color: #008000;">+</span><span style="color: #008000;">&#40;</span>operatorBase i2, operatorBase i3<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              <span style="color: #008080; font-style: italic;">// create the return;</span>
              operatorBase locali<span style="color: #008000;">=</span> <span style="color: #008000;">new</span> operatorBase<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
              locali<span style="color: #008000;">.</span><span style="color: #0000FF;">i</span> <span style="color: #008000;">=</span> i2<span style="color: #008000;">.</span><span style="color: #0000FF;">i</span> <span style="color: #008000;">+</span> i3<span style="color: #008000;">.</span><span style="color: #0000FF;">i</span><span style="color: #008000;">;</span>  have access to the internals of passed parameters
              <span style="color: #0600FF; font-weight: bold;">return</span>  locali<span style="color: #008000;">;</span>       <span style="color: #008080; font-style: italic;">// return the operatorBase class</span>
       <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> operatorTest
<span style="color: #008000;">&#123;</span>
&nbsp;
       <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              operatorBase opBase <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> operatorBase<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
              <span style="color: #008080; font-style: italic;">// set the value to 3 and also output the value;</span>
              opBase<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">;</span>
              Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>opBase<span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
              operatorBase opBase2 <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> operatorBase<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
              <span style="color: #008080; font-style: italic;">// to add to the operatorbases together, but will return an operatorBase, thus bracket the equation and use the .Value to get the value. </span>
              Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span>opBase <span style="color: #008000;">+</span> opBase2<span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
              <span style="color: #008080; font-style: italic;">// since creating two new on the fly operatorBase, then the result is an int value again.</span>
              Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> operatorBase<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">+</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">new</span> operatorBase<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Value</span> <span style="color: #008000;">=</span> <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/24/operator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yielding results</title>
		<link>http://www.codingfriends.com/index.php/2009/07/24/yielding-results/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=yielding-results</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/24/yielding-results/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 21:32:07 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=176</guid>
		<description><![CDATA[Yield is a new syntax word within the .net 2 language. This allows for the IEnumerable interface to have direct interaction instead of using the IEnumerator interface to iterate though a result set. For easier demonstration, I think, the code examples, the first code is a .net 1 version of the interface IEnumerable. using System; [...]]]></description>
			<content:encoded><![CDATA[<p>Yield is a new syntax word within the .net 2 language.  This allows for the IEnumerable interface to have direct interaction instead of using the IEnumerator interface to iterate though a result set.  For easier demonstration, I think, the code examples, the first code is a .net 1 version of the interface IEnumerable.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// the inital class is IEnumerable- means that it has to implement the GetEnumerator</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> <span style="color: #6666cc; font-weight: bold;">Class</span> <span style="color: #008000;">:</span> IEnumerable 
<span style="color: #008000;">&#123;</span>
       <span style="color: #008080; font-style: italic;">// internal class to implement the IEnumerator interface, which inturn has to do the MoveNext, Current, Reset methods.</span>
       <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">class</span> ClassEnumerator <span style="color: #008000;">:</span> IEnumerator
       <span style="color: #008000;">&#123;</span>
              <span style="color: #008080; font-style: italic;">// internal values to go through the array</span>
              <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">int</span> index <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
              <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">Class</span> P<span style="color: #008000;">;</span>
              <span style="color: #008080; font-style: italic;">// setup the internal variable</span>
              <span style="color: #0600FF; font-weight: bold;">public</span> ClassEnumerator<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">Class</span> P<span style="color: #008000;">&#41;</span> 
              <span style="color: #008000;">&#123;</span>
                     <span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">.</span><span style="color: #0000FF;">P</span> <span style="color: #008000;">=</span> P<span style="color: #008000;">;</span> 
              <span style="color: #008000;">&#125;</span>
              <span style="color: #008080; font-style: italic;">// move within the array, if any values are present.</span>
              <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">bool</span> MoveNext<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
              <span style="color: #008000;">&#123;</span>
                     index<span style="color: #008000;">++;</span>
                     <span style="color: #0600FF; font-weight: bold;">return</span> index <span style="color: #008000;">&lt;</span> P<span style="color: #008000;">.</span>_Names<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
              <span style="color: #008000;">&#125;</span>
              <span style="color: #008080; font-style: italic;">// set the internal to -1, thus restart</span>
              <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">void</span> Reset<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> 
              <span style="color: #008000;">&#123;</span> 
                     index <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
              <span style="color: #008000;">&#125;</span>
&nbsp;
              <span style="color: #008080; font-style: italic;">// get the Current object</span>
              <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">object</span> Current 
              <span style="color: #008000;">&#123;</span> 
                     get 
                     <span style="color: #008000;">&#123;</span> 
                            <span style="color: #0600FF; font-weight: bold;">return</span> P<span style="color: #008000;">.</span>_Names<span style="color: #008000;">&#91;</span>index<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span> 
                     <span style="color: #008000;">&#125;</span> 
              <span style="color: #008000;">&#125;</span>
       <span style="color: #008000;">&#125;</span>
&nbsp;
       <span style="color: #008080; font-style: italic;">// return a IEnumerator object of the above with itself passed</span>
       <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              <span style="color: #0600FF; font-weight: bold;">return</span> <span style="color: #008000;">new</span> ClassEnumerator<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">this</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
&nbsp;
       <span style="color: #008080; font-style: italic;">// the internal string array of names</span>
       <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> _Names<span style="color: #008000;">;</span>
       <span style="color: #008080; font-style: italic;">// the new class setup process, </span>
       <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">Class</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> Names<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              _Names <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span>Names<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
              <span style="color: #008080; font-style: italic;">// copy the passed parameter starting at position 0</span>
              Names<span style="color: #008000;">.</span><span style="color: #0000FF;">CopyTo</span><span style="color: #008000;">&#40;</span>_Names,<span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> ClassProgram
<span style="color: #008000;">&#123;</span>
       <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              <span style="color: #6666cc; font-weight: bold;">Class</span> ClassArr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">Class</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Student1&quot;</span>, <span style="color: #666666;">&quot;Student2&quot;</span>,<span style="color: #666666;">&quot;Studentx&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
              <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #0600FF; font-weight: bold;">in</span> ClassArr<span style="color: #008000;">&#41;</span>
                     Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>This is able to output a string of </p>
<pre class="consoleoutput">
Student1
Student2
Studentx
</pre>
<p>Since the Class class (it is a class of students), there is allot of code to iterate with the foreach function.</p>
<p>Within .net 2 there is a nice and more tighter method called yield, an yield will yield a result and be able to iterate though a result set with the IEnumerable return set.  Again, here is the source code.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> yielding
<span style="color: #008000;">&#123;</span>
       <span style="color: #008080; font-style: italic;">// same as the class as in .net 1 version.</span>
       <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> <span style="color: #6666cc; font-weight: bold;">Class</span> <span style="color: #008000;">:</span> IEnumerable
       <span style="color: #008000;">&#123;</span>
              <span style="color: #008080; font-style: italic;">// but only need to implement this one method to do the same as the pervious separate class</span>
              <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
              <span style="color: #008000;">&#123;</span>
                     <span style="color: #6666cc; font-weight: bold;">int</span> counter <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                     <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>counter<span style="color: #008000;">++</span> <span style="color: #008000;">&lt;</span> <span style="color: #008000;">&#40;</span>_Names<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                            <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> _Names<span style="color: #008000;">&#91;</span>counter<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
              <span style="color: #008000;">&#125;</span>
&nbsp;
              <span style="color: #008080; font-style: italic;">// same as the other .net 1 class internals for the private variable and the constructor</span>
              <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> _Names<span style="color: #008000;">;</span>              
              <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">Class</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> Names<span style="color: #008000;">&#41;</span>
              <span style="color: #008000;">&#123;</span>
                     _Names <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span>Names<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
                     Names<span style="color: #008000;">.</span><span style="color: #0000FF;">CopyTo</span><span style="color: #008000;">&#40;</span>_Names,<span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
              <span style="color: #008000;">&#125;</span>
       <span style="color: #008000;">&#125;</span>       
&nbsp;
       <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              <span style="color: #008080; font-style: italic;">// same as the .net 1 version</span>
              <span style="color: #6666cc; font-weight: bold;">Class</span> ClassArr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">Class</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Student1&quot;</span>, <span style="color: #666666;">&quot;Student2&quot;</span>, <span style="color: #666666;">&quot;Studentx&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
              <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #0600FF; font-weight: bold;">in</span> ClassArr<span style="color: #008000;">&#41;</span>
                     Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Far less code and also the result is exactly the same.</p>
<p>Also there are other benefits of the yield attached to an IEnumerable interface, here is some more code to demonstrate how to implement a power function to display the power result being built up.  This demonstrates how to use just a single method to return an iterate-able result.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System.Collections</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #6666cc; font-weight: bold;">class</span> yielding
<span style="color: #008000;">&#123;</span>
       <span style="color: #008080; font-style: italic;">// same as the class as in .net 1 version.</span>
       <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> <span style="color: #6666cc; font-weight: bold;">Class</span> <span style="color: #008000;">:</span> IEnumerable
       <span style="color: #008000;">&#123;</span>
              <span style="color: #008080; font-style: italic;">// but only need to implement this one method to do the same as the pervious separate class</span>
              <span style="color: #0600FF; font-weight: bold;">public</span> IEnumerator GetEnumerator<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
              <span style="color: #008000;">&#123;</span>
                     <span style="color: #6666cc; font-weight: bold;">int</span> counter <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
                     <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>counter<span style="color: #008000;">++</span> <span style="color: #008000;">&lt;</span> <span style="color: #008000;">&#40;</span>_Names<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                            <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> _Names<span style="color: #008000;">&#91;</span>counter<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
              <span style="color: #008000;">&#125;</span>
&nbsp;
              <span style="color: #008080; font-style: italic;">// same as the other .net 1 class internals for the private variable and the constructor</span>
              <span style="color: #0600FF; font-weight: bold;">private</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> _Names<span style="color: #008000;">;</span>              
              <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">Class</span><span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> Names<span style="color: #008000;">&#41;</span>
              <span style="color: #008000;">&#123;</span>
                     _Names <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span>Names<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
                     Names<span style="color: #008000;">.</span><span style="color: #0000FF;">CopyTo</span><span style="color: #008000;">&#40;</span>_Names,<span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
              <span style="color: #008000;">&#125;</span>
       <span style="color: #008000;">&#125;</span>       
&nbsp;
       <span style="color: #008080; font-style: italic;">// IEnumerable linked to yield is new in .net 2, it allows for the </span>
       <span style="color: #008080; font-style: italic;">// iteration of this method, acting like array instead of a single return.</span>
       <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> IEnumerable Power<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> number, <span style="color: #6666cc; font-weight: bold;">int</span> exp<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              <span style="color: #6666cc; font-weight: bold;">int</span> counter <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span>, result <span style="color: #008000;">=</span> <span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>       <span style="color: #008080; font-style: italic;">// setup the default values.</span>
              <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>counter<span style="color: #008000;">++</span> <span style="color: #008000;">&lt;</span> exp<span style="color: #008000;">&#41;</span>              <span style="color: #008080; font-style: italic;">// increament counter and whilst lower than the passed parameter exp</span>
              <span style="color: #008000;">&#123;</span>
                     result <span style="color: #008000;">*=</span> number<span style="color: #008000;">;</span>       <span style="color: #008080; font-style: italic;">// result = result * number;</span>
                     <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> result<span style="color: #008000;">;</span>       <span style="color: #008080; font-style: italic;">// yield the result and return</span>
              <span style="color: #008000;">&#125;</span>
       <span style="color: #008000;">&#125;</span>
&nbsp;
       <span style="color: #008080; font-style: italic;">// can also just iterate though a array of strings (in this example)</span>
       <span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">static</span> IEnumerable StReturn<span style="color: #008000;">&#40;</span><span style="color: #0600FF; font-weight: bold;">params</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> names<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              <span style="color: #6666cc; font-weight: bold;">int</span> counter <span style="color: #008000;">=</span> <span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">;</span>
              <span style="color: #0600FF; font-weight: bold;">while</span> <span style="color: #008000;">&#40;</span>counter<span style="color: #008000;">++</span> <span style="color: #008000;">&lt;</span> <span style="color: #008000;">&#40;</span>names<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span><span style="color: #008000;">-</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                     <span style="color: #0600FF; font-weight: bold;">yield</span> <span style="color: #0600FF; font-weight: bold;">return</span> names<span style="color: #008000;">&#91;</span>counter<span style="color: #008000;">&#93;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
&nbsp;
       <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              <span style="color: #008080; font-style: italic;">// can do a return of a power function as well</span>
              <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">int</span> e <span style="color: #0600FF; font-weight: bold;">in</span> Power<span style="color: #008000;">&#40;</span><span style="color: #FF0000;">4</span>,<span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                     Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>e<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
              <span style="color: #008080; font-style: italic;">// you can just iterate a string </span>
              <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> st <span style="color: #008000;">=</span> <span style="color: #008000;">&#123;</span><span style="color: #666666;">&quot;student1&quot;</span>,<span style="color: #666666;">&quot;student2&quot;</span>,<span style="color: #666666;">&quot;studentx&quot;</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
              <span style="color: #0600FF; font-weight: bold;">foreach</span><span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #0600FF; font-weight: bold;">in</span> StReturn<span style="color: #008000;">&#40;</span>st<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
                     Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
              <span style="color: #008080; font-style: italic;">// same as the .net 1 version</span>
              <span style="color: #6666cc; font-weight: bold;">Class</span> ClassArr <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">Class</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Student1&quot;</span>, <span style="color: #666666;">&quot;Student2&quot;</span>, <span style="color: #666666;">&quot;Studentx&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
              <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> s <span style="color: #0600FF; font-weight: bold;">in</span> ClassArr<span style="color: #008000;">&#41;</span>
                     Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>s<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>I have also include the code from above to demonstrate the similar usage for iterate though a string array.</p>
<p>Output would be</p>
<pre class="consoleoutput">
4
16
64
student1
student2
studentx
Student1
Student2
Studentx
</pre>
<p>You will need to have .net 2 installed or <a title="mono"  href="http://www.mono-project.com/Main_Page" target="_blank">mono</a> (use the gmcs part of mono to use the .net 2 functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/24/yielding-results/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generics</title>
		<link>http://www.codingfriends.com/index.php/2009/07/24/generics/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=generics</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/24/generics/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 21:27:35 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=174</guid>
		<description><![CDATA[A Generic is a way to declassify the type of variable used, e.g. if there was a function to add up two integer values, but you had two double values, then the int function would not function correctly, so there would be a need to function overload the function to allow for the double increment [...]]]></description>
			<content:encoded><![CDATA[<p>A Generic is a way to declassify the type of variable used, e.g. if there was a function to add up two integer values, but you had two double values, then the int function would not function correctly, so there would be a need to function overload the function to allow for the double increment as well.  But just envisage that the same function would be required for characters, floats, personally created ones etc, then there would be more functions per type of variable than the actual work done.  </p>
<p>A generic class negates this by allowing any class type passed to the function, as long as the type has the standard increment process defined, it will work with any type.</p>
<p>This will demonstrate the basics of the generics.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// create a class with a generic type of T (e.g. pass in the type of your choice</span>
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> GenTest<span style="color: #008000;">&lt;</span>T<span style="color: #008000;">&gt;;</span>
<span style="color: #008000;">&#123;</span>
       <span style="color: #008080; font-style: italic;">// the private value</span>
       <span style="color: #0600FF; font-weight: bold;">private</span> T genericValue<span style="color: #008000;">;</span>
&nbsp;
       <span style="color: #008080; font-style: italic;">// default constructor for the class (same name as the class)</span>
       <span style="color: #008080; font-style: italic;">// ~ is the deconstructor</span>
       <span style="color: #0600FF; font-weight: bold;">public</span> GenTest<span style="color: #008000;">&#40;</span>T generic<span style="color: #008000;">&#41;</span> 
       <span style="color: #008000;">&#123;</span>
              genericValue <span style="color: #008000;">=</span> generic<span style="color: #008000;">;</span>       <span style="color: #008080; font-style: italic;">// set the internal T = passed value</span>
       <span style="color: #008000;">&#125;</span>
&nbsp;
       <span style="color: #008080; font-style: italic;">// return the value of the internal value</span>
       <span style="color: #0600FF; font-weight: bold;">public</span> T ReturnValue<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              <span style="color: #0600FF; font-weight: bold;">return</span> genericValue<span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> MainProgram
<span style="color: #008000;">&#123;</span>
       <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
       <span style="color: #008000;">&#123;</span>
              <span style="color: #008080; font-style: italic;">// create a integer type class, with the default value of 2</span>
              GenTest<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span> genInt <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> GenTest<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">int</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
              Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Int generic = &quot;</span> <span style="color: #008000;">+</span> genInt<span style="color: #008000;">.</span><span style="color: #0000FF;">ReturnValue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
              <span style="color: #008080; font-style: italic;">// same class create a string with the default value of &quot;cool&quot;</span>
              GenTest<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span> genString <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> GenTest<span style="color: #008000;">&lt;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;cool&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
              Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;String generic = &quot;</span> <span style="color: #008000;">+</span> genString<span style="color: #008000;">.</span><span style="color: #0000FF;">ReturnValue</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
       <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>If you save the code and run with .net 2 or greater (<a title=".NET Framework"  href="http://msdn.microsoft.com/netframework/" target="_blank">.NET Framework</a>) ( since this was part of .net 2 and not with .net 1). The output will be</p>
<pre class="consoleoutput">
Int generic = 2
String generic = cool
</pre>
<p>As you can see the same class is able to use both integer and strings as the default variable type.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/24/generics/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Collections &#8211; ArrayList</title>
		<link>http://www.codingfriends.com/index.php/2009/07/24/collections-arraylist/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=collections-arraylist</link>
		<comments>http://www.codingfriends.com/index.php/2009/07/24/collections-arraylist/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 21:22:32 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[C Sharp (c#)]]></category>
		<category><![CDATA[c sharp]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=172</guid>
		<description><![CDATA[Collections &#8211; ArrayList, the ArrayList is part of the Collections namespace. The ArrayList acts as the same as the array (Array Example) apart from it allows other functional aspects to access the list of array. Shall comment more on the different aspects in later tutorials, but basically there many different parts to a class that [...]]]></description>
			<content:encoded><![CDATA[<p>Collections &#8211; ArrayList, the ArrayList is part of the Collections namespace.  The ArrayList acts as the same as the array (<a title="Array example"  href="http:// http://www.codingfriends.com/index.php/2009/07/24/arrays/" target="_blank">Array Example</a>) apart from it allows other functional aspects to access the list of array.</p>
<p>Shall comment more on the different aspects in later tutorials, but basically there many different parts to a class that allow for better functionality e.g<br />
1.       Interfaces =  base of a class for strict implementation)<br />
2.       IEnumerable (which is a interface) = means that you implement GetEnumerator aspects of a class, e.g. for the foreach command)<br />
3.       Generics (which is part of .net 2) = Similar to templates (<a title="Generics"  href="http:// http://www.codingfriends.com/index.php/2009/07/24/templates/" target="_blank">Generics</a>) in c++ and also generics from Java).<br />
4.       Etc. shall comment more on these.</p>
<p>This is the code</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF; font-weight: bold;">namespace</span> ArrayListTest
<span style="color: #008000;">&#123;</span>
    <span style="color: #6666cc; font-weight: bold;">class</span> Program
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">static</span> <span style="color: #6666cc; font-weight: bold;">void</span> Main<span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> args<span style="color: #008000;">&#41;</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">ArrayList</span> arrayList <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #000000;">System.<span style="color: #0000FF;">Collections</span></span><span style="color: #008000;">.</span><span style="color: #0000FF;">ArrayList</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            arrayList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            arrayList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            arrayList<span style="color: #008000;">.</span><span style="color: #0000FF;">Add</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;hi there&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
            <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">object</span> obj <span style="color: #0600FF; font-weight: bold;">in</span> arrayList<span style="color: #008000;">&#41;</span>
                Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>obj<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>If you save as arraylisttest.cs, and then run, the output will be</p>
<pre class="consoleoutput">
2
3
hi there
</pre>
<p>It basically allows you to add in any object into the ArrayList, and since objects have the ToString() function, then it is able to print out the object, of course custom made objects will either display there type or the ToString() will have to be implemented.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2009/07/24/collections-arraylist/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

