Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C# ASP.NET MVC Basics Modeling and Presenting Data Adding the Repository

cyd cason
cyd cason
2,901 Points

Can you help me understand the concept?

In the video (@3:07), the instructor says “By making this field static, we're associating it with the ComicBook repository class instead of the individual object instances of this class.”

Can anyone explain that for me? In particular, what does * individual object instances of this class * mean? I get that we made an array, filled it with data , and called it ComicBookRepository. I get that ComicBookRepository is a class. I am lost on the phrase * individual object instances of this class*. I don’t know what that means…..conceptually speaking.

I am pressing on, but I can tell I am missing a concept.

2 Answers

There are two ways to write in a class. The first is to write methods, getters and setters, and so forth. Those are actually non-static methods that are only used when a class has been instantiated first. That is, a new object has been created. In the case of the ComicBookRepository, you'd have ComicBookRepository comicBooks = new ComicBookRepository(). And then you could use your methods, getters, setters, and so forth. These (non-static) functions are called from the class.

However, this leaves us with a bit of a disadvantage, as in the case of the ComicBookRepo. We don't want to have to create a new object every time in order to use the Repo because that would be an expensive operation to receive back a single array. So, instead, we use static keyword to allow us to call the method on the class itself. In other words, we're allowed to use it without creating a new object, because the method is not attached to any one single instance. Where, as in, ComicBookRepository comicBookRepo = new ComicBookRepo(name = "John Smith" ), the name John Smith is associated only with comicBookRepo, the getRepository() is not; it's attached to ComicBookRepository and can only be called on ComicBookRepository; not on comicBook Repo, for example.

This is definitely a tough concept at first, but it's also a very powerful feature of most OOP programming languages, and one that you'll definitely want to have down pat, because of what it allows you to do to simplify your code.

Hope that helps.

Charles-Antoine Francisco
Charles-Antoine Francisco
27,426 Points

The advantage of making the the array static is that there is only one instance of the array. So, every time we want to access the _comicBooks array, we don't have to create a new instance of ComicBookRepository. This is an application of the "DRY" principle. This avoids copying the object for no reason, when we only need one.