Class problem

S

shapper

Hello,

I have the following class:

public class BoxLeaf {
public Box Box { get; set; }
public Validator Validator { get; set; }

public bool Validate() {
this.Validator.Add(Rule.NonEmpty(this.Box.Name), "Fill the
name");
return this.Validator.Success;
}
}

If I have something like:

BoxLeaf leaf = new BoxLeaf();
leaf.Validate();

Validate is not recognized.

If I change my code to:

public static bool Validate() {
this.Validator.Add(Rule.NonEmpty(this.Box.Name), "Fill the
name");
return this.Validator.Success;
}

Then I get the following error:
Keyword 'this' is not valid in a static property, static method, or
static field initializer

How should I do this?

Thanks,
Miguel
 
D

DH

shapper said:
Hello,

I have the following class:

public class BoxLeaf {
public Box Box { get; set; }
public Validator Validator { get; set; }

public bool Validate() {
this.Validator.Add(Rule.NonEmpty(this.Box.Name), "Fill the
name");
return this.Validator.Success;
}
}

If I have something like:

BoxLeaf leaf = new BoxLeaf();
leaf.Validate();

Validate is not recognized.

If I change my code to:

public static bool Validate() {
this.Validator.Add(Rule.NonEmpty(this.Box.Name), "Fill the
name");
return this.Validator.Success;
}

Then I get the following error:
Keyword 'this' is not valid in a static property, static method, or
static field initializer

How should I do this?

Thanks,
Miguel
The code you have provided should work. The reason you cannot use 'this'
is because static items do not have instances so you cannot refer to a
specific instance.
I copy pasted your code into VS and did the same thing and it works and
compiles (not the static )
Make sure that your namespace is correct and that you are using the
correct namespace. but then BoxLeaf leaf = new BoxLeaf() should not work
either.
What is the exact error you get when you say Validate is not recognized?
 
S

shapper

The code you have provided should work. The reason you cannot use 'this'
is because static items do not have instances so you cannot refer to a
specific instance.
I copy pasted your code into VS and did the same thing and it works and
compiles (not the static )
Make sure that your namespace is correct and that you are using the
correct namespace. but then BoxLeaf leaf = new BoxLeaf() should not work
either.
What is the exact error you get when you say Validate is not recognized?

Somehow it started working ... I probably had some mistake and
corrected without noticing it.

Thanks,
Miguel
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top