Part of the
You Can Learn C# series.
By Ken Brown
Updated: August 31, 2004
This is a two part article on creating a C# class in Visual Studio .Net.
Create a Class
Using a Class
This is part two in a 2 part
tutorial on creating a class using C#. Up to this point you have learned to
create a class, but now you need to know how to use it, otherwise you only have
half the story.
It was easy to create a class, but how do you use a class?
1. Go to WebForm1.aspx.
2. Drag two label controls onto the form from the toolbox.
3. Change the text property of one of the labels to ID.
4. Change the text property of the other label to Name.
5. Drag a Textbox onto the form from the toolbox.
6. Change the ID property to tbID.
7. Drag another Textbox onto the form from the toolbox.
8. Change the ID property to tbName.
9. Drag a button control onto the form.
10. Change the id property to btnUseClass.
11. Change the text property of the button control to "Use Class".
12. Add two more label controls to the form, give one an ID of lblID and the
label an ID of lblName. Delete the value of the text property for both of the
Creating Code Behind on an ASPX form
1. Double Click the Use Class button.
2. This brings you to the btnUseClass_Click event
3. First you have to create an instance of your class.
Friends f = new Friends();
4. What we are trying to do is have your user type in an ID and a name,
and then store the information in your class and then get the
information from the class and show it on the labels.
5. First we get the info from the text boxes
f.id = tbID.Text;
f.name = tbName.Text;
6. As you are typing in the friend class f, Intellisense will
kick in and you can select the field you want.
7. Now, let's take the values in the Friend class and display them in the
two labels we created.
lblID.Text = f.id;
lblName.Text = f.name;
8. Run the Application. Type in a number in the ID box and a name and click on
the Use Class button.
You know sometimes the easiest things to do in programming end up being the
hardest tasks to complete. But now you have seen a simple solution to creating
a class. As with all programming, now you need to practice creating your own
class with Visual Studio.Net so it becomes second nature to you. This is the
foundation to good programming with C#.
Go To Page 1 /