C# web project complaining about a class - c#

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!

Related

Master Import (using) file c#

So I have many classes for a unity video game and it is growing VERY quickly, however
I am using quite a few assets to help and as a result I have many using that
are repeated on each class.
Is there a way in c# or unity to have a master file that just contains all of them for example
namespace MasterImports {
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
}
then just adding something like this to each class by default.
using MasterImports;
I just think this makes managment of my skripts alot easier and
means I am unlikely to run into an error where I forget to add a using
then forgetting what one it was i used and having to hunt scripts for it.
This is a deliberate rule of C#. If you do this:
namespace Frobozz
{
namespace Magic
{
class Lamp {}
}
class Foo
{
Magic.Lamp myLamp; // Legal; Magic means Frobozz.Magic when inside Frobozz
}
}
That is legal. But this is not:
namespace Frobozz
{
namespace Magic
{
class Lamp {}
}
}
namespace Flathead
{
using Frobozz;
class Bar
{
Magic.Lamp myLamp; // Illegal; merely using Frobozz does not bring Magic into scope
}
}
Look into namespace aliases:
using n2 = n1.n2;
...
n2.foo something;
What is before the class name should be a complete name space (with/or other class name(s) for nested types). A truncated namespace will not work.

accessing a method in a dll

okay, so to start with i have set up the references in the project that i am useing the dll in.
what i am trying to do is access the method "haha" in my utils dll
code for dll
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace Utils
{
public class kb
{
public class yes {
public void haha(string yes)
{
int test = Convert.ToInt32(yes);
}
}
}
}
and in the project im trying to access haha in i have just "Utils.kb.yes" but there is no method in that.. all i can do is Utils.kb.yes.equals and Utils.kb.yes.ReferenceEquals.
Since haha() is an instance method, you need to create an instance of the Utils.kb.yes class first:
Utils.kb.yes kb = new Utils.kb.yes();
kb.haha("nextproblem");
Or you also can make the method static:
public class yes {
public static void haha(string yes)
{
int test = Convert.ToInt32(yes);
}
}
then you can call it like this:
Utils.kb.yes.haha("I am static!");
Your classes do not have a constructor, and besides that, you simply CAN'T do much with a class before instantiating an object out of it. So you should reference your dll, and then create a new object first. From within that object, you can then reference your method(s).

C# importing class into another class doesn't work

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.

Can´t find assembly from other project

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;

C# subclass while maintaining name. Deep voodoo?

I have a dll that I'm working with, it contains a class foo.Launch. I want to create another dll that subclasses Launch. The problem is that the class name must be identical. This is used as a plugin into another piece of software and the foo.Launch class is what it looks foe to launch the plugin.
I've tried:
namespace foo
{
public class Launch : global::foo.Launch
{
}
}
and
using otherfoo = foo;
namespace foo
{
public class Launch : otherfoo.Launch
{
}
}
I've also tried specifying an alias in the reference properties and using that alias in my code instead of global, that also didn't work.
Neither of those methods work. Is there a way I can specify the name of the dll to look in within the using statement?
You'll need to alias the original assembly and use an extern alias to reference the original assembly within the new one. Here's an example of the use of the alias.
extern alias LauncherOriginal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace foo
{
public class Launcher : LauncherOriginal.foo.Launcher
{
...
}
}
Here's a walkthrough that explains how to implement that.
Also, you'd mentioned that you tried to use an alias before and encountered problems but you didn't say what they were, so if this won't work then please mention what went wrong.
as Chris said, you can use an alias on your original assembly.
If you can't you that, then you might be able to cheat by using a 3rd assembly
Assembly1.dll (your original)
namespace foo {
public class Launch {}
}
Assembly2.dll (dummy)
namespace othernamespace {
public abstract class Dummy: foo.Launch {}
}
Assembly3.dll (your plugin)
namespace foo{
public class Launch: othernamespace.Dummy{}
}
I'm not even proud of this!
Class name can be identical if it's defined in another namespace, but it boggles the mind why anybody would want to do that to themselves.
Maybe you need to use extern aliases.
For example:
//in file foolaunch.cs
using System;
namespace Foo
{
public class Launch
{
protected void Method1()
{
Console.WriteLine("Hello from Foo.Launch.Method1");
}
}
}
// csc /target:library /out:FooLaunch.dll foolaunch.cs
//now subclassing foo.Launch
//in file subfoolaunch.cs
namespace Foo
{
extern alias F1;
public class Launch : F1.Foo.Launch
{
public void Method3()
{
Method1();
}
}
}
// csc /target:library /r:F1=foolaunch.dll /out:SubFooLaunch.dll subfoolaunch.cs
// using
// in file program.cs
namespace ConsoleApplication
{
extern alias F2;
class Program
{
static void Main(string[] args)
{
var launch = new F2.Foo.Launch();
launch.Method3();
}
}
}
// csc /r:FooLaunch.dll /r:F2=SubFooLaunch.dll program.cs

Categories

Resources