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
MOD
Allan Clark
Treehouse Moderator 10,799 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!