Namespace could not be found - c#

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!

Related

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

Take String from Clipboard? No assembly reference?

If the current (most recent) item in the clipboard is a string, I want to take the string from the clipboard, and put each word in an array (just a simple loop which eliminates spaces and newlines, to take just words from the string). Then I simply want to print each item in the array (let's say each separated by a newline for testing purposes) to the terminal.
This is super easy, simple, etc. My trouble is that I cannot seem to find/use the Clipboard class that is built in to C#! Here is what I am using as my assembly references:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Object;
using System.Windows.Clipboard;
Note that the System.Windows.Clipboard; gets a red underline, because it is not lining up. Why could this be? http://msdn.microsoft.com/en-us/library/system.windows.clipboard.aspx shows the clipboard class information.
It's probably a fault on my end, but is it otherwise possible I have not set my paths correctly or something? I just installed Visual Studio 2012 For Desktop SP3 (although, I have had VS2012 for Web for quite some time, and it works properly).
System.Windows.Clipboard is a class and not a namespace, there is no point putting a class into a using directive.
The compiler error message should tell you the same.
First of all you must add a reference to System.Windows.Forms in your application. Go to Project -> Add reference, select System.Windows.Forms from .NET tab in the window that just opened. You must avoid the ThreadStateException by applying the STAThread attribute to your Main() function. Then you can use the Clipboard functions without any problems.
using System;
using System.Windows.Forms;
class Program {
[STAThread]
static void Main(string[] args) {
Clipboard.SetText("this is in clipboard now");
}
}
i hope this will help you
http://blog.another-d-mention.ro/programming/c/use-clipboard-copypaste-in-c-console-application/
it shows how to use the Clipboard functions in C# console application
You don't need this line: using System.Windows.Clipboard; using statements are for namespaces, and Clipboard is a class. That class is provided from the assembly PresentationCore.dll, and since you have WPF project, your project already has a reference to it.
You should have reference to System.Windows namespace only it has Class Clipboard in it.
You can set and get data from there
System.Windows.Clipboard.SetData();
You'll need a namespace declaration:
using System.Windows.Forms;
OR for WPF:
using System.Windows;
To copy an exact string
var dataObject = System.Windows.Clipboard.GetDataObject();
string text= dataObject.GetData("UnicodeText", true).ToString();

How to load SoundTouchSharp?

I'm using the library, SoundTouchSharp, in my project. How do I create an instance of it? I don't really understand if I need to use the "using" line and if I need anything else so that I can eventually get
SoundTouchSharp s = new SoundTouchSharp();
because right now the compiler says:
"the type or namespace name SoundTouchSharp could not be found."
I included SoundTouchSharp as a reference (after creating a .dll version of the .cs file), and I also put both SoundTouchSharp.dll and soundtouch.dll in the bin/Debug folder.
So now what?
I included SoundTouchSharp as a reference (after creating a .dll
version of the .cs file), and I also put both SoundTouchSharp.dll and
soundtouch.dll in the bin/Debug folder.
If you want to use the SoundTouchSharp class from the BigMansStuff.PracticeSharp.Core namespace in another namespace you have to make it public and then create the library.
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;
namespace BigMansStuff.PracticeSharp.Core
{
/// <summary>
/// .NET C# Wrapper to the SoundTouch Native C++ Audio library
/// </summary>
/// <see cref="http://www.surina.net/soundtouch/index.html"/>
public class SoundTouchSharp: IDisposable
{
//...
}
//...
}
Then in your project you have to add a using directive that allows to use that type from the namespace:
using BigMansStuff.PracticeSharp.Core;
You need to add a using statement to include the namespace that the class is defined in:
using BigMansStuff.PracticeSharp.Core;

CS0234 - Cannot Instantiate C# class in ASP.NET

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.

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