<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>DevChix » .NET</title>
	
	<link>http://www.devchix.com</link>
	<description>Boys can't have all the fun</description>
	<pubDate>Sat, 04 Jul 2009 06:18:34 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.7.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" href="http://feeds.feedburner.com/devchix/PVpl" type="application/rss+xml" /><feedburner:browserFriendly></feedburner:browserFriendly><item>
		<title>Repeating a Try-Catch block</title>
		<link>http://www.devchix.com/2007/12/03/repeating-a-try-catch-block/</link>
		<comments>http://www.devchix.com/2007/12/03/repeating-a-try-catch-block/#comments</comments>
		<pubDate>Mon, 03 Dec 2007 19:22:54 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<category><![CDATA[Tips and Tricks]]></category>

		<guid isPermaLink="false">http://www.devchix.com/2007/12/03/repeating-a-try-catch-block/</guid>
		<description><![CDATA[Here&#8217;s a tip for beginners. You know how a try/catch block works: place some code inside a try, and if it fails, catch it with the catch block. But what do you do if you want to try an operation a set number of times, and then throw an exception if it&#8217;s still failing? There&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>Here&#8217;s a tip for beginners. You know how a try/catch block works: place some code inside a try, and if it fails, catch it with the catch block. But what do you do if you want to try an operation a set number of times, and <strong>then </strong>throw an exception if it&#8217;s still failing? There&#8217;s no such animal that is native in C#, but you can quickly implement one yourself. With a counter and a ref parameter, this can be done quite simply. This brief example will show you how.</p>
<p>Suppose we want to do some FooBar operation, but we know it could fail, due to some external circumstance, but we also know could work after the initial attempt or two. Let&#8217;s write the code to do 3 tries, and only then throw the error:</p>
<pre>
int MaxTries = 3;   //this is the limiter value - set this to the number of tries you want to execute
int NumTries = 0;   //this is our counter that keeps track of how many tries we've executed

//Perform the FooBar operation(we retry 3 times and if we don't succeed, the exception will be caught and handled)
while (NumTries &lt; MaxTries)
               throw (err);           

private void FooBar(int foo, int bar, ref int numTries)
        {
            try
            {
                //count the try
                numTries++;

                //set up &amp; and perform your operation
                Foo(bar);
                Bar(foo);
                DoFinalOp();

                //if we got here, there was no error so set counter to kick us out of loop
                numTries = MaxTries;
            }
            catch (Exception err)
            {
                //only throw error if we've tried enough times
                if (numTries &gt;= MaxTries)
                    throw (err);
            }

        }
</pre>
<p>Note that our FooBar method takes a ref parameter of numTries. When we change this value in the method, and the method returns, the counter will have been changed, for re-testing in the while loop clause.</p>
<p>Let&#8217;s walk through this and see how it works:</p>
<ul>
<li>After setting NumTries and MaxTries (to 0 and 3), we hit the while loop. This first time, NumTries is definitely less than MaxTries, so we execute FooBar().</li>
<li>When we enter FooBar(), the first thing we do is increment <em>tries</em>, to count the attempt. Since this is a ref parameter, changing this local variable tries also ends up changing the global variable <em>NumTries</em>.</li>
<li>Then we start executing whatever we have to do in FooBar(). If an error is encountered at this point, we&#8217;ll fall to the catch block.</li>
<li>In the catch block, we test whether or not to throw an error, based on the value of <em>tries</em>. Since at this point, t<em>ries = 1</em>, we don&#8217;t throw the error, FooBar() finishes executing and we return to the while loop.</li>
<li>Back in the while loop, we retest <em>NumTries </em>to see if it&#8217;s still less than <em>MaxTries</em>. Since we incremented it in the method, <em>NumTries </em>is now equal to 1, but this is still a true condition, so we call FooBar() again.
<li>Once again inside FooBar(), we increment <em>tries </em>by 1 and try our operation. Suppose this time, it succeeded. In that case, we will reach the line: <em>tries = MaxTries</em> and then exit the method.</li>
<li>Now, back in the while loop, the test (NumTries &amp;lt MaxTries) is false, so we don&#8217;t execute FooBar() again, and we&#8217;re done.
</ul>
<p>Voila! We&#8217;ve implemented try/catch/repeat functionality, allowing us multiple tries before throwing an error.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devchix.com/2007/12/03/repeating-a-try-catch-block/feed/</wfw:commentRss>
		</item>
		<item>
		<title>C#.NET Data Binding Basics</title>
		<link>http://www.devchix.com/2007/10/17/cnet-data-binding-basics/</link>
		<comments>http://www.devchix.com/2007/10/17/cnet-data-binding-basics/#comments</comments>
		<pubDate>Wed, 17 Oct 2007 17:45:10 +0000</pubDate>
		<dc:creator>Alex</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[C#]]></category>

		<guid isPermaLink="false">http://www.devchix.com/2007/10/17/cnet-data-binding-basics/</guid>
		<description><![CDATA[Binding data from windows controls to objects isÃ‚Â easy in C#.NET.Ã‚Â  Here is a basic introduction:Ã‚Â 
Step one:
Create a class named Employee in C# and add a getÃ‚Â property for each field you want to bind to a windows control.
public string getLastName {
Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  get { return mStrLastName; }
}Ã‚Â 
Add a set property for any fields that you want two-way [...]]]></description>
			<content:encoded><![CDATA[<p>Binding data from windows controls to objects isÃ‚Â easy in C#.NET.Ã‚Â  Here is a basic introduction:<strong>Ã‚Â </strong></p>
<p><strong>Step one:</strong></p>
<p>Create a class named Employee in C# and add a getÃ‚Â property for each field you want to bind to a windows control.</p>
<p>public string getLastName {</p>
<p>Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  get { return mStrLastName; }</p>
<p>}Ã‚Â </p>
<p>Add a set property for any fields that you want two-way binding to occur. For example:</p>
<p>public string firstName {</p>
<p>Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  get { return mStrFirstName; }</p>
<p>Ã‚Â Ã‚Â Ã‚Â Ã‚Â Ã‚Â  set { mStrFirstName = value; }</p>
<p>}Ã‚Â </p>
<p><strong>Step two:</strong></p>
<p>Add a form to your project and throw someÃ‚Â controls on the form.Ã‚Â  For this example, add aÃ‚Â list box and a textbox. Also, create an array of objects that you want to bindÃ‚Â to this list box.</p>
<p>arrayOfEmployees = new ArrayList();</p>
<p>arrayOfEmpoyees.Add(new Employee(&#8221;Doe&#8221;,&#8221;John&#8221;,&#8221;SoftwareEngineer&#8221;,&#8221;ServerTeam&#8221;));</p>
<p>arrayOfEmployees.Add(new Employee(&#8221;Smith&#8221;,&#8221;Jane&#8221;,&#8221;SoftwareEngieer&#8221;,&#8221;ClientTeam&#8221;));</p>
<p><strong>Step three:</strong></p>
<p>Bind the data from the object to the list box and textbox</p>
<p>listBoxLastName.DataSource = arrayOfEmployees;</p>
<p>listBoxLastName.DisplayMember = &#8220;getLastName&#8221;;</p>
<p>txtBoxForFirstName.DataBindings.Add(&#8221;Text&#8221;, arrayOfEmployees, &#8220;getFirstName&#8221;);</p>
<p><strong>Step four:</strong></p>
<p>Run the project and notice that the data is bound to the list box and the textbox.Ã‚Â </p>
<p>First, notice that the textboxÃ‚Â is an example of two-way binding.Ã‚Â  If a user changes the values in the textboxÃ‚Â then the object is updated with a new first name.Ã‚Â  To change this to one-way binding, just remove the set property from the object bound to the textbox.</p>
<p>Secondly, simple binding is done to controls like textboxes that only have one possible value.Ã‚Â  Complex binding is done to controls like list boxes, that have multiple values to display at once.Ã‚Â </p>
<p>This example shows both simple and complex binding as well as one-way and two-way binding. More advanced data binding topics in C#.Net Visual Studio 2005Ã‚Â would cover the DataGridView class.</p>
<p>Cheers,</p>
<p>alex</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devchix.com/2007/10/17/cnet-data-binding-basics/feed/</wfw:commentRss>
		</item>
		<item>
		<title>ASP.NET - ‘PASSING PARAMETERS’ IN BUTTON CLICK-HANDLER</title>
		<link>http://www.devchix.com/2007/08/10/aspnet-passing-parameters-in-button-click-handler/</link>
		<comments>http://www.devchix.com/2007/08/10/aspnet-passing-parameters-in-button-click-handler/#comments</comments>
		<pubDate>Fri, 10 Aug 2007 20:46:04 +0000</pubDate>
		<dc:creator>Kris</dc:creator>
		
		<category><![CDATA[.NET]]></category>

		<category><![CDATA[ASP.NET]]></category>

		<category><![CDATA[Frameworks]]></category>

		<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://www.devchix.com/2007/08/10/aspnet-passing-parameters-in-button-click-handler/</guid>
		<description><![CDATA[A recent posting to a technical mailing list I&#8217;m on asked how to pass a parameter to a button click-event handler. The programmer wanted to do the equivalent of this:
&#60;asp:button ID="btnSubmit" runat="server" OnClick="Btn_Onclick(PARAM)" /&#62;
But, she knew this didn&#8217;t work, because the default signature of event handlers in ASP.NET is this:

void myBtn_OnClick(Object sender, EventArgs e)
So, she [...]]]></description>
			<content:encoded><![CDATA[<p>A recent posting to a technical mailing list I&#8217;m on asked how to pass a parameter to a button click-event handler. The programmer wanted to do the equivalent of this:</p>
<pre>&lt;asp:button ID="btnSubmit" runat="server" OnClick="Btn_Onclick(PARAM)" /&gt;</pre>
<p>But, she knew this didn&#8217;t work, because the default signature of event handlers in ASP.NET is this:</p>
<pre>
void myBtn_OnClick(Object sender, EventArgs e)</pre>
<p>So, she was stumped on how to pass some sort of argument to the event handler.</p>
<p>Now, there a certainly some kludges that would work, such as having a hidden form field which contains the &#8216;parameter&#8217; value, and having the event-handler get the value that way. But surely, there&#8217;s a better way? Well, yes, of course there is! :)</p>
<p>This better way is to use the &#8220;CommandArgument&#8221; property on the button control. This property is directly accessable in the event handler. The added bonus is that you can write a single even-handler for all of your button clicks, and use the associated &#8220;CommandName&#8221; property to know which button fired. </p>
<p>So, now, in your aspx page, your button object will be declared thusly:</p>
<pre>&lt;asp:Button ID="test" runat="server" CommandArgument="MyVal" CommandName="ThisBtnClick" OnClick="MyBtnHandler" /&gt;</pre>
<p>Then, in the OnClick handler, you can cast the sender object to the correct type, and get the values of<br />
 CommandName &amp; CommandArgument.  (You could make this even more generic and handle the OnClick event for different senders, but we&#8217;ll leave that for a later post! :)</p>
<pre>
void MyBtnHandler(Object sender, EventArgs e)
{
	 Button btn = (Button)sender;
          switch (btn.CommandName)
          {
                case "ThisBtnClick":
                    DoWhatever(btn.CommandArgument.ToString());
                    break;
                case "ThatBtnClick":
                    DoSomethingElse(btn.CommandArgument.ToString());
                    break;
           }
}
</pre>
<p>You can see that the switch statement uses the CommandName property to determine which button was clicked, and then can use the CommandArgument property passed in by the button click event.</p>
<p>Voila! You&#8217;ve &#8220;passed a parameter&#8221; to a button click-event handler!</p>
<p>[UPDATE] Thanks to Jim for pointing out some confusion in my sample code. I hope that this version is clearer.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.devchix.com/2007/08/10/aspnet-passing-parameters-in-button-click-handler/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
