Home Page

Click here to download the sample source code for this Data types lab exercise 

Go back to the Design view of your Default.aspx web page and add another Button and TextBox control on the next line.  Change the Text property of the new Button to “Show a floating point number”.  Double click that button to create the Button3_Click method.  Put the following code in the new method:

 

float f = 55.12f;
TextBox3.Text = f.ToString();

Here we have declared a floating point (or decimal) number.  Notice that I had to put a suffix of “f” following the number.  This is because by default, the compiler will assume that any decimal number is of type “double” which is a larger number.  Click the play button to run the project.  You can also run the project by clicking F5 which is a shortcut key for running the application.

Click on the new button when the web browser comes up and watch as it populates the TextBox control with the decimal number value.

Now I want you to go back to the Design view and drag another button onto the web page.  Change the Text to “Show a boolean”.  Double click on the new button to create the event method and inside the method put:

 

bool tedsbool = false;
TextBox4.Text = tedsbool.ToString();

Here we have declared a boolean type which only allows two different values “true” or “false”.  Also notice that we are repeatedly using a special method that a lot of .NET built in data types have which is the “ToString” method.  The ToString method converts the data type to a string or text type, so that it may be shown on the screen.

In C#, we call methods using the open and closing parentheses as in the ToString() example above.  The term method is synonymous with function, procedure and subroutine from other languages.

Just to ensure you are following along correctly, here is what the code in my C# looks like after I added 4 new event methods.  This is inside the file Default.aspx.cs and follows the empty Page_Load method:

 

protected void Button1_Click(object sender, EventArgs e)
{
 TextBox1.Text = "My first program";
}
protected void Button2_Click(object sender, EventArgs e)
{
 //This is a variable declaration in C#
int i;
//This is an assignment operation in C#
i = 33;
//This is an assignment operation in C#
TextBox2.Text = i.ToString(); 
}
protected void Button3_Click(object sender, EventArgs e)
{
 float f = 55.12f;
 TextBox3.Text = f.ToString();
}
protected void Button4_Click(object sender, EventArgs e)
{
 bool tedsbool = false;
 TextBox4.Text = tedsbool.ToString();
}

Notice that each button (1-4) has its own event handler method that gets executed when the corresponding button is pressed.

Finally to close out this exercise lesson in Data Types, in the Design view, add another Button and TextBox on a new line.  This time make the button say “Show a string”.  Create the event method.  Inside the method put:

 

string tedstring = "This is an example string!";
TextBox5.Text = tedstring;

This special data type called “string” is actually an alias for the System.String class from the Framework and holds text characters.  It is dynamic so you don’t have to declare how many characters you want to put in there before you use it.  The right side of the assignment operator where we have “This is an example string!” is what’s called a string literal.  This means you are hard-coding a text value for the string variable right into your code.  Also, since string variables are dynamic, you can always reassign the tedstring variable a different value further down in the code.  This applies for the other variable types that we experimented with in this exercise as well.  Here is an example where I reassign a string variable:

 

string tedstring = "This is an example string!";
tedstring = "A new value";
TextBox5.Text = tedstring;

If you run the program now, it will display “A new value” in the Textbox because I have reassigned the variable to a new value on the second line.  You only declare a variable once, but you can assign it many times, as much as is needed in your program.

5 Comments »

  1. After a long time searching, this is the perfect walk through for a novice like me. Thanks

    Comment by Doug Snyder — January 28, 2009 @ 3:20 pm

  2. I agree; this is great for beginners like me - thank you!

    Comment by Nicola Williams — April 10, 2009 @ 5:30 pm

  3. Excellent Work

    Comment by Junaid — July 28, 2009 @ 12:53 am

  4. no need to scratch our heads till TED is with us

    Comment by Junaid — July 28, 2009 @ 12:54 am

  5. I really like this guide/tutorial! The concepts are explained very well, and you increment the level of difficulty just enough with each new step! I felt really comfortable with these concepts upon completing this lesson.
    Thank you!

    Comment by Spencer Gardner — September 22, 2009 @ 11:00 am

RSS feed for comments on this post. TrackBack URL

Leave a comment