<?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>.Net Bits and Pieces &#187; C#</title>
	<atom:link href="http://www.dotnetbitsandpieces.com/?feed=rss2&#038;tag=csharp" rel="self" type="application/rss+xml" />
	<link>http://www.dotnetbitsandpieces.com</link>
	<description>What ever I find interesting to tell about .Net</description>
	<lastBuildDate>Sat, 06 Apr 2013 21:31:17 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>Document Generation with OOXML Part 2</title>
		<link>http://www.dotnetbitsandpieces.com/?p=83</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=83#comments</comments>
		<pubDate>Thu, 04 Apr 2013 20:47:49 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Document Generation]]></category>
		<category><![CDATA[OOMXL]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=83</guid>
		<description><![CDATA[When creating a document from Word templates are used when creating documents. These can be default templates or templates created by users. It is their for essential that these templates can be used when generating OOXML documents. With the Document Generator library templates can be loaded when a new document is created. //Load a template [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small">When creating a document from Word templates are used when creating documents. These can be default templates or templates created by users. It is their for essential that these templates can be used when generating OOXML documents. With the Document Generator library templates can be loaded when a new document is created.</span></p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Load a template from file. document is the name of the new document.</span><br />
<span style="color: #008080; font-style: italic;">//template is the name and full path of the template file</span><br />
<span style="color: #008080; font-style: italic;">//true indicates that the document can override documents with the same name.</span><br />
var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>document, template, <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Load a template from a stream</span><br />
<span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var stream <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> FileStream<span style="color: #008000;">&#40;</span>templateFile, FileMode<span style="color: #008000;">.</span><span style="color: #0000FF;">Open</span>, FileAccess<span style="color: #008000;">.</span><span style="color: #0000FF;">Read</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
<span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0600FF; font-weight: bold;">using</span> <span style="color: #008000;">&#40;</span>var reader <span style="color: #008000;">=</span> <a href="http://www.google.com/search?q=new+msdn.microsoft.com"><span style="color: #008000;">new</span></a> StreamReader<span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #008080; font-style: italic;">//stream is the stream of the template.</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; var document <span style="color: #008000;">=</span> TextDocument<span style="color: #008000;">.</span><span style="color: #0000FF;">Create</span><span style="color: #008000;">&#40;</span>stream<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
&nbsp; &nbsp; <span style="color: #008000;">&#125;</span><br />
<span style="color: #008000;">&#125;</span></div></div>
<p><span style="font-size: small">Once the template is loaded the styles defined in the template can be used. As long as the styles are the default for the header and the paragraphs only the level needs to be defined.</span></p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//This adds a new paragraph with the paragraph text and heading text.</span><br />
<span style="color: #008080; font-style: italic;">//The styles used are Heading2 and Normal2. When the styles cannot be found then</span><br />
<span style="color: #008080; font-style: italic;">//Heading1 and normal will be used as styles.</span><br />
var paragraph <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Paragraph Text&quot;</span>, <span style="color: #666666;">&quot;Heading Text&quot;</span>, <span style="color: #FF0000;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p><span style="font-size: small">Templates can also define custom styles. When the name of the style in the template is known the style can be used with the Document Generator library.<br />
The style can be applied to paragraphs and headers</p>
<div class="codecolorer-container csharp geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="csharp codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #008080; font-style: italic;">//Creates a paragraph and assigns it the MyParagraphStyle.</span><br />
var paragraph <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;This is the paragraph&quot;</span>, <span style="color: #666666;">&quot;MyParagraphStyle&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span><br />
<br />
<span style="color: #008080; font-style: italic;">//Creates a paragraph and assigns it the MyParagraphStyle.</span><br />
<span style="color: #008080; font-style: italic;">//and gives it a header with the style MyHeaderStyle</span><br />
var paragraph <span style="color: #008000;">=</span> document<span style="color: #008000;">.</span><span style="color: #0000FF;">AddParagraph</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;This is the paragraph&quot;</span>, <span style="color: #666666;">&quot;Header for Paragraph&quot;</span>, <span style="color: #666666;">&quot;MyParagraphStyle&quot;</span>, <span style="color: #666666;">&quot;MyHeaderStyle&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></div></div>
<p><span style="font-size: small">In this post I showed you how you can use a template when creating documents and how to use custom styles in the template.<br />
In the next part I will show how you can replace content in documents using Content Controls.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=83</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Enum.HasFlag() versus Bitwise comparison</title>
		<link>http://www.dotnetbitsandpieces.com/?p=11</link>
		<comments>http://www.dotnetbitsandpieces.com/?p=11#comments</comments>
		<pubDate>Mon, 14 Jan 2013 21:15:31 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[.Net]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[Enum]]></category>

		<guid isPermaLink="false">http://www.dotnetbitsandpieces.com/?p=11</guid>
		<description><![CDATA[Call me a bit slow, but I recently discovered that Microsoft has implemented an HasFlag method on the Enum class since .Net 4. The method is meant as replacement for the bitwise comparison between values of an enumeration that has the FlagAttribute. For more information about the the FlagAttribute and bitwise comparison you can have [&#8230;]]]></description>
				<content:encoded><![CDATA[<p><span style="font-size: small;">Call me a bit slow, but I recently discovered that Microsoft has implemented an HasFlag method on the Enum class since .Net 4. The method is meant as replacement for the bitwise comparison between values of an enumeration that has the FlagAttribute. For more information about the the FlagAttribute and bitwise comparison you can have a look at </span><a href="http://geekswithblogs.net/BlackRabbitCoder/archive/2010/07/22/c-fundamentals-combining-enum-values-with-bit-flags.aspx" target="_blank"><span style="font-size: small;">this blog</span></a><span style="font-size: small;">. </span></p>
<p><span style="font-size: small;">For a project I am working on I am using the bitwise comparison extensively, so using the HasFlag method would make the code much more readable. Before using it I wanted to get some more information about how the HasFlag method works and I found this </span><a href="http://www.codeproject.com/Tips/441086/NETs-Enum-HasFlag-and-performance-costs" target="_blank"><span style="font-size: small;">article</span></a><span style="font-size: small;">. It shows the inner workings of the method, but also mentions that there is a performance cost to pay. However it does not mention what the cost is.</span></p>
<p><span style="font-size: small;">I therefor did my own test to see what the performance overhead would be and if it was worth to change.<span id="more-11"></span></span></p>
<p><span style="font-size: small;">The first part was to create an enumeration</span></p>
<pre>

<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public enum EnumValues<br />
{<br />
&nbsp; Value1 = 1,<br />
&nbsp; Value2 = 2,<br />
&nbsp; ...,<br />
&nbsp; Value16 = 32768<br />
}</div></div>

</pre>
<p><span style="font-size: small;">Next I needed a class to evaluate the different comparison methods</span></p>
<pre>

<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">public class EnumsClass<br />
{<br />
&nbsp; public bool CheckEnum(EnumValues values)<br />
&nbsp; {<br />
&nbsp; &nbsp; bool isTrue = false;<br />
&nbsp; &nbsp; if ((values &amp;amp; EnumValues.Value8) == EnumValues.Value8) isTrue = true;<br />
&nbsp; &nbsp; return isTrue;<br />
&nbsp; }<br />
<br />
&nbsp; public bool CheckEnumWithFlagMethod(EnumValues values)<br />
&nbsp; {<br />
&nbsp; &nbsp; bool isTrue = false;<br />
&nbsp; &nbsp; if (values.HasFlag(EnumValues.Value8)) isTrue = true;<br />
&nbsp; &nbsp; return isTrue;<br />
&nbsp; }<br />
}</div></div>

</pre>
<p><span style="font-size: small;">The last part I needed was a class to call these two methods and determine how long each of the methods would take to run. I decided to use the unit test in Visual Studio 2012 as this also shows how long each test method takes.<br />
</span></p>
<pre>

<div class="codecolorer-container text geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">[TestClass]<br />
public class EnumTest<br />
{<br />
&nbsp; private EnumsClass _enumsClass;<br />
<br />
&nbsp; [TestInitialize]<br />
&nbsp; public void Initialize()<br />
&nbsp; {<br />
&nbsp; &nbsp; _enumsClass = new EnumsClass();<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForFalseValueWithFlag()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsFalse(_enumsClass.CheckEnumWithFlagMethod(EnumValues.Value1 | EnumValues.Value10));<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForFalseValue()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsFalse(_enumsClass.CheckEnum(EnumValues.Value1 | EnumValues.Value10));<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForTrueValue()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsTrue(_enumsClass.CheckEnum(EnumValues.Value8 | EnumValues.Value10));<br />
&nbsp; }<br />
<br />
&nbsp; [TestMethod]<br />
&nbsp; public void TestForTrueValueWithFlag()<br />
&nbsp; {<br />
&nbsp; &nbsp; Assert.IsTrue(_enumsClass.CheckEnumWithFlagMethod(EnumValues.Value8 | EnumValues.Value10));<br />
&nbsp; }<br />
}</div></div>

</pre>
<p><span style="font-size: small;">To make sure that there was no difference between a true and false result I used both methods in the EnumsClass to return both <em>true</em> and <em>false. </em>I also repeated the different calls multiple times in a foreach loop. The results are in the below.</span></p>
<table width="400" border="0" cellspacing="0" cellpadding="2">
<tbody>
<tr>
<td valign="top" width="200">Test</td>
<td valign="top" width="200">1x</td>
<td valign="top" width="200">10000x</td>
<td valign="top" width="200">100000x</td>
<td valign="top" width="200">1000000x</td>
<td valign="top" width="200">10000000x</td>
</tr>
<tr>
<td valign="top" width="200">TestForTrueValue</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">7 ms</td>
<td valign="top" width="200">79 ms</td>
</tr>
<tr>
<td valign="top" width="200">TestForTrueValueWithFlag</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">4 ms</td>
<td valign="top" width="200">45 ms</td>
<td valign="top" width="200">453 ms</td>
</tr>
<tr>
<td valign="top" width="200">TestForFalseValue</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">6 ms</td>
<td valign="top" width="200">78 ms</td>
</tr>
<tr>
<td valign="top" width="200">TestForFalseValueWithFlag</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">&lt; 1 ms</td>
<td valign="top" width="200">5 ms</td>
<td valign="top" width="200">46 ms</td>
<td valign="top" width="200">477 ms</td>
</tr>
</tbody>
</table>
<p><span style="font-size: small;">So, there is an performance cost to using the HasFlag() method. It seems that the HasFlag() method is about 6 times as slow as the good old bitwise comparison. However you will notice the difference once you need to execute the call over 100000 times.</span><br />
<span style="font-size: small;">It is up to the programmer to see if the performance cost is worth the readability. I will stick to the bitwise comparison for now.</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.dotnetbitsandpieces.com/?feed=rss2&#038;p=11</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
