locked
How do namespace work in C#? RRS feed

  • Question

  • Hi,

    I'm new to C# and .NET Core Framework. I'm trying to change the file structure of my project. I created a 'Models' Folder and followed convention similar to the 'Controllers' folder that already exists setting the namespace to  'project.FolderName' but when I add this namespace and move the file my controller file cant recognize the class anymore. I've also tried importing the class like this 

    using User.Models

    using System;
    using System.Data.Entity;
    
    namespace project.Models
    {
        public class User
        {
            public string email { get; set; }
    
            public string username {get; set;}
    
            public string password { get; set; }
        }
    }

    What am I missing. When I leave the class in the root of the project with the namespace just ass my project name it works.


    Monday, July 20, 2020 5:03 AM

All replies

  • Hi Arvind16,

    Thank you for posting here.

    I cannot reproduce this error.

    After I modify the namespace, as long as I add a reference to this namespace when using this class, it will not affect the use no matter where the class is placed.

    Edit:

    You show above:

    using User.Models

    Is your project called User?

    If so, you need to use the User class like this:

              Models.User user = new Models.User();

    If not, could you please show us the error message?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, July 20, 2020 8:57 AM
  • Hello,

    The following may help to understand about namespaces. The project focuses on Entity Framework Core, a folder Models for each table in a database, a Class folder for worker classes, a Context folder for DbContext.

    If you right click on the image below and open in a new browser window it will help to see via color coding a class in the class folder how I reference classes in the Model folder and the DbContext in the Contexts folder.

    If I comment out using North.Contexts; we get

    Then change to this we are working again.

    So now in the last line Visual Studio knows where to find NorthwindContext. Does this make sense?

    The project is here

    https://github.com/karenpayneoregon/NorthWind-2020


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Proposed as answer by CoolDadTx Monday, July 20, 2020 2:01 PM
    Monday, July 20, 2020 10:31 AM
  • using project.Models;

    It should be a statement at the top of the controller class if you want to access any class class in the Models folder from the controller class. PublishingCompany is the name of the ASP.NET MVC project. The Models folder is a folder in PublishingCompany that contains IAuthorDM and AuthorDM, and there must be a using using PublishCompany.Models in the Authorcontroller to use the AuthorDM class.

    ASP.NET MVC issues can be discussed at teh ASP.NET forums.

    https://forums.asp.net/

    using Microsoft.AspNetCore.Mvc;
    using PublishingCompany.Models;
    
    namespace PublishingCompany.Controllers
    {
        public class AuthorController : Controller
        {
            private IAuthorDM adm;
            public AuthorController(IAuthorDM authorDM)
            {
                adm = authorDM;
            }
    
            public IActionResult Index()
            {
                return View(adm.GetAll());
            }
    
            public IActionResult Detail(int id = 0)
            {
                return id == 0 ? null : View(adm.Find(id));
            }
    
            public IActionResult Create()
            {
                return View(adm.Add());
            }
    
            [HttpPost]
            public ActionResult Create(AuthorVM.Author author, string submit)
            {
                if (submit == "Cancel") return RedirectToAction("Index");
    
                if (!ModelState.IsValid) return View(author);
    
                adm.Add(author);
                return RedirectToAction("Index");
            }
            
            public ActionResult Edit(int id = 0)
            {
                return id == 0 ? null : View(adm.Update(id));
            }
            
            [HttpPost]
            public ActionResult Edit(AuthorVM.Author author, string submit)
            {
                if (submit == "Cancel") return RedirectToAction("Index");
                            
                if (!ModelState.IsValid) return View(author);
                           
                adm.Update(author);
                return RedirectToAction("Index");
            }
            public IActionResult Delete(int id = 0)
            {
                if (id > 0) adm.Delete(id);
    
                return RedirectToAction("Index");
            }
    
            public ActionResult Cancel()
            {
                return RedirectToAction("Index", "Home");
            }
        }
    }
    using System.Linq;
    using ServiceLayer;
    using Entities;
    
    namespace PublishingCompany.Models
    {
        public class AuthorDM :IAuthorDM
        {
            private IAuthorSvc svc;
            public AuthorDM(IAuthorSvc authorSvc)
            {
                svc = authorSvc;
            }
    
            public AuthorVM GetAll()
            {
                var vm = new AuthorVM();
    
                var dtos = svc.GetAll().ToList();
    
                vm.Authors.AddRange(dtos.Select(dto => new AuthorVM.Author()
                {
                    AuthorID = dto.AuthorId,
                    FirstName = dto.FirstName,
                    LastName = dto.LastName
                }).ToList());
    
                return vm;
            }
    
            public AuthorVM.Author Find(int id)
            {
                var dto = svc.Find(id);
    
                var author = new AuthorVM.Author
                {
                    AuthorID = dto.AuthorId,
                    FirstName = dto.FirstName,
                    LastName = dto.LastName
                };
    
                return author;
            }
    
            public AuthorVM.Author Add()
            {
                return new AuthorVM.Author();
            }
    
            public void Add(AuthorVM.Author author)
            {
                var dto = new DtoAuthor
                {
                    FirstName = author.FirstName,
                    LastName = author.LastName
                };
    
                svc.Add(dto);
            }
    
            public AuthorVM.Author Update(int id)
            {
                var dto = Find(id);
    
                var author = new AuthorVM.Author
                {
                    AuthorID = dto.AuthorID,
                    FirstName = dto.FirstName,
                    LastName = dto.LastName
                };
    
                return author;
            }
    
            public void Update(AuthorVM.Author author)
            {
                var dto = new DtoAuthor
                {
                    AuthorId = author.AuthorID,
                    FirstName = author.FirstName,
                    LastName = author.LastName
                };
    
                svc.Update(dto);
            }
    
            public void Delete(int id)
            {
                var dto = new DtoId
                {
                    Id = id
                };
    
                svc.Delete(dto);
            }
           
        }
    }
    

    namespace PublishingCompany.Models
    {
        public interface IAuthorDM
        {
            AuthorVM GetAll();
            AuthorVM.Author Find(int id);
            AuthorVM.Author Add();
            void Add(AuthorVM.Author author);
            AuthorVM.Author Update(int id);
            void Update(AuthorVM.Author author);
            void Delete(int id);
        }
    }

    Monday, July 20, 2020 4:18 PM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, July 30, 2020 8:54 AM