Hi all,

I am new to C#, so please bear with me.

What I need to do, is to create a method in one class, that can be used to update the scores of an object in another class.

The entire thing is a small program, that creates a VERY small figthing game..

SO - A random "warrior" is created in the game, and IF it is LukeS (Yep, Luke Skywalker..) I need to use the method from my other class to help him, and set his score back to "Healthy". If its not LukeS, nothing happens.

Here the random warrior name is created:

JediKnight warrior = myJedis[randomInt];

                    switch (warrior.Name)
                    {

                        case "Darth Vader":
                            if (!warrior.Deceased)
                            {
                                warrior.AttackEnemy(warrior, LukeS);
                                if (myWarrior.DarkSide == false) { warrior.AttackEnemy(warrior, myWarrior); }
                            }
                            else // this happens if the Emperor points a bony finger at a dead Jedi Knight
                            {
                                warrior.Deceased = false;
                                warrior.currentDamageLevel = JediKnight.DamageLevel.Hurting;
                            }
                            break;

                        case "Luke Skywalker":
                            if (!warrior.Deceased)
                            {
                                // THIS IS WHERE I TRY TO HELP LUKE, AND GIVE HIM A HIGHER DAMAGELEVEL, IF ITS CURRENT < HURTING (25)
                                if (warrior.currentDamageLevel < JediKnight.DamageLevel.Hurting)
                                {
                                    // USE THE OBIWANK.SAVELUKE METHOD - EXPLAINED JUST BELOW...
                                   ObiwanK.SaveLuke(warrior);
                                }

                                warrior.AttackEnemy(warrior, DarthV);
                                if (myWarrior.DarkSide == true) { warrior.AttackEnemy(warrior, myWarrior); }
                            }
                        break;
                    }

In the case: Luke Skywalker, I am using this to give him a better score, if his score has dropped to below "hurting", which is a constant set earlier.

// HELPER METHOD FOR LUKES
if (warrior.currentDamageLevel < JediKnight.DamageLevel.Hurting)
{
// HELP LUKE SKYWALKER AND GIVE HIM A BETTER DAMAGELEVEL:
ObiwanK.SaveLuke(warrior);
}

But nothing happens at all, to the score. I have set ObiWanK earlier up in the program and left it out here. But it should be good..

The other class that has the helper method is like this:

public void SaveLuke(JediKnight Warrior)
    {           
            // HELP LUKES AND GIVE HIM A HIGHER CURRENTDAMAGELEVEL HERE:
            this.currentDamageLevel = JediKnight.DamageLevel.Healthy;
    }

So trying to set his damagelevel to healthy doesnt Work like this, and I have not too much experience in C#, so I have been stuck here for the last two days.

JediKnight is a class, with broader enums defining warrior name and other things. By using it as a parameter, is that not the right way to Refer to it, so that I can target the currentDamageLevel defined in the class, for the specific object LukeS?

Basically, I need to use the object ObiWanK to update the currentDamageLevel of the object warrior, if the warrior name is LukeS..

I hope it makes sense, and will appreciate all help a lot, as I have to deliver tomorrow and my lack of experience is stopping me from getting much further at the moment :-/

Best Regards, Klemme

It appears to me that you should use Warrior.currentDamageLevel instead of this.currentDamageLevel

@tinstaafl: It doesnt Work with warrior instead of this.

Any idea what I am doing wrong? Is it not the correct approach I am looking at?

/Klemme

Capitalization is important in C#. Did you try Warrior.currentDamageLevel and not warrior.currentDamageLevel? C# recognizes those as 2 different names. Perhaps in the SaveLuke routine use a single 'w'. It might be less confusing.

If you are trying the right name and it's not working, then you might have try the ref keyword.

ObiwanK.SaveLuke(ref warrior);

Then in the method

public void SaveLuke(ref JediKnight w)
{
    // HELP LUKES AND GIVE HIM A HIGHER CURRENTDAMAGELEVEL HERE:
    w.currentDamageLevel = JediKnight.DamageLevel.Healthy;
}

For the love of all that is holy, please rename your Obi-Wan Kenobi variable =p

Is JediKnight a struct (value type) or a class (reference type)? If it's a class, you don't need the ref keyword as it's passed by reference anyway. If it's a struct, however, you do.

tinstaafl's point about not using this is correct though, you need to use Warrior.currentDamageLevel = JediKnight.DamageLevel.Healthy;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.