Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / August 2007

Tip: Looking for answers? Try searching our database.

out with inherited objects

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
RichB - 29 Aug 2007 11:06 GMT
Something I don't understand why it is happening, or how to fix it.....

I have a teacher class which inherits from person.
And within a search class have a search method with the following:

Dictionary<String, Person> people = new Dictionary<String, Person>();
people.Add("a", new Teacher("Bob", "Smith");

Teacher teach;
if (!people.TryGetValue("Jane", out teach)
{
teach = new Teacher ("Jane", "Jones");
}

Error 1 The best overloaded method match for
'System.Collections.Generic.Dictionary<string,DataTier.VOs.VO>.TryGetValue(string,
out DataTier.VOs.VO)' has some invalid arguments C:\Documents and
Settings\Richard Box\My Documents\Visual Studio
2005\Projects\DataTier\DataTier\DataAccess\EventDAO.cs 307 26 DataTier

Error 2 Argument '2': cannot convert from 'out DataTier.VOs.EventVO' to 'out
DataTier.VOs.VO' C:\Documents and Settings\Richard Box\My Documents\Visual
Studio 2005\Projects\DataTier\DataTier\DataAccess\EventDAO.cs 307 57
DataTier

But Teacher is a Person, so why can an implicit conversion not be performed?

Is there a way around this other than not using the TryGetValue() method?

Thanks,
Richard
RichB - 29 Aug 2007 11:10 GMT
Sorry, created my example from my head, then cut and pasted the errors.....
In the errors read
VO = Person,
EventVO = Teacher.

Sorry hope this clears any confusion.
Richard

> Something I don't understand why it is happening, or how to fix it.....
>
[quoted text clipped - 28 lines]
> Thanks,
> Richard
Jon Skeet [C# MVP] - 29 Aug 2007 11:15 GMT
<snip>

> But Teacher is a Person, so why can an implicit conversion not be performed?

Suppose your dictionary had a person *other* than a teacher in...

The simple example of this is something like:

void DoSomething (ref object x)
{
   x = new object();
}

string y = "hello";
DoSomething (ref y);

Something's got to go bang somewhere, otherwise y has a reference to a
non-string object.
The compiler keeps this safe.

> Is there a way around this other than not using the TryGetValue() method?

Yes, use a temporary local variable and then cast.

Jon
RichB - 29 Aug 2007 14:25 GMT
Sorted thanks.

> <snip>
>
[quoted text clipped - 22 lines]
>
> Jon

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.