How to remove variable of Specific Type from Object[] in C#
How to remove variable of Specific Type from Object in C#
I have an array of Objects
type which have information of variable of different classes, say, ClassA
and ClassB
.
Objects
ClassA
ClassB
public object SelectedObjects get;
Now I am filtering the objects of ClassB
which I need to remove from the array of SelectedObjects
.
ClassB
SelectedObjects
var selectedClassBObjects = SelectedObjects.OfType<ClassB>().ToList();
When I perform the operation of Remove()
or RemoveAll()
, it does not do anything.
Remove()
RemoveAll()
Can anyone suggest me how to perform this operation?
I need to remove objects of ClassB
because at a time only one object of ClassB
can be present when user is trying to Highlight the objects on the canvas. First remove the ClassB
objects then add the newly selected ClassB
objects to SelectedObjects
.
ClassB
ClassB
ClassB
ClassB
SelectedObjects
List<T>
public List<object> SelectedObjects get;
SelectedObjects.RemoveAll(item => !(item is ClassB));
maybe this will help you: stackoverflow.com/questions/496896/…
– AsfK
Sep 3 at 9:24
2 Answers
2
You can't remove anything from an array, you will need to create a new one. If you want to exclude objects of type ClassB
you can do:
ClassB
SelectedObjects = SelectedObjects.Where(x => !(x is ClassB)).ToArray();
If you want a modifiable collection, List<T>
is a better choice than an array.
List<T>
SelectedObjects get;
is read only property; SelectedObjects =
probably (if it's not in the constructor) will not compile– Dmitry Bychenko
Sep 3 at 9:26
SelectedObjects get;
SelectedObjects =
yeah well, do I have to say everything?
– Selman Genç
Sep 3 at 9:26
If you insist on array you can put the backing field explicitly:
private object m_SelectedObjects = new object[0];
public object SelectedObjects
get
return m_SelectedObjects;
private void MyRemove()
// We can't modify read-only property but can operate with its backing field
m_SelectedObjects = m_SelectedObjects?.OfType<ClassB>()?.ToArray();
Another possibility is to change array object
into List<object>
:
object
List<object>
public List<object> SelectedObjects get;
...
// We still can't assign SelectedObjects but we can modify the collection now
SelectedObjects?.RemoveAll(item => !(item is TypeB));
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
if you want to modify the collection, array is not a good choice; try
List<T>
:public List<object> SelectedObjects get;
; thenSelectedObjects.RemoveAll(item => !(item is ClassB));
– Dmitry Bychenko
Sep 3 at 9:23