[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

Adding your own class to a .NET project

Hi

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?

/M
0
Mickeys
Asked:
Mickeys
  • 3
2 Solutions
 
DhaestCommented:
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);


http://www.csharp-station.com/Tutorials/Lesson07.aspx
0
 
abelCommented:
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.
0
 
abelCommented:
If your App_Code class starts with "public class MyClass", you should be able to use your class like:

MyClass myClassObj = new MyClass();
myClassObj.DoSomething();

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();
 myClassObj.DoSomething();


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()
{
}
0
 
abelCommented:
Tx, glad I could've been of help :)
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now