CS0234 - Cannot Instantiate C# class in ASP.NET - c#

I made the same ASP.NET C# project in both VS2010 and MonoDevelop using these two classes among the standard files (Site.Master, Web.Config, Default.aspx, etc.) and recieve this same error (CS0234) seen at the bottom.
Login.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using mynamespace;
namespace mynamespace
{
public partial class Logon
{
public void btnClicked(object sender, EventArgs e)
{
//ERROR IS HERE:
mynamespace.Test session = new mynamespace.Test();
//Obviously, this doesn't work either:
Response.Write(session.echoUser());
}
}
}
Test.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using mynamespace;
namespace mynamespace
{
public class Test
{
public string echoUser()
{
return "foobar";
}
}
}
I recieve the same error in both IDEs, here is the MonoDevelop error:
The type or namespace 'Test' does not exist in the namespace 'mynamespace' (are you missing an assembly reference?) (CS0234) Logon.cs
Basically, the class Test refuses to instantiate. Any input is appreciated!

If you have that Test class in an ASP.Net web project, then you need to place it in the App_Code folder, not just anywhere in the site.

You need to refer the projects to each other, if you haven't done so yet. I'm guessing that you don't get the intellisense to show the class in the other namespace, right?
You can see the References on the right side (most cases) under your project view. You can right click there and choose to Add reference. Then you browse to the binaries from the pther projects. (You might be able to point to the project itself too - I don't have VS in front of me at the moment.)
Also, It's a convention to use camel case for namespaces, so it should be MyNameSpace.
If the classes are in the same project, you might want to skip using mynamespace and refer to the class by Test intead of mynamespace.Test.

If these are both in the same project, please check that both files are included in the project and have the "Build Action" property set to "Compile". Please also check that all of these namespaces have exactly matching spelling and casing.

Related

Namespace could not be found

It can be seen and compiled inside the same file it is in inside the same project, the file is called default.aspx.cs.
However when I try to include the namespace in another file of the same project using the using DBConnStrings; statement -> I keep getting the compiler error "The type or namespace name 'DBConnStrings' could not be found".
The Code in the file which compiles called default.aspx.cs is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using DBConnStrings;
namespace DBConnStrings
{
public class GlobalStrings
{
public static string carSalesDBConnString =
ConfigurationManager.ConnectionStrings["ConnectionString2"].ToString();
}
}
public void BindManu()
{
SqlConnection conn = new
SqlConnection(GlobalStrings.carSalesDBConnString); // Connect to Carsales database
conn.Open();
// .....
}
The other files can not see this namespace although they are in the same project.
How do I make them see it?
First of all, I'd suggest to put the method BindManu into the class, because I can't imagine this would work like that.
But to your problem: You have to specify the whole namespace. That means, if the file with the DBConnStrings namespace is in the folder test, you have to use using test.DBConnStrings to import the namespace. You should name the namespace like that as well to avoid confusion (namespace test.DBConnStrings).
But you actually don't have to specify the whole path, just the path within the project. That means, if your class is in C:\Users\Foo\Documents\Visual Studio 2017\Projects\BlaProject\DirectoryA\DirectoryB\MyClass with the project located in C:\Users\Foo\Documents\Visual Studio 2017\Projects\BlaProject, your namespace would be BlaProject.DirectoryA.DirectoryB. If the file your using isn't in your project folder, then you have to add a reference and use the path within the other project as above (but with another project name, obviously). If you want to add references, open the Solution Explorer, right click onto References, select add Reference, and select the reference to the project.
If you don't want to struggle with all that you can let vs do it for you. Simply type in the class you want from the other namespace, it will be marked as an error, click onto the lightbulb and select something like add using reference.
Furthermore, if you want to add a class to your project, don't do it with the explorer - simply right click onto the folder from your project you want to add the class to in your Solution Explorer, select Add, then New Item. In the popup select class and type in your name for the class. That's it!

The type or namespace name could not be found is shown

I have created sample application as layers.
I have a layer which is under DataAccess namespace
using System;
using System.Data;
using DataAccess.DataAccessLayer; //This line is showing error
namespace AdapterClass
{
public class SchemaAdapter
{
}
}
Inside a file, intellisense of current namespace only available.
For ex:
In above example, intellisense of AdapterClass only available.
Be careful with your terms. A namespace is different than an assembly. If your data access class (and namespace) is in another project (say class library) then it is in a different assembly that needs to be referenced. To do that, you can:
Right click the project you shown the code above and right click References => Add Reference => Solution Tab => Click that project

Couldn't find modelDataContext

Im trying to use LINQ in my ASP.NET/C# website. For that I need to access the class 'modelDataContext' which is a part of the LINQ namespace.
Im using Visual Studio 2010 Professional
This is my C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class admin_new_feed : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
modelDataContext data = new modelDataContext();
}
}
Full error message I get
Error 1 The type or namespace name 'modelDataContext' could not be
found (are you missing a using directive or an assembly
reference?)
Things i've tried, which doesn't solve problem
Adding the reference (Right-click solution, add reference - System.Data.Linq)
Re-create the website
Right clicking on the line, to see if the tab "Resolve" was there. But it wasn't.

