I have a class called Foo that has a function that looks like the following
List<Bar> LoadData();
Both Foo and Bar are in a library that I want to reuse in other projects. Now I am working on a new project and I want to subclass Bar. Let's call it NewBar.
What is a simple and flexible way to get Foo.LoadData to return a list of NewBar? I think that a factory is needed or perhaps just a delegate function. Can anyone provide an example?
Thanks, Andy
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
Maybe even simplier, depends on what you need, but generic constraints may suffice if Bar is constructed with parameterless constructor:
public List<TBar> LoadData<TBar>()
where TBar : Bar, new()
{
// return a list populated with `new TBar()`;
}
Usage:
var newBars = Foo.LoadData<NewBar>();
The easiest way would be to have an interface implemented by Bar and NewBar, and have your function return List<IBar>
instead.
It's that or return object
and cast galore.
There are a few problems here. Since you are using List<Bar>
this would not work if you return List<Baz>
since List<>
does not do covariance.
My suggestion is to redesign it to return IEnumerable<Bar>
and make it virtual
so that in the new project create a SubFoo
and you can return IEnumerable<Baz>
.
OK, according to the new information you provided (you use it for populating list from XML), I would create a virtual protected CreateBar()
which creates a new bar object and is called by LoadData() to create new Bar
in the loop. In the SubFoo
I override and return Baz
instead. In the SubFoo
I will call base.LoadData()
and populate the Baz
list and then add the logic to populate new properties of Baz
which are not in Bar
.
just use XML Serilization and I get all of that for free!
Make LoadData()
virtual. Then override it in a derived factory.
If the derived factory method would still return a list of Bar
, but you could specify in the method implementation that all of the Bar
instances are actually NewBar
. This could be altered by using a generic method like in Snowbear's example.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With