I am trying to modify an existing .net "website" project by adding a new class to it. I added my class inside the App_Code folder (let's call this class "ClassA"), and tried to access it from the outside in an aspx.cs file. For some reason, right after I created the class, I can create an instance of it in my aspx.cs file without any warnings from Visual Studio (e.g., ClassA a = new ClassA()). But every time I rebuild the project, I get the following Error from visual studio (The type or namespace name 'ClassA' could not be found (are you missing a using directive or an assembly reference?)). What am I missing here?
Code for Class A -> App_Code/ClassA.cs
public class ClassA
{
public string test;
}
Code for Test.aspx.cs
namespace A{
public class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClassA a = new ClassA(); // line with error
}
}
}
If I put everything in Test.aspx.cs I get no errors:
namespace A{
public class ClassA
{
public string test;
}
public class Test : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ClassA a = new ClassA(); // no error!
}
}
}
By default the classes inside App_Code folder has property "Build Action" set as "Content".
If you want to use it you need to set its "Build Action" property to "Compile".
This should work.
I am really sorry, for I could not totally understand the problem.
In my opinion you should not use namespaces when you create a class in app_code and let it be accessible to the whole website project.
In this instance you have added a namespace, try adding:
using b;
where b is your namespace you used in your new class. This should solve your problem.
Related
I want to use old code in a new project in c#.
This is the old code. It is saved in an arbitrary folder on my PC.
namespace OldCode{
class HellowWorld
{
public static void SayHello()
{
Console.writeline("Hello World")
}
}
}
I want to use this in a new project called new code.
using OldCode (?)
namespace NewCode
{
class SayHelloNew
{
HelloWorld.SayHello();
}
}
My question is, how do I link to my old project? Did I do it right with using OldCode? Or is there some other dependency file I need to set up?
Yeap, Let me describe for you,
First you should make public your class to access it in other sources by adding public modifier like this:
using System;
namespace Stackoverflow.OldCode
{
public class HelloWorld // <----------- make it public
{
public static void SayHello()
{
Console.WriteLine("Hello World!!!");
}
}
}
Then I suggest adding a prefix in every other namespaces to show they are related to each other, like
namespace Stackoverflow.OldCode;
namespace Stackoverflow.NewCode;
The NewCode project and the OldCode should be in one solution, if not now, right-click on the solution name and choose: Add>New Project... or Add>Existing Project...
Then you should add the NewCode project as a reference to the OldCode project:
step1:
step2:
Now you can access old project methods easily, like:
using Stackoverflow.OldCode; // <---- add the reference
namespace Stackoverflow.NewCode
{
class SayHelloNew
{
public static void SomeRandomName()
{
HelloWorld.SayHello(); // <----- call it
}
}
}
That is almost correct. The colon (:) needs to be removed from the namespace declaration and to access the HelloWorld class externally, it must be public.
OldCode\HelloWorld.cs
namespace OldCode
{
public class HelloWorld
{
public static void SayHello()
{
Console.writeline("Hello World")
}
}
}
NewCode\SayHelloNew.cs
using OldCode;
namespace NewCode
{
class SayHelloNew
{
HelloWorld.SayHello();
}
}
You will also need to ensure the compiler has access to OldCode on compilation, or, if using Visual Studio, add a Reference to the OldCode project in the NewCode project.
Add references in Visual Studio
I have one main form and a static class when i access static class member it gives me nullreference error . Previously it was working fine donot know what happend . Any one can suggest what is wrong.
code snap:
namespace MyNamespace
{
public partial class myForm : Form
{
public myForm()
{
InitializeComponent();
}
private void myForm_Load(object sender, EventArgs e)
{
My_Static_Data_Class.player_name="Demo Player"
}
}
public static class My_Static_Data_Class
{
public static string player_name = "";
}
}
please help?
You're either accessing the static class member before it was set to "Demo Player". For example, you are trying accessing My_Static_Data_Class.player_name in Program.cs code before it calls the main form from the Main[] method. Alternatively you're might be setting My_Static_Data_Class.player_name to null elsewhere in code and then accessing it.
Check all the references in code editor and follow it up. To do this, right click on My_Static_Data_Class.player_name in Visual Studio editor, and choose Find All References menu item.
I have an ASP.net solution, which contains a project InvestTracking where I am calling a function GetInvestPeriod() from the PageLoad event.
I have another project InvestTrackingBL which contains the function GetInvestPeriod().
protected void Page_Load(object sender, EventArgs e)
{
GetInvestPeriod();
}
public class InvestTrackingBL
{
protected void GetInvestPeriod()
{
}
}
I am getting an error in my main project:
'The name GetInvestPeriod does not exist in the current context'.
What am I missing here?
You can't just use the function like that.
You either need an instance of InvestTrackingBL or call the static method on it:
//If it's an instance member
var investTrackingBL = new InvestTrackingBL();
investTrackingBL.GetInvestPeriod();
//or if it's static
InvestTrackingBL.GetInvestPeriod();
And following your update. Methods that are protected cannot be called by other classes, only derived classes, you need to make it public or internal.
I have two projects in my solution called ePad demo and PLHardwareHelper. I have added a reference to the PLHardwareHelper for the epad demo and I can successfully access methods in epad from hardwarehelper.
The problem is though that I cannot access anything in hardware helper from epad. I can't added a reference because it says it will result in a circular reference. I can't even add the namespace as a reference in the form.
How can access methods in hardwarehelper from within epad without adding a reference?
I have added a third reference called ApplicationJoin so I can add it's reference to both the other projects but it still complains that it can't find it. I have added an image below to demonstrate:
The code from ApplicationJoin:
namespace ApplicationJoin
{
public partial class Form1 : Form
{
public static string _imagevalues = "";
public Form1()
{
InitializeComponent();
}
public static void ImageValues(string value)
{
_imagevalues = value;
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
The issue:
The error on the blue squiggly line is:
The name 'ApplicationJoin' does not exist in the current context
I have tried rebuilding and cleaning the solution but it still doesn't work.
I want to create a user control DerivedUserControl.ascx that derives form another user control BaseUserControl.ascx. The base user control derives from System.Web.UI.UserControl as required. These user controls are defined in different folders. Because I'm using a Visual Studio 2010 Web Site project (I'm not able to switch to Web Application project), these user controls are not defined inside a namespace.
My problem is that when I try to compile the project the base class of the derived user control cannot be resolved (obviously because the compiler doesn't know what .ascx file defines the base class). Is there a way resolve this issue?
I tried everything I could imagine, without success. Any help would be greatly appreciated.
BaseUserControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="BaseUserControl.ascx.cs" Inherits="BaseUserControl" %>
BaseUserControl.ascx.cs
public partial class BaseUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
DerivedUserControl.ascx
<%# Control Language="C#" AutoEventWireup="true" CodeFile="DerivedUserControl.ascx.cs" Inherits="DerivedUserControl" %>
DerivedUserControl.ascx.cs
public partial class DerivedUserControl : BaseUserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
Error
The type or namespace name 'BaseUserControl' could not be found
When using ASP.NET Web Site (as apposed to a Web Project) you need to add <#Reference> element to your DerivedUserControl.ascx.
From MSDN it...
Indicates that another user control, page source file, or arbitrary
file located at some virtual path should be dynamically compiled and
linked against the current ASP.NET file (Web page, user control, or
master page) in which this directive is declared.
<%# Reference VirtualPath="~/FolderName1/BaseUserControl.ascx" %>
Once you've done that you can reference it like so
public partial class DerivedUserControl : ASP.foldername1.baseusercontrol_ascx
Where FolderName1 is the folder your BaseUserControl is in.
Create a regular class/.cs for your base class called BaseUserControl.cs:
public class BaseUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
}
}
The problem appears to be that the DerivedUserControl.ascx does not have access to the DLL that contains the BaseUserControl. Make sure that you add a reference to the dll and have copy local = true.
This does not compile:
namespace MyBase
{
public class BaseUserControl : System.Web.UI.UserControl
{ }
}
public class DerivedUserControl : BaseUserControl
{ }
This does compile:
namespace MyBase
{
public class BaseUserControl : System.Web.UI.UserControl
{ }
}
public class DerivedUserControl :MyBase.BaseUserControl
{ }
So pretty much add the name of the namespace + dot + the name of your base class.
Good luck!