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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:05:17+00:00 2026-06-18T05:05:17+00:00

I need to code a rogue-like game as a project, but I have a

  • 0

I need to code a rogue-like game as a project, but I have a slight problem. There is a time I need to choose between which object to create using a switch. I want to declare an “empty” object outside of the switch and the switch then fills the object with values. This is kind of what I want to do:

Console.WriteLine("What race would you like to be?")

int answer = Convert.ToInt32(Console.ReadLine());

Object heroRace; // This is where the problem comes in

switch(answer)
{
    case 1: heroRace = new Orc(); break;
    case 2: heroRace = new Elf(); break;
}

I want heroRace to be outside of the switch scope for re-usage. It would greatly simplify my program if I could create something like that.

  • 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-18T05:05:18+00:00Added an answer on June 18, 2026 at 5:05 am

    You need to cast the object to more concrete type before accessing it’s members

    Object o=new Orc();
    ((Orc)o).methodNameWithinOrc();
    

    But this can lead to casting exception.

    For example..

      ((Elf)o).methodNameWithinOrc();
    

    would lead to a casting exception because o is an object of Orc not Elf.

    It’s better to check if the object belongs to a particular class before casting using is operator

     if(o is Orc)
    ((Orc)o).methodNameWithinOrc();
    

    Object itself is not useful unless you override the ToString,GetHashCode.. methods

    It should be like

     LivingThingBaseClass heroRace;
    

    Orc and Elf should be subclass of LivingThingBaseClass

    LivingThingBaseClass can contain methods like move,speak,kill..All or some of these methods would be overridden by Orc and Elf

    LivingThingBaseClass can be an abstract class or even an interface depending upon your requirement

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

Sidebar

Related Questions

I need code in JavaScript which returns the time difference between two strings in
i need code like this if($('#someElement').hasClass('test')) { ... } but i need to check
I need code for rotating an image in C++ which functions like imrotate function
Need a much Optimized code.Well I Got a Project and I have Succefully made
Am I doing something wrong if I need code like this in a Controller?
How to create simple PHP COMET server page displaying current time? I need code
I need to code certain Dialogs as Activities that look like dialogs. For this
I need code example please.i tried selectedindexchange but it doesnot register any index change
I'm writing a relatively big project in C++ and have a problem with object
I have a time of Hour and minute. like 21:30 and 21:35 i want

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.