The type or namespace 'IRange' could not be found

I'm using C#.net 4.0 with Visual Studio 2010. I am getting the error
Error 10 The type or namespace name 'IRange' could not be found (are
you missing a using directive or an assembly
reference?) C:\git\emtexporter\EMTExporter.IRepository\IRangeRepository.cs 11 27 EMTExporter.IRepository
IRange is an interface in the project EMTExporter.IEntities and the project IEntities builds successfully. IRange.cs has the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wiggle.EMTExporter.IEntities;
namespace Wiggle.CategoryXMLExporter.IEntities
{
interface IRange
{
long ID { get; }
Dictionary<ILanguage, string> rangeNames { get; set; }
}
}
The problem occurs in IRangeRepository.cs which has the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wiggle.EMTExporter.IEntities;
namespace CategoryXMLExporter.IRepository
{
interface IRangeRepository
{
Dictionary<string,IList<IRange>> getAllRanges();
}
}
I am referencing EMTExporter.IEntities in the IRepository project.
I have no idea what could be going wrong!
edit: the issue was that the project was changed from CategoryXMLExporter to EMTExporter, but I hadn't updated the Assembly name and Default namespace of the project. Updated that, made the interface public and now it works!
Your interface is not public try the following
public interface IRange
The default accessibility level top level classes and interfaces is internal, not public, so if these are in different projects, it won't be visible.
Classes and structs that are declared directly within a namespace (in other words, that are not nested within other classes or structs) can be either public or internal. Internal is the default if no access modifier is specified.
namespace Wiggle.CategoryXMLExporter.IEntities
{
public interface IRange
{
long ID { get; }
Dictionary<ILanguage, string> rangeNames { get; set; }
}
}
You need to add the namespace Wiggle.CategoryXMLExporter.IEntities to your using clauses, because IRange is defined there:
using Wiggle.CategoryXMLExporter.IEntities;
Also, if it's in a different assembly, you need to make it public.
IRange is in the namespace Wiggle.CategoryXMLExporter.IEntities so you will need to reference that. Also make the Interface public
In addition to what has been mentioned above, sometimes you may need to ensure the referenced library is configured as being built under the selected active configuration and platform.
In Visual Studio 2012 or 2013, right-click on the Solution and chose "Configuration Manager".
For the selected "Active Solution Configuration" (e.g. "Debug" or "Release") and "Active Solution Platform" (e.g. "Any CPU", "x64", "x86", or "ARM") ensure the project that contains your interfaces and any of its dependencies have "Build" checked.

Cannot inherit System.Window.Forms.Form class after manually change class name

I have written a simple Form1 (inherit from Form) class in Visual Studio C#. All was good.
Then I wanted to change the name of the class and the namespace to something meaningful instead of the default 'WindowApplicationForm1' . I also changed the file name of the Form1.cs class to match the new class name (IRISReaderGUI and Com.Harmonysoft). First I manually rename the code, then compiled, the compiler gave me lot of error that I could not figure out. So I tried to rename my class and namespace using 'refactoring' menu. My code still didn't compile.
I did some research and change the 'IRISReaderGUI.Designer.cs' class name and namespace to match the new names. C# still didn't give me no joy.
The compiler erros message was :
'Com.Harmonysoft.IRISReaderGUI.Dispose(bool)': no suitable method
found to override'
On the designer view it said : The base class'System.object' can not be designed.
So I guess my IRISReaderGUI did not correctly inherit the System.Windows.Forms.Form class, this was confirmed by putting the mouse over the Form word in the code, Visual Net does not popup the text describing the class, and pressing F12 does not get me to the Form definition.
Here is the code :
"IRISReaderGUI.Designer.cs"
namespace Com.Harmonysoft
{
partial class IRISReaderGUI
{
.......
}
"IRISReaderGUI.cs"
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Data.Odbc;
using System.Data.OleDb;
using System.Data.OracleClient;
using Com.StellmanGreene.CSVReader;
using System.Windows.Forms;
using System.IO;
namespace Com.Harmonysoft
{
public partial class IRISReaderGUI : Form
{
......
}
I am new to C# and Visual Studio, I have previously worked with Java compiler and Vim editor mostly. Could anyone please help me to compile my code ?
Forms in VS2005 and up are partial classes. You edited the one part but not the other. In the Solution Explorer window, expand the node next to the form and double-click the Designer.cs file to open it.
Using the Refactor + Rename context menu command is the better approach, it doesn't forget to edit the other source files as well. It didn't work when you tried it because the damage was already done, the parts no longer had the same name.

Categories