accessing files from a difference namespace c# - c#

I have created a project structure like below
-Project.sln
--Provider.csproj
---Middleware
----Helper.cs
---Tests.csproj
----test1.cs
I am trying to access Helper.cs from test1.cs like using Provider.Middleware, but build is failing with message:
The type or namespace name 'Middleware' does not exist in the namespace 'Provider'.
Am I missing something?

Are you sure that you have Middleware as a folder in your project and something like this in your Helper class:
namespace Provider.Middleware{
…
}

Related

c#: Problems with the namespaces

I am having a problem with Visual Studio 2015 and Framework 4.6.1.
I have a business layer with this namespace: BusinessLayer.LocalStorage. In this class (named LocalStorage) I have this function:
...
public static void XXX()
{
}
...
In the main project with the same Framework 4.6.1, I have in one Winform
using BusinessLayer.LocalStorage;
In the Load I wrote the function but the reference is not recognized:
I have to add LocalStorage before:
LocalStorage.LocalStorage.XXX();
This way the call is working
why is not working using LocalStorage.XXX()? I declared my namespace in the using clausules.. Then should be working.
I have the class in a directory inside BusinessLayer project for this reason namespace should be BusinessLayer.LocalStorage
Code is:
namespace BusinessLayer.LocalStorage
{
public class LocalStorage
{
...
public static void XXX()
{
}
...
}
}
This is because LocalStorage is ambiguous, it could either be namespace LocalStorage or the class LocalStorage. You can solve this in 3 ways:
Remove the namespace LocalStorage entirely and move everything in it inside the BusinessLayer namespace
Rename the class LocalStorage to something else
Add using LocalStorage = BusinessLayer.LocalStorage.LocalStorage to the top of your file where you need to use the class LocalStorage
Edit: Option 4. If you desperately want to keep the directory structure as it is right now (with a LocalStorage directory), you can alternatively tell Visual Studio that the directory LocalStorage is not a namespace provider. You can do this by editing the properties of the folder in the Solution Explorer
Visual studio automatically generates Namespaces based on the current location of a class within a solution. When you create a new class it first takes the name of the project. Then it takes the current folder the class is located in. it will continue appending the folder name to the namespace until it reaches the class name itself.
So for example, if you have a project called ExampleProject, with a folder called Models, with a class inside called BaseModel.cs, the resulting namespace would be ExampleProject.Models
Make sure the namespace containing XXX is not LocalStorage.LocalStorage and is called BusinessLayer.LocalStorage. You can change this namespace to whatever you want and rebuild your project.
Although the namespace structure and object names are not recommended to be the same, which will lead to errors in many cases, I see that you want to use it this way from the comments you made.
I don't recommend you to name a class like its namespace, see this article
I would recommend would be <XYZ>LocalStorage.cs
I don't know your layer structure, but you can call it LocalStorage.XXX () with a structure like the one below.
BusinessLayer structure:
LocalStorage Class:
namespace BusinessLayer.LocalStorage
{
public class LocalStorage
{
public static void XXX()
{
}
}
}
And finally, after referencing BusinessLayer to winForm, you can call it in Load method as follows.
using System;
using BusinessLayer.LocalStorage;
using System.Windows.Forms;
namespace WindowsFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
LocalStorage.XXX();
}
}
}
Or, if you don't want a folder in your BusinessLayer structure, you can use it using the same namespace (namespace BusinessLayer.LocalStorage) as below.

Why I cannot use HttpResponseMessage class?

I'm new to ASP.NET Web API and want to make HttpResponseMessage instance from a utility class I made. Then I made very simple class.
But following compile error occurred.
CS0246: The type or namespace name 'HttpResponseMessage' could not be
found (are you missing a using directive or an assembly reference?)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Web;
namespace myapplication.App_Code.Utils
{
public class HttpUtility
{
// compile error here
public HttpResponseMessage GetHttpResponseMessage ()
{
return new HttpResponseMessage();
}
}
}
HttpResponseMessage is available from Controller Class which was made automatically by ASP.NET but not my Utility class.
What am I missing?
I have noticed that you placed your class in App_Code folder in your project. This folder has a special purpose in ASP.NET world and should be used for shared classes and business objects that you want to compile as part of your application. So either move your class into another folder, or change its Build Action in properties section to Compile.

Cannot access BLL Class on ASP.NET

I'm creating a Web Application on ASP.NET and C#.
The problem I have is that I cannot create a instance of a BLL class on the code behind of a page.
I have the Log In Page (LogIn.aspx) and the page that controls de Log In Page (LogIn.aspx.cs). When I am on LogIn.aspx.cs and I try to do: UsersBLL _users = new UsersBLL(), I get the missing a using directive or assembly reference message.
I can fix it by rigth-clicking on UsersBLL.cs class > Properties > Build Action and changing Content to Compile.
At this point I can create an instance of UsersBLL.cs class on LogIn.aspx.cs, but the class get "broken" and does not recognize any "DataSet Instructions" (refer the image to understand it...)
What should I do to fix it?
Thankyou in advance for your answer!
"Broken" UsersBLL.cs class
You need to check what namespace is defined for your USERSTableAdapter, what is your assembly of this class. Add reference to this assembly. Then add the namespace of the adapter in your class UsersBLL. If this USERSTableAdapter is defined in the current project then only the namespace added required. For more help on using ADO.NET and TableAdapters in Visual Studio see this manual

"The type or namespace name 'Models' could not be found" with "using"

I moved my Models classes to a dll file and I try to use it form another project with "using":
using MyApp.Library;
//...
var db = new Models.Database.MyDatabaseEntities();
//...
But I get the error:
The type or namespace name 'Models' could not be found (are you missing a using directive or an assembly reference?)
If I use it like this:
var db = new MyApp.Library.Models.Database.MyDatabaseEntities();
It seems to work. But I want to use "using" since I will need to use Models a lot. Why I cant use "using"? Is there a solution?
Problem : I Suspect that you have multiple Namespaces which refer to Model class.
Solution :
1. You can avoid this ambiguity by using FullyQulaifiedNameSpace as below:
var db = new MyApp.Library.Models.Database.MyDatabaseEntities();
2. You can use Namespace Alias to avoid this ambiguity.
Try This:
using mymodel = MyApp.Library;
var db = new mymodel.Models.Database.MyDatabaseEntities();

How can I access a Masterpage property from a base class or other functions file

I'm accessing Masterpage properties from a regular ASP.NET C# page by doing the following:
((SecondMasterPage)(Page.Master)).speciallink = true;
((SecondMasterPage)(Page.Master)).specialspan = false;
That works fine in a page's code-behind, but when I try to move it to a function in my base class file like this:
public class MyBaseClass : System.Web.UI.Page
{
public void portalFuncs()
{
((SecondMasterPage)(Page.Master)).speciallink = true;
((SecondMasterPage)(Page.Master)).specialspan = false;
}
}
... I get the following error message:
Compiler Error Message: CS0246: The type or namespace name 'SecondMasterPage' could not be found (are you missing a using directive or an assembly reference?)
My base class file is in my App_Code directory and other functions in there work without errors. Why won't this function work? Also if a function like this particular one won't work in my base class file, where could I put it so it would be available to be called from any page?
Unless I'm missing something, you should just be able to go into the codebehind for your SecondMasterPage file, and check the namespace of it. Perhaps the namespace is incorrect or something different than you want.
In your base class, use a using my.masterpage.namespace; statement to get it to work.

Categories