How to Use a Class, a Tutorial on Using a C# Class using Visual Studio

Part of the You Can Learn C# series.
By Ken Brown
Editor, YouCanLearnSeries.com
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 other
label an ID of lblName. Delete the value of the text property for both of the labels.

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.

This is the final webform from the Create a C# Class project.

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 / 2

Home| About Us | NewsLetters | Contact Us |

Copyright © 2004-2014 You Can Learn Series

You Can Learn Series Home Page You Can Learn Series
You Can Learn Series Home Page Home            About the creators of YouCanLearnSeriesAbout Us      Contact YouCanLearnSeriesContact Us      Site map of YouCanLearnSeriesSite Map      Privacy Policy of You Can Learn SeriesPrivacy Policy       Terms of Use of You Can Learn Series web siteTerms of Use       YouCanLearnSeries RSS feed for C# tips and tutorials     
C# and ASP.Net Programming TipsC# and ASP.Net      SQL Tutorial, tips and tricks for DBA'sSQL       Landscaping Tips for Home GardenersLandscaping       WeightLifting Tips for the Young and OldWeightLifting       Kennos BlogBlog       Good health TipsGoodHealth       Learn about Great Travel DestinationsTravel     Web Business TipsWeb Business            You Can Learn Series' Online StoreStore



You Can Learn C# and ASP.Net SeriesC# and ASP.Net
  Using Client Side Code with C# and ASP.NetClient Side Code
  Using Client Side State Management with C# and ASP.NetClient Side State Management
  Create a C# Class in Visual StudioCreate A Class
  Use A Class in C#Use A Class
  Create a Solution in Visual StudioCreate a Solution
  Create a Project in Visual StudioCreate a Project
  Create an RSS feed for your Web SiteCreate an RSS Feed
  Create an RSS feed for your Web SiteListItemCollection
  Set Events in ASP.NetSet Events ASP.Net
  Set Events in C#Set Events C#
  What is Session State and How to Use it.Session State
  Creating and Using the C# SortedList ObjectSortedList Object
  Stylesheet Tips for Visual Studio .Net and ASP.NetStyleSheet Tricks
  Using C# MethodsUsing Methods
  Pass Objects to MethodsPass Objects to Methods
  Pass Objects from MethodsPass Objects from Methods
  Using Client side State management like ViewState and Hidden fieldsView State
  LDAP, What is Lightweight Directory Access ProtocolWhat is LDAP
  Build an LDAP ReaderBuild an LDAP Reader
  Convert information from LDAP to a Web ServiceLDAP To Web Service
  Question of the dayQuestion of the Day

You Can Learn SQL SeriesSQL
You Can Learn Landscaping and Gardening SeriesLandscape Tips
You Can Learn WeightLifting SeriesWeightLift Tips
Kenno's BlogBlog
You Can Learn Good Health SeriesGood Health Tips
You Can Learn Good Series NewslettersNewsletters
Learn about Great Travel DestinationsTravel
You Can Learn Series Web Business TipsWeb Business Tips
Merchandise Available from You Can Learn SeriesMerchandise

Bill Welter - The Prepared Mind of a Leader




Americas Public Schools