How to Create a Class, a Tutorial on Creating a C# Class using Visual Studio.Net

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 class in Visual Studio.
Create a Class
Using a Class

One of the basics to good object oriented programming is the use of classes to control access to objects and provide reusable code, which is easy to maintain. This tutorial covers the basic task of creating a class using Visual Studio. After following the simple steps presented, you will easily create your own class and be able to instantiate the class on a WebForm.

Let's get started creating our C# class...

1. Open Visual Studio
2. Choose New Project
3. Choose Visual Studio Solutions / Blank Solution
4. Type in the name "CreateClass" / Click on the Okay button.

The New Project Dialog Box for creating a class in Visual Studio

5. Right Click on the new solution you just created, found in the Solution Explorer tab.
6. Choose menu item Add / New Project
7. Choose Project type of Visual C# Projects
8. Choose Template type of Asp.Net Web Application
9. Type in a location of "http://localhost/CreateClass/YCLSProj1"

Creating a new project in Visual Studio

10. Choose OKAY, This will create a new Web Project on your local harddrive in "C:\inetpub\wwwroot\createclass\YCLSproj1"

Now you are ready to add a new class.

1. In Solution Explorer, right click on your Project. (YCLSProj1)
2. Choose menu item "ADD" / "Add Class"
3. The dialog box that opens will already have "Web Project Items" chosen with the "Class" template highlighted.
4. You need to change the class name to "Friends.cs", then choose the "Open" button.

Creating a class using the Add New Item dialog box.

5. That will create a new class ...
      public class Friends
      {
      }
6. You also see the constructor for the class
      public Friends()
      {
      }
7. Create a couple of variables.
Above the constructor for the class add two public variables.
      public string id;
      public string name;
8. Now create a new public constructor using the variables you just created.
      public Friends(string id, string name)
      {
      this.name = name;
      this.id = id;
      }

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

Sara A Mason - Sci-Fi Author of Renegades


Americas Public Schools

Learn insider tips from one of the web’s biggest podcasters Pat Flynn!

Tap Influence Co-Founder Holly Hamann details how to make your blog more brand friendly.

Tiny Cupcake Toppers, with Sheryl Bito

Figure Anatomy for the Artist, with Roberto Osti

BODYBUILDING PROTEIN

Shop Seattle Seahawks Super Bowl Championship Gear at Fanatics.com

Dog Supplies

Lowest Prices on Fruit Trees, Shade Trees, Flower Bulbs, Perennials, House plants, Vegetable Plants, Seed and More!