Main Points

Here's demo of shared classes in ASP.NET. A shared class is a class that can be used by any webpage. The most elementary/easiest way to write C# code for a .aspx page is to write the necessary code in the code behind file, e.g. code for the page Default.aspx goes in the code behind file Default.aspx.cs. This is just fine as long as no other webpage needs to run the same code. If not, you need a shared class.

Shared C# Class Demo

If there is code for Default.aspx that's needed by a second, several, or all other webpages, you'll need to retype the code into the code behind page for every other webpage. And if you need to perform maintenance later, you'll need to change it in every single code behind file. Shared classes eliminate this problem. ASP.NET includes a folder called App_Code. Any files placed inside the App_Code folder can be shared by all pages.

So if you have a miscellaneous function that needs to be accessed from two or more webpages create the class in a .cs file, e.g. Misc.cs. Then in the code behind file of each page, you simply use the class you've created. Now, if you need to make a change, you alter the class once in Misc.cs and it will take effect for all pages. The class in the Misc.cs file is "shared" between all pages. Which isn't to say that you have to use it on every page, just that it's available for any page where you choose to call it. Here's a very simple application of this technique. Try typing something in the text box and activate the submit button.



Entered Text.
Skip to Main Points

Shared C# Class Code

Here's an example of a shared class in action. We're going to create a new class called SampleClass1 and define it as a public class - hence one we can access from outside SampleClass1 class code. This is a very simple class. It has two variables: an internal variable only used inside this code, testStringValue, and one which is available from outside, the public variable testString. The public variable is a property which uses get and set functions to control it's value. There aren't even any calculations here, testString is simply a string variable to hold string values.

public SampleClass1()
{
}
private string testStringValue;
public string testString
{
get
{
return testStringValue;
}
set
{
testStringValue = value;
}
}

The code behind contains the code which actually interacts with the page. It refers to tbInput which is a text box on the page and lblOutput which is a label. Only the code behind page (.aspx.cs) which is melded into the main .aspx page knows about these elements and so these lines of code must be in the code behind. Other than that, the only other line creates a SampleClass1 object called sc. It's defined in the first line of the button click event so its abilities can be tapped, in this case the testString property.

protected void btnSubmit_Click(object sender, EventArgs e)
{
SampleClass1 sc = new SampleClass1();
sc.testString = tbInput.Text;
lblOutput.Text = sc.testString;
}

Therefore, when the submit button is clicked, a SampleClass1 object, sc, is created. The testString property of sc is set to the value of tbInput's text value. (ie tbInput's text value is passed into SampleClass1's set function as the "value" and assigned to testStringValue.) When lblOutput's text value is set to sc.testString, it retrieves the value which we originally grabbed from the text box. (ie SampleClass1's get function returns the value of testStringValue and passed to lblOutput's text value.)

Now, why go to all this trouble just to create a string variable? Welcome to ASP.NET where a lot of the examples are pretty stupid. There really is no reason why you would go to all this trouble just to store a string value, however, this is a Microsoft help exercise. And a very important one given the importance of shared classes. However, it is a simple understandable demonstration to help get you started. Go ahead. Type something into the text box. When you click the submit button, the new SampleClass1 object will be created, your text stored in it (in the testString property). It will then appear in the label underneath the Submit button.

Skip to Main Points
<-- Back to C# Development
Continue to 2. Link Factory -->
<---- Jump back to C# Development