Jump to content

Getting around error in c#

Go to solution Solved by MikeD,

I imagine you are getting the error after the if blocks.

Unlike some languages (Python is an example I believe) C# variables are scoped. If you define, like you are doing, a variable inside the if block it will not be visible outside. You have to declare the variable outside and assign it in the ifs.

You are probably going to have to use a supertype of the monster instances you are creating and assigning the actual monster to it.

Basically the problem I'm having is getting around the error " Error 11 The name 'playerMonster' does not exist in the current context ". i thought having the else at the end of the block would allow me to get around it but apparently not. code below

            if (monster == "OldDirtyBastard")            {                Monsters.OldDirtyBastard playerMonster = new Monsters.OldDirtyBastard(hero);            }            else if (monster == "DiscoDragon")            {                Monsters.DiscoDragon playerMonster = new Monsters.DiscoDragon(hero);            }            else if (monster == "PirateRobocop")            {                Monsters.PirateRobocop playerMonster = new Monsters.PirateRobocop(hero);            }            else            {                Monsters.SpideryThing playerMonster = new Monsters.SpideryThing(hero);            }

i understand that visual studio thinks that theres a chance playerMonster will never be initialized but I'm stumped as to a good solution without restructuring a lot of my code.

any help would be greatly appreciated, not too worried about how clean the answer is as long as it works :)

Link to comment
Share on other sites

Link to post
Share on other sites

I imagine you are getting the error after the if blocks.

Unlike some languages (Python is an example I believe) C# variables are scoped. If you define, like you are doing, a variable inside the if block it will not be visible outside. You have to declare the variable outside and assign it in the ifs.

You are probably going to have to use a supertype of the monster instances you are creating and assigning the actual monster to it.

Link to comment
Share on other sites

Link to post
Share on other sites

As Mike mentioned you cannot access that variable outside the if blocks as they are scoped, however you can declare player monster above the if blocks and set them within, in your example you are using several different classes so they should all conform to an interface for example IMonster or an abstraction eg class Monster.

So if you assume that all your monster types implement IMonster, you can simply do:

IMonster playerMonster = null;

If(...){

playerMonster = new ......

}

//playerMonster is accessible here

(I would use a code block and type it out nicely but on iPad so it's a pain soz:P)

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×