Adding your own class to a .NET project

Posted on 2009-02-20
Last Modified: 2012-05-06

I am trying to learn C# .Net. I have created a Default.aspx file and also included a class in App_Code map.

I am wondering how I include this new class in the Default.aspx.cs?

What shall I write. I think I have tried everything.

using App_Code/MyNewClass;  ???

or what?

Question by:Mickeys
    LVL 53

    Expert Comment

    you can initiate your class very easily like:

    myClassname item = new myClassname();
    // do whatever function your declared on your class
    int sum = item.CalcSum(100,200);
    LVL 39

    Assisted Solution

    What you need is using ... + the namespace you have put in the top of the App_Code class, or nothing, in which case you should be able to use it directly.
    LVL 39

    Accepted Solution

    If your App_Code class starts with "public class MyClass", you should be able to use your class like:

    MyClass myClassObj = new MyClass();

    If your App_Code class starts with

    namespace XYZ.MyNamespace
          public class MyClass

    then you should call it in your code-behind (aspx) like follows:

    using XYZ.MyNamespace;  // in the top


    // in your Page_Load or wherever
    MyClass myClassObj = new MyClass();

    Now, if you get some strange errors about "No public constructors found" or similar, you are missing a public constructor. Add this to your Class definition:

    // empty public constructor
    public MyClass()
    LVL 39

    Expert Comment

    Tx, glad I could've been of help :)

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    731 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now