C# can't find namespace vs code - c#

I'm just learning C# so I don't know if I'm just messing up the setup or what, but I used dotnet to create a new project where I'm keeping ALL of my little test scripts. I'm using VS Code as my editor. I create a path variable for csc to compile.
I can compile a Hello World project no problem, but I'm working through this course online and the next part was to make a simple gradebook.
I have GradeMain.cs
namespace Grades
{
public class GradesMain
{
static void Main (string[] args)
{
GradeBook book = new GradeBook();
book.AddGrade(91);
}
}
}
And GradeBook.cs
using System.Collections.Generic;
namespace Grades
{
public class GradeBook
{
public void AddGrade (float grade)
{
grades.Add(grade);
}
List<float> grades = new List<float>();
}
}
When I try to compile using csc I get the following error
error CS0246: The type or namespace name 'Grades' could not be found (are you missing a using directive or an assembly reference?)
My intellisense shows that my GradeBook has two references from GradeMain. I saw this post Referenced Project gets "lost" at Compile Time but since I'm not using visual studio I wasn't sure if it was related / how to check my dotnet client profile.

For me what worked is what #SLaks suggested which was to compile both files with csc.
So runing csc GradesMain.cs GradeBook.cs worked perfectly.

Related

The type or namespace ,,blabla" error appearing after i build

As the title suggests, when i'm trying to build my unity project on my phone i get:
error CS0246: The type or namespace name 'InitializeOnLoadMethod' could not be found (are you missing a using directive or an assembly reference?)
But the project is working fine. No errors in visual studio, no errors while playing the game, nothing.
I tried to change the solution .NET version in visual studio from
.NET Framework 4.7.1
to
unity 3.5 .net full Base Class Libraries
(i did that without really knowing what i was doing so maybe i did something wrong)
But then i got even more errors. Can someone please explain what to do? i fell helpless
using System.Linq;
using System.Reflection;
using UnityEditor;
using UnityEngine;
[ExecuteInEditMode]
public class ExecuteOnRefresh : MonoBehaviour
{
[InitializeOnLoadMethod]
private static void OnInitialized()
{
var propTypes = Assembly.GetAssembly(typeof(Property)).GetTypes().Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(Property)));
EntityBuild.PROP_NAMES = propTypes.Select(type => type.ToString()).ToList();
}
}
InitializeOnLoadMethod attribute is editor-only stuff. Either you have to move your script in a folder named "Editor", or surround your code between #if UNITY_EDITOR - #endif, so Unity won't try to compile it into the build.
https://docs.unity3d.com/ScriptReference/InitializeOnLoadMethodAttribute.html

Problem setting up VS2019 Community for unit testing a dll

I am using vs 2019 Community and have created a Class Library (.NET Framework) project named TestDLL.
The project contains a single file "TestDLL.cs" which has TestDLL as a namespace, a public class named MyDll and a single method
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace TestDLL
{
public class MyDLL
{
static bool Opposite(bool input)
{
return !input;
}
}
}
To the solution I added Unit Test Project (.NET Framework) named UnitTest, added TinyLib_dll.dll as a reference, and created the very simple test.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
bool start = true;
bool result = MyDLL.Opposite(start);
Assert.AreNotEqual(start, result, "MyDll.Opposite failed");
}
}
}
When I try to run the test, the dll file compiles successfully, but the UnitTest fails with "Error CS0103 The name 'MyDLL' does not exist in the current context UnitTest".
It appears that although I have referenced the dll as a reference it is not being loaded into the test project.
Can someone help me out?
You appear to be missing a TestDLL namespace in the test in order for the test to recognize the subject class
either add a using namespace
using TestDLL;
//...
or use the full name of the type
//...
bool result = TestDLL.MyDLL.Opposite(start);
//...
In my haste to resolve my problem i submitted code samples with simple compilation errors. Correcting these errors resolved my stated problem. However I now have a new issue with the same project, but for clarity I will submit a new question. I offer thanks to Nkosi for his assistance.

Compile C# class using SOAP Web Reference into .dll Error CS0234

Hello I'm trying to compile my C# code into a .dll file to be used client-side in a VBA script.
My C# code references a SOAP Webservice and isn't behaving well. When I run csc /target:library file.cs I get the following errors CS0234 on com, and CS0246 BbsSecurityContext,BbsEntityResult (Classes within the webservice). It says that the com namespace does not exist in namespace SOAPTest. Please let me know if I can supply more information.
Here is a snippet of code from the file
...
using SOAPTest.com.blueberryhosting.broadviewbbsvcstg;
using System.Runtime.InteropServices;
namespace SOAPTest
{
[Guid("9E5E5FB2-219D-4ee7-AB27-E4DBED8E123E")]
[ClassInterface(ClassInterfaceType.AutoDual)]
public class Webservice
{
private BbsSecurityContext sc;
public Webservice(string apiKey, string sourceSystem, int userID)
{
createSecurityContext(apiKey, sourceSystem, userID);
}
...
public void getInvestors(string filepath)
{
using (BbsInteropWebService ws = new BbsInteropWebService())
{
BbsEntityResult x = ws.GetInvestors(sc);
if(x.Success)
{
generateCSV(x,filepath);
}
}
}
...
Your snippet includes using SOAPTest.com.blueberryhosting.broadviewbbsvcstg;. I suspect you recieved the error because the csc.exe command you specified included only file.cs and that namespace was not defined in the file. Most likely it was defined in another code file or a library. You need to provide all dependencies when you invoke the compiler.
Command-line Building With csc.exe
How to use references when compiling c# code via command line

Build error of C# reference of name space in two projects in VS 2013 win 7

I am doing C# programming on Visual Studio 2013 win 7.
In project 1 I have:
using test.name.abc; // error: type or namespace "test" cannot be found.
public class testName2 : testName1 // build error here
{
public int id;
}
And in project 2:
namespace test.name.abc
{
public class testName1
{
public int dd;
}
}
I have added reference of dll file generated from project2 in project1. I also made the build dependence as project1 depends on project2.
But, I always got build error of
The type or namespace name 'testName1' could not be found (are you missing a using directive or an assembly reference?)
Any help would be appreciated.
Namespaces and type names connot begin with a numeric character.
http://msdn.microsoft.com/en-us/library/aa691136(v=vs.71).aspx

Namespace/Reference errors

My solution wont compile. I am getting an error message when i try to compile my project:
Error 2 The type or namespace name 'Security' does not exist in the namespace 'Base' (are you missing an assembly reference?)
It is confusing, however, because i have referenced the project and that is the correct namespace! here's the solution setup
Solution Base
- Base.Domain
- Base.Security
- Base.Tests
- Base.WebUI
in Base.Security I have a custom role provider file like this:
namespace Base.Security.Providers
{
public class EFRoleProvider : System.Web.Security.RoleProvider
{
//code here
}
}
I have referenced Base.Security in Base.Tests and in Base.Tests I have the following file (that is giving me error):
using Base.Security.Providers;
namespace Base.Tests
{
class Program
{
static void Main(string[] args)
{
var a = new EFRoleProvider();
//more stuffs
}
}
}
I don't get it.. why can I not access Base.Security types from Base.Tests?
Make sure your projects are built against the same .NET version. I've had this issue when adding a reference to a .NET 3.5 project from a .NET 4.0 project.
To check/change the .NET version right click on your project, select properties, and under the "Application" tab make sure the "Target Framework" is identical for each project.
Your code looks correct, it's an issue with the way the reference is set up.

Categories

Resources