I having trouble finding assembly from a project. I have two Console Applications and one where i make a reference from the other. But then i can´t make the using-statement because it can´t find the project...
EDIT: my Program-class in Lab1PersonId:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Lab1PersonId
{
class Program
{
static void Main(string[] args)
{
}
}
}
EDIT:
using Lab1PersonId;
namespace Test
{
public class Program
{
static void Main(string[] args)
{
Lab1PersonId lab = new Lab1PersonId(); ***
}
}
}
Error on row ***:
Type name expected, but namespace named found
Your setup seems fine. You have added reference to the Lab1PersonId project inside your Test project, so normally you should be able to use classes that are defined in this assembly.
For example, you should first rebuild the Lab1PersonId project and then you should be able to add reference to some namespace that exists in it:
using Lab1PersonId;
Related
I opened a new project right now, and I opened a new class there with a "Main" inside it.
I already read whole over the internet about it and I already got that it happens because I have more than one "Main" method, but I read that when you choose inside the - Properties --> Startup object - your "Main" that you want to open it should be fixed.
The problem is that it doesn't show them at all.
What am I suppose to do?
That's the Error:
CS0017 Program has more than one entry point defined. Compile with /main to specify the type that contains the entry point.
My classes:
First -
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeginnerProject
{
internal class Program
{
static void Main(string[] args)
{
}
}
}
Second -
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeginnerProject
{
internal class FakeCoin
{
static void Main(string[] args)
{
Console.WriteLine("Hi");
}
}
}
Third -
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BeginnerProject
{
internal class WhyLikeThat
{
static void Main(string[] args)
{
}
}
}
Properties:
https://i.stack.imgur.com/5wkYV.png
As described in the official documention, you just need to specify your entry point in your .csproj by using the <StartupObject> or the <MainEntryPoint> tag.
In your case you should be writting something like <StartupObject>BeginnerProject.FakeCoin</StartupObject> if you want the second class to be your entry point.
When I try to compile a simple application I made in VSCode with 2 .cs files one with a class, and the other with the main method, I get an error: cannot find type or namespace "MyClass". I do not understand why this happens, as I have put both the main method and class declaration in the same namespace.
MyClass.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes_test.src
{
public class MyClass
{
public void testFunc() { Console.WriteLine("Hello World!"); }
}
}```
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace classes_test
{
class Program
{
static void Main(string[] args)
{
MyClass myClass = new MyClass();
myClass.testFunc();
Console.WriteLine("Hello World!");
}
}
}
I want the console to print Hello World!, but instead I get this error.
classes_test.src
and
classes_test
are not the same Namespace.
In order to set your class visible to the main method, you have to use the following using statement.
using classes_test.src;
This is a quite common mistake, especially for those who are c#-newbie.
Including a namespace, DOES NOT include the subnamespaces
I want to do some tests with the class PluralizationService using dotnetfiddle.net but I get an error:
The type of namespace name 'Design' does not exist in the namespace 'System.Data.Entity'
using System;
using System.Data.Entity.Design.PluralizationServices;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
}
}
link to the example in dotnetfiddle
I've tried adding a reference to EntityFramework (in the NuGet Packages section) but this has not worked. Does anyone know how to solve this error?
I'm quite new to C#, and have made a class that I would like to use in my main class. These two classes are in different files, but when I try to import one into the other with using, cmd says says
The type or namespace name "MyClass" could not be found (are you missing a using directive or an assembly reference?
I know that in Java I have to mess around with CLASSPATH to get things like this to work, but I have no idea about C#.
Additional details:
As you've probably figured out, I'm compiling and executing via command prompt. I'm compiling my non-main class using /target:library (I heard that only main classes should be .exe-files).
My code looks like this:
public class MyClass {
void stuff() {
}
}
and my main class:
using System;
using MyClass;
public class MyMainClass {
static void Main() {
MyClass test = new MyClass();
/* Doesn't work */
}
}
I have tried to encompass my non-main class with namespace MyNamespace { } and importing that, but it doesn't work either.
UPDATE: As of C# 6, if you want to use the static members of a class without specifying the class name, you can use the using static directive to import the static members into the current scope, like this:
using static MyNamespace.MyClass;
Although this is not what the original question is about, you get a similar error to the one OP gets, so I'm providing it for completeness.
Answer to OP's question:
using is for namespaces only - if both classes are in the same namespace just drop the using.
You have to reference the assembly created in the first step when you compile the .exe:
csc /t:library /out:MyClass.dll MyClass.cs
csc /reference:MyClass.dll /t:exe /out:MyProgram.exe MyMainClass.cs
You can make things simpler if you just compile the files together:
csc /t:exe /out:MyProgram.exe MyMainClass.cs MyClass.cs
or
csc /t:exe /out:MyProgram.exe *.cs
EDIT:
Here's how the files should look like:
MyClass.cs:
namespace MyNamespace {
public class MyClass {
void stuff() {
}
}
}
MyMainClass.cs:
using System;
namespace MyNamespace {
public class MyMainClass {
static void Main() {
MyClass test = new MyClass();
}
}
}
I know this is very old question but I had the same requirement and just discovered that after c#6 you can use static in using for classes to import.
I hope this helps someone....
using static yourNameSpace.YourClass;
Well what you have to "import" (use) the namespace of MyClass not the class name itself. If both classes are in the same namespace, you don't have to "import" it.
Definition MyClass.cs
namespace Ns1
{
public class MyClass
{
...
}
}
Usage AnotherClass.cs
using Ns1;
namespace AnotherNs
{
public class AnotherClass
{
public AnotherClass()
{
var myInst = new MyClass();
}
}
}
If they are separate class files within the same project, then you do not need to have an 'import' statement. Just use the class straight off. If the files are in separate projects, you need to add a reference to the project first before you can use an 'import' statement on it.
If the other class is compiled as a library (i.e. a dll) and this is how you want it, you should add a reference from visual studio, browse and point to to the dll file.
If what you want is to incorporate the OtherClassFile.cs into your project, and the namespace is already identical, you can:
Close your solution,
Open YourProjectName.csproj file, and look for this section:
<ItemGroup>
<Compile Include="ExistingClass1.cs" />
<Compile Include="ExistingClass2.cs" />
...
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
Check that the .cs file that you want to add is in the project folder (same folder as all the existing classes in the solution).
Add an entry inside as below, save and open the project.
<Compile Include="OtherClassFile.cs" />
Your class, will now appear and behave as part of the project. No using is needed. This can be done multiple files in one shot.
MyClass is a class not a namespace. So this code is wrong:
using MyClass //THIS CODE IS NOT CORRECT
You should check the namespace of the MyClass (e.g: MyNamespace). Then call it in a proper way:
MyNamespace.MyClass myClass =new MyNamespace.MyClass();
using is used for importing namespaces not classes.
So if your class is in namespace X
namespace X
{
public class MyClass {
void stuff() {
}
}
}
then to use it in another namespace where you want it
using System;
using X;
public class MyMainClass {
static void Main() {
MyClass test = new MyClass();
}
}
namespace MyNamespace
{
public class MyMainClass
{
static void Main()
{
MyClass test = new MyClass();
}
}
public class MyClass
{
void Stuff()
{
}
}
}
You have no need for using a namespace then because it is all encompased in the same namespace.
If you are unsure of what namespace your class is located, type the class (case sensitive you wish to use) then with your cursor on the class, use CTRL + . and it will offer you a manual import.
I created a new project (web project in C#).
Created a new folder in my project called App_Code.
I then proceeded to add a class with the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
And in one of my pages default.aspx in the code behind I tried to do:
int i = BL.JustSomeTest();
I am not getting any intellisense when I type in BL.. It says I am missing an assembly or reference. Or it will say The name BL does not exist in the current context.
But do I have to include a reference if the class file is in the same project? I even tried to Build my project and it at first generated a dll file with the name of my solution file, Shipper.dll but as soon as I add this reference it says The name BL does not exist in the current context.
In my default.aspx page I've tried to add a using statement
using Shipper.
But as soon as I do that my namespace BusinessLayer is not shown...
Im confused?
Edit
Here is default.aspx:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shipper.BusinessLayer;
namespace Shipper
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetDefaults();
int i = BL.JustSomeTest();
}
}
}
}
Here is my BL.cs file:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Shipper.DataLayer;
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
The error reads The type or namespace nameBusinessLayerdoes not exist in the namespace Shipper (are you missing an assembly reference)?
your...
public static int JustSomeTest()
{
return 1;
}
...is out of the 'class' - you cannot have methods defined for the namespace alone.
(at least from your example, it might be just a typo but then you'd need to give us a working example)
your method was outside a class. That's a no-no. Try this instead:
namespace Shipper.BusinessLayer
{
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
}
Also, if you're expecting BL to pop up, make sure the namespace is referenced (using Shipper.BusinessLayer). Why is this a static class, though? I think you probably don't want that unless you're making extension methods.
Try to delete the namespace declaration:
using System;
public static class BL
{
public static int JustSomeTest()
{
return 1;
}
}
This forum thread on Wrox might be also useful.
try
using Shipper.BusinessLayer;
Things to try
Verify the namespace is the one you believe it is by viewing the properties of the target project in the solutions explorer.
In Visual Studio use the Object Browser and locate the namespace and verify the class can be seen. If the object browser can't see it, neither can the code.
First thing is make sure you have added a reference to the Shipper project, not the shipper DLL.
Then make sure the Shipper project is re-built, best bet to do a clean and build.
Then check your intellisense again, I suspect you are referencin an oleder version of the Shipper dll.
Try making your class not static but leave your method as static. (only because I never use static classes, but it doesn't appear to make any difference)
Or if you are seriously having problems and can't work it out install a copy of Resharper and it will probably help!