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 936687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T21:21:17+00:00 2026-05-15T21:21:17+00:00

I frequently link objects to their parents using: Video parent; Sometimes I have objects

  • 0

I frequently link objects to their parents using:

 Video parent;

Sometimes I have objects that can be children of different object types, so do I:

 int parentType;
 Video parentVideo; // if parent == VIDEO then this will be used
 Audio parentAudio; // if parent == AUDIO then this will be used

Is there a better way?
How do I work with a variable that can be an instance of different types?

Edit: Of course, if Video and Audio inherit from the same baseclass (eg. Media) I could do this:

 Media parent;

But what if the parents do not inherit from the same baseclass?

  • 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-05-15T21:21:17+00:00Added an answer on May 15, 2026 at 9:21 pm

    I am assuming that the types in your question are sealed. In which case I would just use object parent and use as on the way out. (Using as can have a higher performance impact than checking a flag, but… not a concern in anything I have done and it can also be nicely used in a null-guard.)

    Video video = null;
    if ((video = parent as Video) != null) {
      // know we have a (non-null) Video object here, yay!
    } else if (...) {
      // maybe there is the Audio here
    }
    

    The above is actually just a silly C# way of writing a one-off-pattern-match on an unconstrained discriminated union (object is the union of every other type in C# 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 523k
  • Answers 523k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Update: If your DataTable itself is called "Sales.SalesContact", you could… May 16, 2026 at 9:31 pm
  • Editorial Team
    Editorial Team added an answer You can use negative matches or request.user_agent =~ /Mobile|webOS/ &&… May 16, 2026 at 9:31 pm
  • Editorial Team
    Editorial Team added an answer Use this approach instead:- object[] arrayOfThings { get { return… May 16, 2026 at 9:31 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I frequently come across Windows programs that bundle in MSVCRT (or their more current
I have a ListView that gets updated frequently, at irregular intervals. I want it
We have a .net 3.5 web app that consumes third party web services. The
We have a production system that gathers telemetry data from remote devices. This data
I frequently have to deal with DataTables connected to grid controls, custom updating always
I have developed a Delphi web server application (TWebModule). It runs as a ISAPI
I frequently run into large, non-template classes in C++ where simple methods are defined
I find myself frequently writing code like this in the HTML HEAD and other
Our company frequently issues important notices via e-mail as do many other companies do
This is link which is explaining in detail, the problem i am facing right

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.