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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:59:44+00:00 2026-06-06T09:59:44+00:00

For example, i have this: class BasePacket { int header; int type; } class

  • 0

For example, i have this:

class BasePacket
{
   int header;
   int type;
}

class ChildPacket1 : BasePacket
{
   //...
}

class ChildPacket2 : BasePacket
{
   //...
}

BasePacket bp;
Type t;
object obj = CreateNeededChildPacket(out t); //return one of childs as object and it's real type
bp = ...// anyway to cast obj to type represented by t? or by using something else?
  • 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-06T09:59:48+00:00Added an answer on June 6, 2026 at 9:59 am

    In your example you are creating one of two derived classes and storing it in a base variable…this does not require any casting at all as it is perfectly fine to do this per the laws of inheritance. A base-type variable can always be assigned a more derived object since anything the base class would be able to do it is guaranteed the derived class can do as well, so it is safe. For example, a Math teacher can perform any action a regular Teacher can (such as GradePapers()). It would be safe to point a Math Teacher object with a Teacher variable.

    BasePacket bp = (BasePacket) CreateNeededChildPacket();
    

    Edit:
    In response to your comment, Ideally you would have the CreateNeededChildPacket() method return a BasePacket type (if the returned object is always dervied from BasePacket). Just as a base parameter can accept a derived object, a base return type can return a derived object, this is the beauty of inheritance. This would be much safer for someone down the road calling your method.

    BasePacket CreateNeededChildPacket() 
    {
        // do something
        return AnyObjectThatDerivesFromBasePacket;    
    }
    

    If you cannot update the method then you could add some additional type-checking just to be safe.

    object obj = CreateNeededChildPacket();
    BasePacket bp = null;
    
    if (obj is BasePacket) 
       bp = (BasePacket) obj;
    else
       throw new Exception("Object was not a valid BasePacket type: " + obj.GetType.ToString());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For example I have this: class A{ private int mine = 0; //Some field...
So for example I have this code: class Object{ public $tedi; public $bear; ...some
i have this example: class One { public void testOne(){System.out.println(One!!!);} public void testTwo(){System.out.println(One!!!);} }
I have this example: public class Inheritance { public static class Animal { public
I have this simple example: using System; using System.Collections.Generic; namespace ConsoleApplication1 { class Program
Say I have a class ObjectA (a view controller for example), that does this
Before Consider to have a class and a global function: This is, for example,
i have this class for example: class labeledtext { private TextBox tb; private Label
For example, i have this: class Family < ActiveRecord::Base :has_many :members def aging(date) members.find_all_by_birthday(date).each
For example I have this css class: .class{ background:#FFF; border:1px solid #ccc } And

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.