0

I have class CheckBoxObject and DeployCheckBoxObject that derived from CheckBoxObject.

i have observable collection of CheckBoxObject and for each element of the collection want to use child properties and functions.

how can i convert the parent collection to child ?

public class CheckboxObject : ViewModelBase
{
        public CheckboxObject(object o)
        {
            Object = o;
        }
        public object Object;
        public string Name => Object.ToString();
}

public class DeployCheckBoxObject : CheckboxObject
{
        public DeployCheckBoxObject(object o) : base(o)
        {

        }
        
        public string status { get; set; }
}

public class Test
{
        ObservableCollection<DeployCheckBoxObject> items = new ObservableCollection<DeployCheckBoxObject>();

public void populate(someObject) {

  items = someObject.ToObservableCheckboxCollection(); //error

  }

}

I also try to call child functions with parent object and get invalid cast error

public class Test
{
        ObservableCollection<CheckBoxObject> items = new ObservableCollection<CheckBoxObject>();

  public void populate(someObject) {

   items = someObject.ToObservableCheckboxCollection(); 
   foreach (CheckboxObject cb in CbVendorItems) {
     ((DeployCheckBoxObject)cb).Count = 100; //error
   }
  }

}
public static ObservableCollection<CheckboxObject> ToObservableCheckboxCollection<T>(this IEnumerable<T> enumerableList)
        {
            var a = new ObservableCollection<CheckboxObject>();
            if (enumerableList == null) return a;
            foreach (var item in enumerableList)
            {
                a.Add(new CheckboxObject(item));
            }
            return a;
        }

how can i access to child methods ?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Adi1992
  • 47
  • 5
  • where is your definition of `ToObservableCheckBoxCollection`? – TJ Rockefeller Apr 06 '22 at 15:45
  • Can you try and provide a [mcve]. The code above references things that don't exist - e.g. `ToObservableCheckBoxCollection`, `DeployCheckBoxObject.Count`, `CbVendorItems` - and `someObject` doesn't have a type. – Charles Mager Apr 06 '22 at 15:46
  • Sounds like an XY Question. Do your really need an `ObservableCollection`, or do you just want to set a property of `DeployCheckBoxObject` on some elements of an `ObservableCollection` that may be of type `DeployCheckBoxObject`. – Heretic Monkey Apr 06 '22 at 16:05
  • In fact you can assign an ObservableCollection to an object without errors. https://dotnetfiddle.net/IVXTo5 – McNets Apr 06 '22 at 17:11

1 Answers1

0

Do you know that all the CheckBoxObjects in your collection are actually DeployCheckBoxObjects, if so, then why not make your observable collection of the type DeployCheckBoxObject instead of CheckBoxObject

If you don't know for sure, and some CheckBoxObjects may not be a DeployCheckBoxObject then you need to check for type safety before casting

foreach (CheckboxObject cb in CbVendorItems)
{
    if (cb is DeployCheckBoxObject deployCb)
    {
        deployCb.Count = 100;
    }
}
TJ Rockefeller
  • 3,178
  • 17
  • 43