0

Okay. So what I want to do... I'm trying to store a list of status effects as delegates in an array. The statuses will act like the statuses in the pokemon games.. (Stun makes you lose a turn etc).

I have this so far...

public class Statuses : Chara{
        public static void para(){
            this.health -= 10;
        }
    }

     status[] statuses = new status[]{
        new status(Statuses.para)
    };

It's complaining about this not being a static property, I was wondering how I should proceed.

Thanks heaps.

1
  • 6
    It's not clear what you're trying to do, where you're trying to declare statuses, what the status type is, or what the Chara type is. I would also strongly advise that you start following .NET naming conventions. Commented Aug 2, 2011 at 6:15

2 Answers 2

1

The compiler error you most likely are getting when compiling the Statuses class says it all: “Keyword 'this' is not valid in a static property, static method, or static field initializer”: You are not allowed to reference “this” in a static method. If your health variable is static you can do like this:

private static int health;
public static void para() 
{
    health -= 10; 
}

If health is not static you will get this compiler error “An object reference is required for the non-static field, method, or property 'Statuses.health'.

Another error is that your para is not a property but a method. Since the code you have posted are very out of context a number of different errors could be present at well.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem the compiler is complaining about is that you've marked the method Para as static. You're then trying to access the health property of the current instance using this, which doesn't make sense, given that the containing method is static.

You should read up on the static keyword and its usage.


I think what you wanted to do was to create a delegate that reduces the instance's health, along the lines of (assuming you have a type called pokemon, with a property health):

public class Statuses : Chara{
    public static Action<Pokemon> para =
        (pokemonInstance) => { pokemonInstance.Health -= 10; };
}

Action<Pokemon>[] statuses = new Action<Pokemon>[]{
    Statuses.para
};

Read up on Action<T> and Anonymous Methods.

2 Comments

I know - but delegates can only be used with static methods can't they?
You can also assign a delegate, or in my case Action<T> in-place. In my above code i'm creating a method that has a single parameter, the pokemonInstance. The method will reduce the health by 10. I'm assigning that method to the para static variable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.