Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Tuesday, May 29, 2007

Introduction to XML

For those of you just starting out with XML, I highly recommend that you check out the video tutorial(s) below:

VB

Working with XML & VB.net

C#

Working with XML & C#

Thursday, May 24, 2007

Reading Text File Into Combo Box

In the previous blog (Reading Text File Using StreamReader Class), I demonstrated the use of StreadReader class to read text file. The System.IO.File class also provide us with a couple of ways to read text file as well.

We will use it's capability in this blog to read the data from text file and load it directly into a combo box.

Code sample:

string[] theStates;
theStates=System.IO.File.ReadAllLines(txtFilename.Text);
cboStates.Items.AddRange(theStates);


A sample application is available here.

Load text file into combobox

Reading Text File Using StreamReader Class

The namespace System.IO has a class called StreamReader which can be used for reading text files.

The StreamReader class provides us with a couple of methods and a property as listed below:

Close: Closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.

Peek: Returns the next available character but does not consume it.

Read: Reads the next character or next set of characters from the input stream.

ReadLine: Reads a line of characters from the current stream and returns the data as a string.

ReadToEnd: Reads the stream from the current position to the end of the stream.

To read a text file, we need to open it using System.IO.File.OpenText method which returns a StreamReader object. Once we obtain the StreamReader object, just call the appropriate method to pull the data.

C#

System.IO.StreamReader oSR;
oSR = System.IO.File.OpenText(sFilename);
txtOutput.Text = oSR.ReadToEnd();


VB

Dim oSR As System.IO.StreamReader
oSR = System.IO.File.OpenText(sFileName)
txtOutput.Text = oSR.ReadToEnd()


For further reading, please visit the this link.

Sample applications can be downloaded here: VB.net or C#

Sunday, May 20, 2007

Short Circuit Evaluation

What is Short Circuit? In the simplest terms, Short Circuit is a control structure put in place to allow the control of the program to be passed onto a different set of instructions while minimizing the evaluation of the code. It operates on booleans or boolean expressions in which the second boolean or boolean expression is only evaluated if the first one does not suffice to determine the value of the entire expression.

Consider the following example:

bool hasMoney = false;
double totalMoney = 0;
if (hasMoney && totalMoney > 5)
{
MessageBox.Show("Buy burger.");
}

If we have no money (hasMoney=false) then there is no need to check and see if the amount of money we have is greater than 5 bucks so the boolean expression on the right hand side of boolean operator "&&" will not be evaluated in this case.

You can really appreciate short circuiting in the following example:

bool hasMoney = false;
double totalMoney = 0;
if (hasMoney && checkTotalMoney())
{
MessageBox.Show("Buy burger.");
}


As you can see, this is just a simple example to demonstrate how short circuit evaluation works but what if in the method checkTotalMoney() you have some lengthy code that goes on to evaluate if you have enough money to pay for the burger and the taxes as well?

Things just get a little bit more complicated and you can see all of that work is skipped if the boolean expression on the left of the operation becomes false. This is important to consider because if you have checkTotalMoney() on the left, the method will be evaluated to determine its value before it can move on to the second expression on the right hand side.

Knowing how short circuit evaluation works and carefully design expressions that use conditional logical operators you can greatly improve your code and boost the performance of your applications by avoiding unnecessary work. You can read more about short-circuit evaluation here.

Friday, May 18, 2007

Converting from Decimal to binary and back

This project was written in C# to demonstrate how to convert data from Decimal to Binary and from Binary back to Decimal number.

Simple Binary Converter

There are a number of ways to convert Decimal to Binary. Here, I used a small function to basically loop through and use division by 2 to convert the decimal number to binary format. Here's the code for the function:

private static string Dec2Bin(Int64 nDecimal)
{
Int64 nBinaryDigit;
string sBinaryNumber = "";
while (nDecimal > 0)
{
nBinaryDigit = nDecimal % 2;
sBinaryNumber = nBinaryDigit + sBinaryNumber;
nDecimal = nDecimal / 2;
}
return sBinaryNumber;
}


To convert Binary number back to Decimal, we can use the built-in function called Convert as seen in the code snippet below:

txtToDecimal.Text = Convert.ToInt64(txtFromBinary.Text, 2).ToString();

You can download the C# project sample here.

Wednesday, May 16, 2007

Using Master Pages in ASP.net Web Applications

Master page helps you to create a consistent look and feel to your site while minimizing the amount of work you have to do. With the introduction ASP.net 2.0 Master Pages, you no longer have to use server side includes to keep the site consistent among different pages. To be able to use and apply master page to your site you first need to create a master page:

1. Right click on the Project
2. Click Add New Item
3. Change the filename as appropriate then click OK to accept
4. Design the layout of the page as you wish and save it

Note that the design in the master page will be applied to all pages that use it except for the content part.

Now that we have a master page, we can start using it. Let's create a web page that would use the master page.

1. Right click on the Project
2. Click Add New Item
3. Select Web Form
4. Check the box called Select Master Page
5. Select the master page you just created and hit OK.

At this point, the code window will open up for you to edit. The new web page doesn't look like a typical web form but instead it will look something like this:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
your code goes here
your code goes here
</asp:Content>

When you run the application and/or browsing to the web form, IIS will merge both the master page and the web form to produce the finale code and send it to the browser.

Now that you know how to use master page, you might want to take a look at this blog to learn how to dynamically change master page at run time

Dynamically changing master page on your website

If you notice, you will see that a lot of blog sites out there allow you to create and use different site template. With a click of a button, you can completely change the look and feel of the entire site; including the theme, layout, navigation...

While this can certainly be done using classic ASP, ASP.net master pages have made it extremely easy to set this up. All you have to do is define a couple of master pages before and change MasterPageFile property at run time.

Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Page.MasterPageFile = "NewMasterPage.master"
End Sub

In this code snippet, I just assign a new master page to the MasterPageFile property of the Page object during the preinit stage and the site gets a new look.

If you are new to master pages, please have a look at this blog: Using Master Pages in ASP.net Applications.