Jump to content
  • Advertisement
Sign in to follow this  
Spa8nky

[C#] Using CopyTo with lists and interfaces. Can't convert to interface type?

This topic is 2558 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an interface:



interface IFoo
{
bool IsTasty { get; set; }
}


and a class that inherits from it:



class Bar : IFoo
{
private bool isTasty;

public Bar() { }

public bool IsTasty
{
get { return isTasty; }
set { isTasty = value; }
}
}


When I create a list of Bars:



List<Bar> bars = new List<Bar>();
bars.Add(new Bar());


which I wish to store inside an Array of the interface IFoo:


IFoo[] foos = new IFoo[12];


I can do the following:


foos[0] = bars[0];


but if I try and use the 'CopyTo' method, the type conversion fails:


bars.CopyTo(foos, 0);




Argument 1: cannot convert from 'Prototype1.IFoo[]' to 'Prototype1.Bar[]'
[/quote]

How can I get this to work correctly?

Share this post


Link to post
Share on other sites
Advertisement
I dare say a C# guru can do it better, but you could do this:

Array.Copy(bars.ToArray<IFoo>(), foos, Math.Min(bars.Count, foos.Length));

Obviously if you don't mind losing the foo array then you could just overwrite it rather than copy into it...

foos = bars.ToArray<IFoo>();

Share this post


Link to post
Share on other sites
I thought about using the 'ToArray' method but wasn't sure if it would fare better than iterating over the list and copying them across that way. That way some garbage collection would be avoided.

Share this post


Link to post
Share on other sites
I know it doesn't answer your question, but is there any reason you don't just use an List<IFoo>? Or are you wanting to keep a list of derived types then concatenate them later.?

Share this post


Link to post
Share on other sites

[color=#1C2837][size=2]Or are you wanting to keep a list of derived types then concatenate them later.?
[color=#1C2837][size=2][/quote]
[color=#1C2837][size=2]

[color="#1c2837"]That's correct. I have a system where all transparent objects are concatenated into an array of IDrawableTransparent(s). I then project their positions onto the camera's forward and sort them by distance so they can be drawn in the correct order.

Share this post


Link to post
Share on other sites
Ok, so it doesn't work because you're essentially doing ICollection<Bar>.CopyTo( Bar[] array, ...) so you can either have List<IFoo> instead of List<Bar>, convert to a new array as already suggested or you can take the opportunity to have your own sort in place which may(not) turn out to be the best / fast approach for your implimentation.

What you could do with is a CopyFrom() ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!