开发者

namespace error in c#

I've a class in namespace Inventory_Software.BL with name BL, and I want to access this class in Inventory_Software.PL.Item_Master class, but despite of using syntax using Inventory_Software.BL before namespace name in that class, I'm getting an error:

'Inventory_Software.BL' is a 'namespace' but is used like a 'type'

whenever I try to initialized BL class with syntax:

BL businesslayer = new开发者_StackOverflow BL();

Could anyone please tell me why?


Basically you're in a world of pain when you start give a class the same name as a namespace. Just don't do it - rename either your class or your namespace.

You probably can work round it with suitable using directives, but is it really worth that much pain? (I personally wouldn't use "BL" as a name for either a namespace or a class, nor would I use an underscore within a name; but that's a different matter.)

Eric Lippert has a series of four blog posts about this - "Do not name a class the same as its namespace":

  • Part 1
  • Part 2
  • Part 3
  • Part 4


since its Inventory_Software.BL, you have initialized a namespace inside a namespace, instead, do this:

Inventory_Software.BL businessLayer = new Inventory_Software.Bl();


The compiler can't tell the difference between whether you're referencing the namespace or they type. Either change the namespace to not be BL or change the name of the class to be something other than BL. Maybe call it BusinessLayer instead...


Have you checked to see if you have a namespace called BL and a class called BL. If so, you'll need to use the full reference to the class or change the name of your class/namespace.


You are trying to create an object of BL. But BL is not a class. It is just a namespace.

You can create object of a class by specifying the name of the class. In your code, you must be having something as

public class NameofYourClass

{

// your methods here

}

So, in this case, you will be able to write

NameofYourClass yourObject = new NameofYourClass();


Looks like you have a class Inventory_Software.BL.BL and you're trying to access it from scope of Inventory_Software.PL.Item_Master.

As expected an unqualified name BL would resolve to 'Inventory_Software.BL' and not 'Inventory_Software.BL.BL'.

You could do away with by adding either of following usings

using Inventory_Software.BL; OR simply using BL;

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