Answered by:
Access class by another class which are not placed in App_Code folder

Question
-
HI,
I have 2 different folders in my projects like New1 & New2 . I created a class in New1 and like same i created class as Class2 in New2 folder. I want to access Class1 from Class2. But Cant able., Anyone tell me the answer if u know.,
Thanks in advance
Jey
Jey www.hoopla.co.in
Tuesday, August 6, 2013 11:16 AM
Answers
-
Open Class2 and see if there is a namespace declaration. It will generally be the project name plus the name of the folder. You'll have to add a using statement in Class1 to add the namespace. You didn't specify whether this is a web app or a web site so note that the code in App_Code might generate slightly differently depending upon which type of app you're building.
Michael Taylor
http://msmvps.com/blogs/p3net
- Proposed as answer by Adavesh Tuesday, August 6, 2013 3:11 PM
- Marked as answer by Lilia gong - MSFT Thursday, August 15, 2013 7:24 AM
Tuesday, August 6, 2013 2:57 PM
All replies
-
Open Class2 and see if there is a namespace declaration. It will generally be the project name plus the name of the folder. You'll have to add a using statement in Class1 to add the namespace. You didn't specify whether this is a web app or a web site so note that the code in App_Code might generate slightly differently depending upon which type of app you're building.
Michael Taylor
http://msmvps.com/blogs/p3net
- Proposed as answer by Adavesh Tuesday, August 6, 2013 3:11 PM
- Marked as answer by Lilia gong - MSFT Thursday, August 15, 2013 7:24 AM
Tuesday, August 6, 2013 2:57 PM -
Hi,
Thanks for your reply.,I Created website. In this i created 2 different folders Like New1 & New2.
In New1 folder i created Class1 and in New2 folder i created Class2. In class2 that i want to use methods which is defined in Class1.But now i cant access Class1 by Class2. Please help me to slve this.,
Example:
New1 Folder :
public Class class1
{
public void test() {}
}
New2 Folder:
public Class class2
{
class1 objClass1 = new class1();
objClass1.test();
}
I want like this.,
Jey www.hoopla.co.in
Wednesday, August 7, 2013 5:53 AM -
Your code is not syntactically correct. The code for referencing Class1 needs to be inside a method of class2.
public Class2 { public void Foo () { var objClass1 = new Class1(); objClass1.Test(); } }
By default when you select Add\Class from the App_Code folder (or subfolder) of a web site then the generated class has no namespace so your code should just work once you fix the syntax error. If it doesn't then open Class1 and see if there is a namespace statement that is putting it elsewhere.
Wednesday, August 7, 2013 3:23 PM