Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8333991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:12:17+00:00 2026-06-09T03:12:17+00:00

I have two custom objects, lets say Cat and Dog . I want to

  • 0

I have two custom objects, lets say Cat and Dog. I want to create an observable collection that can hold either of these objects, as they are very similar. Can I use base classes to do this?

If so, would you mind providing a quick example.

And if I do use a base class, does that mean if I want to use common fields, I should put those into the base class?

EDIT: I’m hoping to then bind a WPF datagrid to the properties of these objects. I don’t know if it’s possible to bind a datagrid in WPF to two different kind of objects…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T03:12:18+00:00Added an answer on June 9, 2026 at 3:12 am

    Yes, in that case you can use a base class:

    public class Pet
    {
        public int Id { get; set; }
        public string Name { get; set; }
    
        public void Run() { }
    }
    
    public class Cat: Pet
    {
        public string Meow()
        {
            return "Meow";
        }
    }
    
    public class Dog :Pet
    {
        public string Bark()
        {
            return "Whow";
        }
    }
    

    Notice however, that when you place instances of both classes in one collection you can access only members of the base class.
    So this code is valid:

    var collection = new ObservableCollection<Pet> {new Cat(), new Dog()};
    
    foreach (var pet in collection)
    {
       pet.Run();
    }
    

    but you cannot use methods Meow() and Bark() unless you use explicit casting.

    Be careful with moving too many members to the base class – in the example above Meow() doesn’t make sense for the Dog and Bark() for the Cat. If you need to use some method specific to the derived class you could check the type with:

    pet.GetType() 
    

    and then cast object to the derived type:

    var cat = (Cat)pet;
    cat.Meow();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function that I want to operate on two different custom objects.
I have a User-class that contains a HashMap to map between two custom objects.
I want to create two types of custom objects. Each has a property which
I have two lists of custom objects and want to update a field for
I have an NSMutableArray that contains a few custom objects. Two of the objects
I have one custom action (add) and two custom objects (favorites list) and (tv
I have two apps that use custom URL schemes to switch between each other.
Let's say I have myapp.appspot.com and two custom domains respectively called foo.com and bar.com
I'm trying to create a simple, custom, gallery. I have two lists, one with
Still having trouble with this language. Ok, let's say I have two objects. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.