views:

1144

answers:

5

How can i cast

from ObservableCollection<TabItem> into ObservableCollection<object>

this doesnt work for me

(ObservableCollection<object>)myTabItemObservableCollection
A: 

You can't. ObservableCollection<TabItem> does not derive from ObservableCollection<object>.

If you explain why you would want to perhaps we can point out an alternative interface you can use.

AnthonyWJones
+3  A: 

you can't in .net 3.5, .net 4 solve this problem in more elegant way. in .net 3.5 you should copy like this

return new ObservableCollection<object>(myTabItemObservableCollection);
ArsenMkrt
you forgot the `new` keyword
Drew Noakes
thanks Drew, I added it
ArsenMkrt
+3  A: 

Basically, you can't. Not now, and not in .NET 4.0.

What is the context here? What do you need? LINQ has Cast<T> which can get you the data as a sequence, or there are some tricks with generic methods (i.e. Foo<T>(ObservalbleCollection<T> col) etc).

Or you can just use the non-generic IList?

IList untyped = myTypedCollection;
untyped.Add(someRandomObject); // hope it works...
Marc Gravell
A: 

you could use IEnumerable.Cast<T>()

Hath
A: 

thanx for all answers, but I think I have solve this problem self with a "helpermethode".

Perhaps has any a better method or a linq statement for this.

private void ConvertTabItemObservableCollection()
{
  Manager manager = this.container.Resolve<Manager>();
  foreach (var tabItem in manager.ObjectCollection)
  {
    TabItemObservableCollection.Add((TabItem)tabItem);
  }
}
Mario Priebe
Since we don't know what ObjectCollection is typed as, that is pretty hard to answer...
Marc Gravell
I dont understand what u mean?! I have a TabItem what i should like add into a ObservableCollection from type object."Manager" is a global classes with a ObservableCollection<object> what i need in any views/components in my prism application.
Mario Priebe
Ah sorry i have misunderstood ur Answer. "ObjectCollection" is typed as object ObservableCollection<object>
Mario Priebe
yea but isn't my answer shorter? and solved the same problem by same way with less code
ArsenMkrt
you are right, thx for this : )
Mario Priebe