Home > 2010 Winter BTI420 > Creating your own types (classes) in C# for ASP.NET Web Forms apps

Creating your own types (classes) in C# for ASP.NET Web Forms apps

January 25, 2010 Leave a comment Go to comments

At this point in time, you know how to use some of the simple types in the .NET Framework. You have also learned something about list web server controls, which are types that are a bit more complex. In this post, you’ll learn to create your own types.

 

Creating your own types enables you to make your app more modular. Your types can encapsulate anything you can think of, in any combination of data and functionality.

Therefore, if you have an opportunity to create a class that models your app’s data, and/or performs services for many pages (web forms), take advantage of the opportunity and do it.

 
Note: In this post, and in others, we will use "type" and "class" for the same concept.

Also, when you create an instance of your type/class in code, we call it an "object".

 

Existing classes that you have seen

Every page – web form – that you have created is a class.

An ASP.NET Web Forms page inherits from System.Web.UI.Page. Open the C# code for one of your pages, and notice the class declaration:

public partial class labs_lab2 : System.Web.UI.Page

A class declaration has these syntax features:

  1. It starts with a visibility keyword – public
  2. It typically includes a keyword that enables extensibility in the future – partial
  3. It then includes the class keyword, followed by your class name
  4. Then, there’s a colon – : – followed by the superclass name that this class inherits from – System.Web.UI.Page

Below the declaration, you have seen method implementations, including Page_Load, which is supplied in the template, as well as your own methods that were created deliberately by you, or by Visual Studio for user interface event handlers.

For your own classes, you need to add a few more features, including constructors and properties. We’ll learn these now.

 

Creating a new class

In an ASP.NET Web Forms app, you create and store your own classes in a folder called "App_Code". Go ahead and create the folder now:

  1. In Solution Explorer, click on your web app root (which starts with http://warp…)
  2. Right-click, and choose Add ASP.NET Folder, then App_Code

Now you’re ready to create your own class. We will create a throw-away class:

  1. Right-click the App_Code folder, and choose Add New Item…
  2. Choose Class 
  3. Enter a name for your new class (beginning with an uppercase letter, and camel-casing for the remainder of the name; for example, you can call it MyFirstClass), and then click the Add button

The template creates the following code in a ".cs" source code file:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

/// <summary>
/// Summary description for MyFirstClass
/// </summary>
public class MyFirstClass
{
	public MyFirstClass()
	{
		//
		// TODO: Add constructor logic here
		//
	}
}

Classes that you create have the following initial syntax features:

  1. It starts with a visibility keyword – public
  2. It does not include the "partial" keyword (that’s OK)
  3. It then includes the class keyword, followed by your class name
  4. It does not include the inheritance declaration (a colon followed by a superclass name); by default, new classes inherit automatically from the .NET Framework "System.Object" class
  5. It includes a default constructor that enables the creation of new instances of the class

Your new classes, by default, are intended to be used as instance classes. By using the "static" keyword on member declarations, and a bit of care, you can easily offer functionality to users without them having to create an instance of your class. You’ll see that in today’s "ClassUtility" example.

 

Learning from today’s code examples

There are four code examples today:

  1. ClassUtility
  2. ClassPeople
  3. ClassCars
  4. UsingMyClasses

During the class discussion, you will learn how to code your own classes, and implement:

  • fields
  • constructors
  • properties
  • methods
  • documentation

You will notice that two of my class examples – People and Cars – include TWO class declarations in the source code file. That’s OK – a source code file is just a container – your classes are the important parts when Visual Studio compiles your app.

 

Checklist to follow when creating a new class

The following is a summarized checklist that you can follow when creating a new class.

__ 1. Create a class, and give it a proper name

__ 2. Make sure you document your work as you go along, by using the /// syntax

__ 3. Include ("using") any other frameworks you need to bring in scope

__ 4. Mark the class as [Serializable] if you intend to serialize it in the future

__ 5. Declare its inheritance (if other than the default)

__ 6. Declare class fields that typically will be used internally by a class instance

__ 7. Implement its default constructor (if needed) by setting class fields to reasonable initial values

__ 8. Add and implement additional constructors so that new instances can be created by arguments that get passed in

__ 9. Typically, you will need state persistence – properties – so, implement them

__ 10. Typically, you will need functionality – methods – so, implement them

 

Happy coding!

 


Advertisements
Categories: 2010 Winter BTI420
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: