0

The constructor for this controller is returning the error "} expected". Visual studio is not recognizing it as a constructor. Other errors include "method must have a return type".

What is wrong with this code?

namespace blahblah.Controllers
{

    public class HomeController : Controller
    {  
        private IUserRepository userRepository;

        public HomeController()
        {     //} expected here
            public Model1Container db = new Model1Container();
            this.userRepository = new UserRepository(db);
        }

        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
             return View();
        }
    }
}

1 Answer 1

3

Your constructor should look like this:

public HomeController()
{   
    Model1Container db = new Model1Container();
    this.userRepository = new UserRepository(db);
}

Remove word public near the local variable db. Access modifiers are not accessible with local variables, as their scope is limited to the method body where they're declared.

From C# language specification 4.0:

A local variable declaration specifies a type name, a variable name, and possibly an initial value.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.