Frapie
Frapie

Reputation: 225

How can I create an empty object field?

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.

Upvotes: 4

Views: 3737

Answers (3)

Saddam Abu Ghaida
Saddam Abu Ghaida

Reputation: 6739

class test1 
    {
        int x=10;
        public int getvalue() { return x; }
    }
    class test2 
    {
        string y="test";
       public  string getstring() { return y;}

    }
    class Program
    {

        static object a;

        static void Main(string[] args)
        {
            int n = 1;
            int x;
            string y;
            if (n == 1)
                a = new test1();
            else
                a = new test2();

            if (a is test1){
               x = ((test1)a).getvalue();
               Console.WriteLine(x);
            }
            if (a is test2)
            {
                y = ((test2)a).getstring();
                Console.WriteLine(y);
            }
        }
    }

Upvotes: 0

manojlds
manojlds

Reputation: 301207

General approach would be:

interface IRace  //or a base class, as deemed appropriate
{
    void DoSomething();
}

class Orc : IRace
{
    public void DoSomething()
    {
        // do things that orcs do
    }
}

class Elf : IRace
{
    public void DoSomething()
    {
        // do things that elfs do
    }
}

Now heroRace will be declared (outside switch) as:

IRace heroRace;

And within switch you can:

heroRace = new Orc(); //or new Elf();

And then...

heroRace.DoSomething();

Upvotes: 0

Anirudha
Anirudha

Reputation: 32807

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

Upvotes: 4

Related Questions