Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

C#

Mitchell Lerdahl
Mitchell Lerdahl
2,471 Points

Why can I not call my method?

Ok I'm pretty confused on why I can not use my CreateContact method in program.cs

Program.cs

Book.cs

Information.cs

1 Answer

Allan Clark
Allan Clark
10,810 Points

This is an issue with the type of your variable. The 'addressBook' variable is explicitly typed as an 'Information' object instead of a 'Book' object. Since a Book is-a 'Information' in this class set up, the compiler won't complain about putting a Book into an Information typed variable. So essentially that variable does not have access to the method. The easiest fix for this would be to change it to an implicitly typed variable, meaning change line 13 in the Program.cs to this:

var addressBook = new Book();

As a side note, in C# the standard is generally to use implicitly typed variables when the type is easily determined. For example when 'new'-ing up an object, or storing a simple string.

Mitchell Lerdahl
Mitchell Lerdahl
2,471 Points

ah ok that makes a lot of sense, thank you!