none
Public,static and instance C# RRS feed

  • Question

  • I want to understand the public, static and instance in C# and the interrelate with OOP.

    And Overloading

    • Edited by selvatori Sunday, July 28, 2019 4:59 PM
    Sunday, July 28, 2019 3:44 PM

Answers

All replies

  • class vs object vs instance OOP is OOP.

    http://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    Static means the class does't have to be instantced

    Overload means two methods with the same method name but with two different signatures and two different functionalities. 

    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);
            }
           
        }
    }
    

    Sunday, July 28, 2019 5:32 PM
  • Hi selvatori,

    Thank you for posting here.

    Here are some links could help you to understand them.

    public (C# Reference)

    static (C# Reference)

    Objects (C# Programming Guide) - instance

    Object-Oriented Programming (C#)

    Best Regards,

    Jack


    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.

    • Marked as answer by selvatori Monday, July 29, 2019 11:54 AM
    Monday, July 29, 2019 6:04 AM
    Moderator